Index: clang-modernize/Core/Transform.h =================================================================== --- clang-modernize/Core/Transform.h +++ clang-modernize/Core/Transform.h @@ -18,6 +18,8 @@ #include "Core/IncludeExcludeInfo.h" #include "Core/Refactoring.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Basic/LLVM.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Registry.h" #include "llvm/Support/Timer.h" @@ -70,6 +72,8 @@ RiskLevel MaxRiskLevel; }; +class ActionFactory; + /// \brief Abstract base class for all C++11 migration transforms. /// /// Subclasses must call createActionFactory() to create a @@ -208,8 +212,7 @@ /// /// The factory returned by this function is responsible for calling back to /// Transform to call handleBeginSource() and handleEndSource(). - clang::tooling::FrontendActionFactory * - createActionFactory(clang::ast_matchers::MatchFinder &Finder); + ActionFactory createActionFactory(clang::ast_matchers::MatchFinder &Finder); private: const std::string Name; @@ -322,4 +325,48 @@ typedef llvm::Registry TransformFactoryRegistry; +/// \brief Custom FrontendActionFactory to produce FrontendActions that simply +/// forward (Begin|End)SourceFileAction calls to a given Transform. +class ActionFactory : public clang::tooling::FrontendActionFactory { +public: + ActionFactory(clang::ast_matchers::MatchFinder &Finder, Transform &Owner) + : Finder(Finder), Owner(Owner) {} + + virtual std::unique_ptr create() const override { + return llvm::make_unique(Finder, Owner); + } + +private: + class FactoryAdaptor : public clang::ASTFrontendAction { + public: + FactoryAdaptor(clang::ast_matchers::MatchFinder &Finder, Transform &Owner) + : Finder(Finder), Owner(Owner) {} + + clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &, + llvm::StringRef) { + return Finder.newASTConsumer(); + } + + virtual bool BeginSourceFileAction(clang::CompilerInstance &CI, + llvm::StringRef Filename) override { + if (!clang::ASTFrontendAction::BeginSourceFileAction(CI, Filename)) + return false; + + return Owner.handleBeginSource(CI, Filename); + } + + virtual void EndSourceFileAction() override { + Owner.handleEndSource(); + return clang::ASTFrontendAction::EndSourceFileAction(); + } + + private: + clang::ast_matchers::MatchFinder &Finder; + Transform &Owner; + }; + + clang::ast_matchers::MatchFinder &Finder; + Transform &Owner; +}; + #endif // CLANG_MODERNIZE_TRANSFORM_H Index: clang-modernize/Core/Transform.cpp =================================================================== --- clang-modernize/Core/Transform.cpp +++ clang-modernize/Core/Transform.cpp @@ -14,7 +14,6 @@ //===----------------------------------------------------------------------===// #include "Core/Transform.h" -#include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceManager.h" #include "clang/Frontend/CompilerInstance.h" @@ -30,48 +29,6 @@ using namespace tooling; using namespace ast_matchers; -/// \brief Custom FrontendActionFactory to produce FrontendActions that simply -/// forward (Begin|End)SourceFileAction calls to a given Transform. -class ActionFactory : public clang::tooling::FrontendActionFactory { -public: - ActionFactory(MatchFinder &Finder, Transform &Owner) - : Finder(Finder), Owner(Owner) {} - - virtual FrontendAction *create() override { - return new FactoryAdaptor(Finder, Owner); - } - -private: - class FactoryAdaptor : public ASTFrontendAction { - public: - FactoryAdaptor(MatchFinder &Finder, Transform &Owner) - : Finder(Finder), Owner(Owner) {} - - ASTConsumer *CreateASTConsumer(CompilerInstance &, StringRef) { - return Finder.newASTConsumer(); - } - - virtual bool BeginSourceFileAction(CompilerInstance &CI, - StringRef Filename) override { - if (!ASTFrontendAction::BeginSourceFileAction(CI, Filename)) - return false; - - return Owner.handleBeginSource(CI, Filename); - } - - virtual void EndSourceFileAction() override { - Owner.handleEndSource(); - return ASTFrontendAction::EndSourceFileAction(); - } - - private: - MatchFinder &Finder; - Transform &Owner; - }; - - MatchFinder &Finder; - Transform &Owner; -}; } // namespace Transform::Transform(llvm::StringRef Name, const TransformOptions &Options) @@ -126,8 +83,8 @@ return true; } -FrontendActionFactory *Transform::createActionFactory(MatchFinder &Finder) { - return new ActionFactory(Finder, /*Owner=*/ *this); +ActionFactory Transform::createActionFactory(MatchFinder &Finder) { + return ActionFactory(Finder, /*Owner=*/ *this); } Version Version::getFromString(llvm::StringRef VersionStr) { Index: clang-modernize/UseNullptr/UseNullptr.cpp =================================================================== --- clang-modernize/UseNullptr/UseNullptr.cpp +++ clang-modernize/UseNullptr/UseNullptr.cpp @@ -47,7 +47,7 @@ Finder.addMatcher(makeCastSequenceMatcher(), &Fixer); - if (int result = UseNullptrTool.run(createActionFactory(Finder))) { + if (int result = UseNullptrTool.run(ActionFactory(Finder, *this))) { llvm::errs() << "Error encountered during translation.\n"; return result; } Index: clang-modernize/tool/ClangModernize.cpp =================================================================== --- clang-modernize/tool/ClangModernize.cpp +++ clang-modernize/tool/ClangModernize.cpp @@ -469,7 +469,7 @@ if (FinalSyntaxCheck) { ClangTool SyntaxTool(*Compilations, SourcePaths); - if (SyntaxTool.run(newFrontendActionFactory().get()) != 0) + if (SyntaxTool.run(newFrontendActionFactory()) != 0) return 1; } Index: clang-tidy/ClangTidy.cpp =================================================================== --- clang-tidy/ClangTidy.cpp +++ clang-tidy/ClangTidy.cpp @@ -324,7 +324,9 @@ public: ActionFactory(ClangTidyASTConsumerFactory *ConsumerFactory) : ConsumerFactory(ConsumerFactory) {} - FrontendAction *create() override { return new Action(ConsumerFactory); } + std::unique_ptr create() const override { + return llvm::make_unique(ConsumerFactory); + } private: class Action : public ASTFrontendAction { @@ -342,7 +344,7 @@ ClangTidyASTConsumerFactory *ConsumerFactory; }; - Tool.run(new ActionFactory(new ClangTidyASTConsumerFactory(Context))); + Tool.run(ActionFactory(new ClangTidyASTConsumerFactory(Context))); *Errors = Context.getErrors(); return Context.getStats(); } Index: modularize/Modularize.cpp =================================================================== --- modularize/Modularize.cpp +++ modularize/Modularize.cpp @@ -672,8 +672,9 @@ : Entities(Entities), PPTracker(preprocessorTracker), HadErrors(HadErrors) {} - virtual CollectEntitiesAction *create() { - return new CollectEntitiesAction(Entities, PPTracker, HadErrors); + virtual std::unique_ptr create() const { + return llvm::make_unique(Entities, PPTracker, + HadErrors); } private: @@ -729,7 +730,8 @@ new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments)); // Create preprocessor tracker, to watch for macro and conditional problems. - std::unique_ptr PPTracker(PreprocessorTracker::create()); + std::unique_ptr PPTracker = + PreprocessorTracker::create(); // Parse all of the headers, detecting duplicates. EntityMap Entities; @@ -737,7 +739,7 @@ Tool.appendArgumentsAdjuster(new AddDependenciesAdjuster(Dependencies)); int HadErrors = 0; HadErrors |= Tool.run( - new ModularizeFrontendActionFactory(Entities, *PPTracker, HadErrors)); + ModularizeFrontendActionFactory(Entities, *PPTracker, HadErrors)); // Create a place to save duplicate entity locations, separate bins per kind. typedef SmallVector LocationArray; @@ -770,7 +772,8 @@ for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end(); DI != DE; ++DI, ++KindIndex) { int ECount = DI->size(); - // If only 1 occurrence of this entity, skip it, as we only report duplicates. + // If only 1 occurrence of this entity, skip it, as we only report + // duplicates. if (ECount <= 1) continue; LocationArray::iterator FI = DI->begin(); Index: modularize/PreprocessorTracker.h =================================================================== --- modularize/PreprocessorTracker.h +++ modularize/PreprocessorTracker.h @@ -77,7 +77,7 @@ virtual bool reportInconsistentConditionals(llvm::raw_ostream &OS) = 0; // Create instance of PreprocessorTracker. - static PreprocessorTracker *create(); + static std::unique_ptr create(); }; } // end namespace Modularize Index: modularize/PreprocessorTracker.cpp =================================================================== --- modularize/PreprocessorTracker.cpp +++ modularize/PreprocessorTracker.cpp @@ -248,6 +248,7 @@ #include "PreprocessorTracker.h" #include "clang/Lex/MacroArgs.h" #include "clang/Lex/PPCallbacks.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallSet.h" #include "llvm/Support/StringPool.h" #include "llvm/Support/raw_ostream.h" @@ -1313,8 +1314,8 @@ PreprocessorTracker::~PreprocessorTracker() {} // Create instance of PreprocessorTracker. -PreprocessorTracker *PreprocessorTracker::create() { - return new PreprocessorTrackerImpl(); +std::unique_ptr PreprocessorTracker::create() { + return llvm::make_unique(); } // Preprocessor callbacks for modularize. Index: module-map-checker/ModuleMapChecker.cpp =================================================================== --- module-map-checker/ModuleMapChecker.cpp +++ module-map-checker/ModuleMapChecker.cpp @@ -192,8 +192,8 @@ ModuleMapCheckerFrontendActionFactory(ModuleMapChecker &Checker) : Checker(Checker) {} - virtual ModuleMapCheckerAction *create() { - return new ModuleMapCheckerAction(Checker); + virtual std::unique_ptr create() const { + return llvm::make_unique(Checker); } private: @@ -403,7 +403,7 @@ // Create the tool and run the compilation. ClangTool Tool(*Compilations, HeaderPath); - int HadErrors = Tool.run(new ModuleMapCheckerFrontendActionFactory(*this)); + int HadErrors = Tool.run(ModuleMapCheckerFrontendActionFactory(*this)); // If we had errors, exit early. return HadErrors ? false : true; Index: pp-trace/PPTrace.cpp =================================================================== --- pp-trace/PPTrace.cpp +++ pp-trace/PPTrace.cpp @@ -136,8 +136,8 @@ std::vector &CallbackCalls) : Ignore(Ignore), CallbackCalls(CallbackCalls) {} - virtual PPTraceAction *create() { - return new PPTraceAction(Ignore, CallbackCalls); + virtual std::unique_ptr create() const { + return llvm::make_unique(Ignore, CallbackCalls); } private: @@ -199,8 +199,7 @@ // Create the tool and run the compilation. ClangTool Tool(*Compilations, SourcePaths); - int HadErrors = - Tool.run(new PPTraceFrontendActionFactory(Ignore, CallbackCalls)); + int HadErrors = Tool.run(PPTraceFrontendActionFactory(Ignore, CallbackCalls)); // If we had errors, exit early. if (HadErrors) Index: remove-cstr-calls/RemoveCStrCalls.cpp =================================================================== --- remove-cstr-calls/RemoveCStrCalls.cpp +++ remove-cstr-calls/RemoveCStrCalls.cpp @@ -233,5 +233,5 @@ callee(methodDecl(hasName(StringCStrMethod))), on(id("arg", expr())))))), &Callback); - return Tool.runAndSave(newFrontendActionFactory(&Finder).get()); + return Tool.runAndSave(newFrontendActionFactory(&Finder)); } Index: tool-template/ToolTemplate.cpp =================================================================== --- tool-template/ToolTemplate.cpp +++ tool-template/ToolTemplate.cpp @@ -103,5 +103,5 @@ // Use Finder.addMatcher(...) to define the patterns in the AST that you // want to match against. You are not limited to just one matcher! - return Tool.run(newFrontendActionFactory(&Finder).get()); + return Tool.run(newFrontendActionFactory(&Finder)); } Index: unittests/clang-modernize/IncludeDirectivesTest.cpp =================================================================== --- unittests/clang-modernize/IncludeDirectivesTest.cpp +++ unittests/clang-modernize/IncludeDirectivesTest.cpp @@ -19,7 +19,8 @@ /// \brief A convenience method around \c tooling::runToolOnCodeWithArgs() that /// adds the current directory to the include search paths. -static void applyActionOnCode(FrontendAction *ToolAction, StringRef Code) { +static void applyActionOnCode(std::unique_ptr ToolAction, + StringRef Code) { SmallString<128> CurrentDir; ASSERT_FALSE(llvm::sys::fs::current_path(CurrentDir)); @@ -33,8 +34,8 @@ SmallString<128> InputFile(CurrentDir); sys::path::append(InputFile, "input.cc"); - ASSERT_TRUE( - tooling::runToolOnCodeWithArgs(ToolAction, Code, Args, InputFile.str())); + ASSERT_TRUE(tooling::runToolOnCodeWithArgs(std::move(ToolAction), Code, Args, + InputFile.str())); } namespace { @@ -115,7 +116,8 @@ std::string addIncludeInCode(StringRef Include, StringRef Code) { tooling::Replacements Replaces; - applyActionOnCode(new TestAddIncludeAction(Include, Replaces), Code); + applyActionOnCode(llvm::make_unique(Include, Replaces), + Code); if (::testing::Test::HasFailure()) return "<>"; @@ -236,11 +238,12 @@ } namespace { -TestAddIncludeAction *makeIndirectTestsAction(const char *HeaderToModify, - tooling::Replacements &Replaces) { +std::unique_ptr +makeIndirectTestsAction(const char *HeaderToModify, + tooling::Replacements &Replaces) { StringRef IncludeToAdd = "c.h"; - TestAddIncludeAction *TestAction = - new TestAddIncludeAction(IncludeToAdd, Replaces, HeaderToModify); + auto TestAction = llvm::make_unique( + IncludeToAdd, Replaces, HeaderToModify); TestAction->mapVirtualHeader("c.h", "#pragma once\n"); TestAction->mapVirtualHeader("a.h", "#pragma once\n" "#include \n"); @@ -261,16 +264,16 @@ // a.h already includes c.h { - FrontendAction *Action = makeIndirectTestsAction("a.h", Replaces); - ASSERT_NO_FATAL_FAILURE(applyActionOnCode(Action, Code)); + auto Action = makeIndirectTestsAction("a.h", Replaces); + ASSERT_NO_FATAL_FAILURE(applyActionOnCode(std::move(Action), Code)); EXPECT_EQ(unsigned(0), Replaces.size()); } // c.h is included before b.h but b.h doesn't include c.h directly, so check // that it will be inserted. { - FrontendAction *Action = makeIndirectTestsAction("b.h", Replaces); - ASSERT_NO_FATAL_FAILURE(applyActionOnCode(Action, Code)); + auto Action = makeIndirectTestsAction("b.h", Replaces); + ASSERT_NO_FATAL_FAILURE(applyActionOnCode(std::move(Action), Code)); EXPECT_EQ("#include \n\n\n", tooling::applyAllReplacements("\n", Replaces)); } @@ -281,11 +284,11 @@ StringRef GuardedHeaderCode) { const char *GuardedHeaderName = "guarded.h"; tooling::Replacements Replaces; - TestAddIncludeAction *TestAction = - new TestAddIncludeAction(IncludeToAdd, Replaces, GuardedHeaderName); + auto TestAction = llvm::make_unique( + IncludeToAdd, Replaces, GuardedHeaderName); TestAction->mapVirtualHeader(GuardedHeaderName, GuardedHeaderCode); - applyActionOnCode(TestAction, "#include \n"); + applyActionOnCode(std::move(TestAction), "#include \n"); if (::testing::Test::HasFailure()) return "<>"; Index: unittests/clang-modernize/TransformTest.cpp =================================================================== --- unittests/clang-modernize/TransformTest.cpp +++ unittests/clang-modernize/TransformTest.cpp @@ -153,8 +153,7 @@ // handleEndSource() calls to it. CallbackForwarder Callbacks(T); - Tool.run( - clang::tooling::newFrontendActionFactory(&Factory, &Callbacks).get()); + Tool.run(clang::tooling::newFrontendActionFactory(&Factory, &Callbacks)); EXPECT_TRUE(Factory.Called); Transform::TimingVec::const_iterator I = T.timing_begin(); @@ -272,7 +271,7 @@ DummyTransform T("dummy", Options); MatchFinder Finder; Finder.addMatcher(varDecl().bind("decl"), new ModifiableCallback(T)); - Tool.run(tooling::newFrontendActionFactory(&Finder).get()); + Tool.run(tooling::newFrontendActionFactory(&Finder)); } TEST(VersionTest, Interface) { Index: unittests/clang-tidy/ClangTidyTest.h =================================================================== --- unittests/clang-tidy/ClangTidyTest.h +++ unittests/clang-tidy/ClangTidyTest.h @@ -49,14 +49,13 @@ Check.setContext(&Context); std::vector ArgCXX11(1, "-std=c++11"); - if (!tooling::runToolOnCodeWithArgs(new TestPPAction(Check, &Context), Code, - ArgCXX11)) + if (!tooling::runToolOnCodeWithArgs( + llvm::make_unique(Check, &Context), Code, ArgCXX11)) return ""; ast_matchers::MatchFinder Finder; Check.registerMatchers(&Finder); - std::unique_ptr Factory( - tooling::newFrontendActionFactory(&Finder)); - if (!tooling::runToolOnCodeWithArgs(Factory->create(), Code, ArgCXX11)) + auto Factory = tooling::newFrontendActionFactory(&Finder); + if (!tooling::runToolOnCodeWithArgs(Factory.create(), Code, ArgCXX11)) return ""; DiagConsumer.finish(); tooling::Replacements Fixes;