From b62b8b9aa5ee5035ca1e02e1191a22a21d0228f4 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Tue, 11 Jun 2013 13:07:19 +0000 Subject: [PATCH] ASTUnit: Invert the dependency of PrecompilePreambleAction on PrecompilePreambleConsumer. Actions outlive consumers. PR16295. Found by AddressSanitizer. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183741 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/ASTUnit.cpp | 83 +++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index b03e71ccbb..c72e4eb3c5 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -966,16 +966,37 @@ public: } }; +class PrecompilePreambleAction : public ASTFrontendAction { + ASTUnit &Unit; + bool HasEmittedPreamblePCH; + +public: + explicit PrecompilePreambleAction(ASTUnit &Unit) + : Unit(Unit), HasEmittedPreamblePCH(false) {} + + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + StringRef InFile); + bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; } + void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; } + virtual bool shouldEraseOutputFiles() { return !hasEmittedPreamblePCH(); } + + virtual bool hasCodeCompletionSupport() const { return false; } + virtual bool hasASTFileSupport() const { return false; } + virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; } +}; + class PrecompilePreambleConsumer : public PCHGenerator { ASTUnit &Unit; - unsigned &Hash; + unsigned &Hash; std::vector TopLevelDecls; - + PrecompilePreambleAction *Action; + public: - PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP, - StringRef isysroot, raw_ostream *Out) + PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action, + const Preprocessor &PP, StringRef isysroot, + raw_ostream *Out) : PCHGenerator(PP, "", 0, isysroot, Out, /*AllowASTWithErrors=*/true), - Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()) { + Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action) { Hash = 0; } @@ -1004,48 +1025,30 @@ public: for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) Unit.addTopLevelDeclFromPreamble( getWriter().getDeclID(TopLevelDecls[I])); + + Action->setHasEmittedPreamblePCH(); } } }; -class PrecompilePreambleAction : public ASTFrontendAction { - ASTUnit &Unit; - PrecompilePreambleConsumer *PreambleConsumer; - -public: - explicit PrecompilePreambleAction(ASTUnit &Unit) - : Unit(Unit), PreambleConsumer(0) {} - - virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, - StringRef InFile) { - std::string Sysroot; - std::string OutputFile; - raw_ostream *OS = 0; - if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, - OutputFile, - OS)) - return 0; - - if (!CI.getFrontendOpts().RelocatablePCH) - Sysroot.clear(); - - CI.getPreprocessor().addPPCallbacks( - new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue())); - PreambleConsumer = new PrecompilePreambleConsumer(Unit,CI.getPreprocessor(), - Sysroot, OS); - return PreambleConsumer; - } +} - bool hasEmittedPreamblePCH() const { - return PreambleConsumer && PreambleConsumer->hasEmittedPCH(); - } - virtual bool shouldEraseOutputFiles() { return !hasEmittedPreamblePCH(); } +ASTConsumer *PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) { + std::string Sysroot; + std::string OutputFile; + raw_ostream *OS = 0; + if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, + OutputFile, OS)) + return 0; - virtual bool hasCodeCompletionSupport() const { return false; } - virtual bool hasASTFileSupport() const { return false; } - virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; } -}; + if (!CI.getFrontendOpts().RelocatablePCH) + Sysroot.clear(); + CI.getPreprocessor().addPPCallbacks(new MacroDefinitionTrackerPPCallbacks( + Unit.getCurrentTopLevelHashValue())); + return new PrecompilePreambleConsumer(Unit, this, CI.getPreprocessor(), + Sysroot, OS); } static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) { -- 2.40.0