From: Craig Topper Date: Wed, 10 Sep 2014 04:53:53 +0000 (+0000) Subject: Unique_ptrify PPCallbacks ownership. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c6da4d1d6ce3e453deb29972f8dc3807500bbd1d;p=clang Unique_ptrify PPCallbacks ownership. Unique_ptr creation stil needs to be moved earlier at some of the call sites. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217474 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Lex/PPCallbacks.h b/include/clang/Lex/PPCallbacks.h index 7f1ea34a46..056c58aa33 100644 --- a/include/clang/Lex/PPCallbacks.h +++ b/include/clang/Lex/PPCallbacks.h @@ -322,15 +322,12 @@ public: /// \brief Simple wrapper class for chaining callbacks. class PPChainedCallbacks : public PPCallbacks { virtual void anchor(); - PPCallbacks *First, *Second; + std::unique_ptr First, Second; public: - PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second) - : First(_First), Second(_Second) {} - ~PPChainedCallbacks() { - delete Second; - delete First; - } + PPChainedCallbacks(std::unique_ptr _First, + std::unique_ptr _Second) + : First(std::move(_First)), Second(std::move(_Second)) {} void FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType, diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 8e352b78e1..1f8001eaa4 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -338,7 +338,7 @@ class Preprocessor : public RefCountedBase { /// \brief Actions invoked when some preprocessor activity is /// encountered (e.g. a file is \#included, etc). - PPCallbacks *Callbacks; + std::unique_ptr Callbacks; struct MacroExpandsInfo { Token Tok; @@ -575,11 +575,12 @@ public: /// /// Note that this class takes ownership of any PPCallbacks object given to /// it. - PPCallbacks *getPPCallbacks() const { return Callbacks; } - void addPPCallbacks(PPCallbacks *C) { + PPCallbacks *getPPCallbacks() const { return Callbacks.get(); } + void addPPCallbacks(std::unique_ptr C) { if (Callbacks) - C = new PPChainedCallbacks(C, Callbacks); - Callbacks = C; + C = llvm::make_unique(std::move(C), + std::move(Callbacks)); + Callbacks = std::move(C); } /// \} diff --git a/lib/ARCMigrate/ARCMT.cpp b/lib/ARCMigrate/ARCMT.cpp index 5d5aa93b6d..dddc886a26 100644 --- a/lib/ARCMigrate/ARCMT.cpp +++ b/lib/ARCMigrate/ARCMT.cpp @@ -449,7 +449,7 @@ public: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { CI.getPreprocessor().addPPCallbacks( - new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); + llvm::make_unique(ARCMTMacroLocs)); return llvm::make_unique(); } }; diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index d0dc1d906e..fe2fd6165d 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -189,7 +189,7 @@ std::unique_ptr ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { PPConditionalDirectiveRecord * PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager()); - CompInst->getPreprocessor().addPPCallbacks(PPRec); + CI.getPreprocessor().addPPCallbacks(std::unique_ptr(PPRec)); std::vector> Consumers; Consumers.push_back(WrapperFrontendAction::CreateASTConsumer(CI, InFile)); Consumers.push_back(llvm::make_unique( @@ -1873,7 +1873,7 @@ MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { ObjCMTAction |= FrontendOptions::ObjCMT_Literals | FrontendOptions::ObjCMT_Subscripting; } - CI.getPreprocessor().addPPCallbacks(PPRec); + CI.getPreprocessor().addPPCallbacks(std::unique_ptr(PPRec)); std::vector WhiteList = getWhiteListFilenames(CI.getFrontendOpts().ObjCMTWhiteListPath); return llvm::make_unique( diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp index bc72ee67a8..b9f83f14af 100644 --- a/lib/CodeGen/CodeGenAction.cpp +++ b/lib/CodeGen/CodeGenAction.cpp @@ -646,7 +646,8 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { // Add the preprocessor callback only when the coverage mapping is generated. if (CI.getCodeGenOpts().CoverageMapping) { CoverageInfo = new CoverageSourceInfo; - CI.getPreprocessor().addPPCallbacks(CoverageInfo); + CI.getPreprocessor().addPPCallbacks( + std::unique_ptr(CoverageInfo)); } std::unique_ptr Result(new BackendConsumer( BA, CI.getDiagnostics(), CI.getCodeGenOpts(), CI.getTargetOpts(), diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 38d9aeaaf5..963752a83f 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -887,7 +887,8 @@ public: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override { CI.getPreprocessor().addPPCallbacks( - new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue())); + llvm::make_unique( + Unit.getCurrentTopLevelHashValue())); return llvm::make_unique( Unit, Unit.getCurrentTopLevelHashValue()); } @@ -985,8 +986,9 @@ PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI, if (!CI.getFrontendOpts().RelocatablePCH) Sysroot.clear(); - CI.getPreprocessor().addPPCallbacks(new MacroDefinitionTrackerPPCallbacks( - Unit.getCurrentTopLevelHashValue())); + CI.getPreprocessor().addPPCallbacks( + llvm::make_unique( + Unit.getCurrentTopLevelHashValue())); return llvm::make_unique( Unit, this, CI.getPreprocessor(), Sysroot, OS); } @@ -1827,7 +1829,8 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( if (Persistent && !TrackerAct) { Clang->getPreprocessor().addPPCallbacks( - new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue())); + llvm::make_unique( + AST->getCurrentTopLevelHashValue())); std::vector> Consumers; if (Clang->hasASTConsumer()) Consumers.push_back(Clang->takeASTConsumer()); diff --git a/lib/Frontend/DependencyFile.cpp b/lib/Frontend/DependencyFile.cpp index ce3308659c..22ed38fc12 100644 --- a/lib/Frontend/DependencyFile.cpp +++ b/lib/Frontend/DependencyFile.cpp @@ -121,7 +121,8 @@ bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, DependencyCollector::~DependencyCollector() { } void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { - PP.addPPCallbacks(new DepCollectorPPCallbacks(*this, PP.getSourceManager())); + PP.addPPCallbacks( + llvm::make_unique(*this, PP.getSourceManager())); } void DependencyCollector::attachToASTReader(ASTReader &R) { R.addListener(llvm::make_unique(*this)); @@ -203,7 +204,7 @@ DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor( PP.SetSuppressIncludeNotFoundError(true); DFGImpl *Callback = new DFGImpl(&PP, Opts); - PP.addPPCallbacks(Callback); // PP owns the Callback + PP.addPPCallbacks(std::unique_ptr(Callback)); return new DependencyFileGenerator(Callback); } diff --git a/lib/Frontend/DependencyGraph.cpp b/lib/Frontend/DependencyGraph.cpp index 4a7e227b24..67a977e38b 100644 --- a/lib/Frontend/DependencyGraph.cpp +++ b/lib/Frontend/DependencyGraph.cpp @@ -61,7 +61,8 @@ public: void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot) { - PP.addPPCallbacks(new DependencyGraphCallback(&PP, OutputFile, SysRoot)); + PP.addPPCallbacks(llvm::make_unique(&PP, OutputFile, + SysRoot)); } void DependencyGraphCallback::InclusionDirective(SourceLocation HashLoc, diff --git a/lib/Frontend/HeaderIncludeGen.cpp b/lib/Frontend/HeaderIncludeGen.cpp index 50117f6028..2701194571 100644 --- a/lib/Frontend/HeaderIncludeGen.cpp +++ b/lib/Frontend/HeaderIncludeGen.cpp @@ -69,9 +69,12 @@ void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, } } - PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders, - OutputFile, OwnsOutputFile, - ShowDepth, MSStyle)); + PP.addPPCallbacks(llvm::make_unique(&PP, + ShowAllHeaders, + OutputFile, + OwnsOutputFile, + ShowDepth, + MSStyle)); } void HeaderIncludesCallback::FileChanged(SourceLocation Loc, diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index 4a6f8dbef9..17d7279e3f 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -724,7 +724,7 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, PP.AddPragmaHandler("clang", new UnknownPragmaHandler("#pragma clang", Callbacks)); - PP.addPPCallbacks(Callbacks); + PP.addPPCallbacks(std::unique_ptr(Callbacks)); // After we have configured the preprocessor, enter the main file. PP.EnterMainSourceFile(); diff --git a/lib/Frontend/Rewrite/InclusionRewriter.cpp b/lib/Frontend/Rewrite/InclusionRewriter.cpp index c5362846f9..140055735a 100644 --- a/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -565,7 +565,7 @@ void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, Opts.ShowLineMarkers); Rewrite->detectMainFileEOL(); - PP.addPPCallbacks(Rewrite); + PP.addPPCallbacks(std::unique_ptr(Rewrite)); PP.IgnorePragmas(); // First let the preprocessor process the entire file and call callbacks. diff --git a/lib/Frontend/VerifyDiagnosticConsumer.cpp b/lib/Frontend/VerifyDiagnosticConsumer.cpp index 4c91e6880d..eb5a045ec0 100644 --- a/lib/Frontend/VerifyDiagnosticConsumer.cpp +++ b/lib/Frontend/VerifyDiagnosticConsumer.cpp @@ -84,8 +84,8 @@ void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, const_cast(PP)->addCommentHandler(this); #ifndef NDEBUG // Debug build tracks parsed files. - VerifyFileTracker *V = new VerifyFileTracker(*this, *SrcManager); - const_cast(PP)->addPPCallbacks(V); + const_cast(PP)->addPPCallbacks( + llvm::make_unique(*this, *SrcManager)); #endif } } diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 2c23d68282..091a5de8c6 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -172,8 +172,6 @@ Preprocessor::~Preprocessor() { // Delete the header search info, if we own it. if (OwnsHeaderSearch) delete &HeaderInfo; - - delete Callbacks; } void Preprocessor::Initialize(const TargetInfo &Target) { @@ -853,5 +851,5 @@ void Preprocessor::createPreprocessingRecord() { return; Record = new PreprocessingRecord(getSourceManager()); - addPPCallbacks(Record); + addPPCallbacks(std::unique_ptr(Record)); } diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index 4f99daa447..20f4474a1e 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -425,12 +425,12 @@ public: IndexCtx.setASTContext(CI.getASTContext()); Preprocessor &PP = CI.getPreprocessor(); - PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx)); + PP.addPPCallbacks(llvm::make_unique(PP, IndexCtx)); IndexCtx.setPreprocessor(PP); if (SKData) { auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager()); - PP.addPPCallbacks(PPRec); + PP.addPPCallbacks(std::unique_ptr(PPRec)); SKCtrl = llvm::make_unique(*SKData, *PPRec, PP); } diff --git a/unittests/Basic/SourceManagerTest.cpp b/unittests/Basic/SourceManagerTest.cpp index 8f066ba8b8..1dda54dff1 100644 --- a/unittests/Basic/SourceManagerTest.cpp +++ b/unittests/Basic/SourceManagerTest.cpp @@ -303,7 +303,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { PP.Initialize(*Target); std::vector Macros; - PP.addPPCallbacks(new MacroTracker(Macros)); + PP.addPPCallbacks(llvm::make_unique(Macros)); PP.EnterMainSourceFile(); diff --git a/unittests/Frontend/FrontendActionTest.cpp b/unittests/Frontend/FrontendActionTest.cpp index f9cf06efd3..bdd22bd181 100644 --- a/unittests/Frontend/FrontendActionTest.cpp +++ b/unittests/Frontend/FrontendActionTest.cpp @@ -111,15 +111,15 @@ struct TestPPCallbacks : public PPCallbacks { }; class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction { - TestPPCallbacks *Callbacks; + std::unique_ptr Callbacks; public: - TestPPCallbacksFrontendAction(TestPPCallbacks *C) - : Callbacks(C), SeenEnd(false) {} + TestPPCallbacksFrontendAction(std::unique_ptr C) + : Callbacks(std::move(C)), SeenEnd(false) {} void ExecuteAction() override { Preprocessor &PP = getCompilerInstance().getPreprocessor(); - PP.addPPCallbacks(Callbacks); + PP.addPPCallbacks(std::move(Callbacks)); PP.EnterMainSourceFile(); } void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; } @@ -140,8 +140,8 @@ TEST(PreprocessorFrontendAction, EndSourceFile) { Compiler.setInvocation(Invocation); Compiler.createDiagnostics(); - TestPPCallbacks *Callbacks = new TestPPCallbacks; - TestPPCallbacksFrontendAction TestAction(Callbacks); + std::unique_ptr Callbacks(new TestPPCallbacks); + TestPPCallbacksFrontendAction TestAction(std::move(Callbacks)); ASSERT_FALSE(Callbacks->SeenEnd); ASSERT_FALSE(TestAction.SeenEnd); ASSERT_TRUE(Compiler.ExecuteAction(TestAction)); diff --git a/unittests/Lex/PPCallbacksTest.cpp b/unittests/Lex/PPCallbacksTest.cpp index f020206470..af046c4e35 100644 --- a/unittests/Lex/PPCallbacksTest.cpp +++ b/unittests/Lex/PPCallbacksTest.cpp @@ -176,7 +176,7 @@ protected: /*OwnsHeaderSearch =*/false); PP.Initialize(*Target); InclusionDirectiveCallbacks* Callbacks = new InclusionDirectiveCallbacks; - PP.addPPCallbacks(Callbacks); // Takes ownership. + PP.addPPCallbacks(std::unique_ptr(Callbacks)); // Lex source text. PP.EnterMainSourceFile(); @@ -222,7 +222,7 @@ protected: Sema S(PP, Context, Consumer); Parser P(PP, S, false); PragmaOpenCLExtensionCallbacks* Callbacks = new PragmaOpenCLExtensionCallbacks; - PP.addPPCallbacks(Callbacks); // Takes ownership. + PP.addPPCallbacks(std::unique_ptr(Callbacks)); // Lex source text. PP.EnterMainSourceFile(); diff --git a/unittests/Lex/PPConditionalDirectiveRecordTest.cpp b/unittests/Lex/PPConditionalDirectiveRecordTest.cpp index 033b3545af..946cb88b98 100644 --- a/unittests/Lex/PPConditionalDirectiveRecordTest.cpp +++ b/unittests/Lex/PPConditionalDirectiveRecordTest.cpp @@ -103,7 +103,7 @@ TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) { PP.Initialize(*Target); PPConditionalDirectiveRecord * PPRec = new PPConditionalDirectiveRecord(SourceMgr); - PP.addPPCallbacks(PPRec); + PP.addPPCallbacks(std::unique_ptr(PPRec)); PP.EnterMainSourceFile(); std::vector toks;