diff --git a/clang-tools-extra/clang-doc/ClangDoc.cpp b/clang-tools-extra/clang-doc/ClangDoc.cpp --- a/clang-tools-extra/clang-doc/ClangDoc.cpp +++ b/clang-tools-extra/clang-doc/ClangDoc.cpp @@ -29,13 +29,13 @@ class MapperActionFactory : public tooling::FrontendActionFactory { public: MapperActionFactory(ClangDocContext CDCtx) : CDCtx(CDCtx) {} - clang::FrontendAction *create() override; + std::unique_ptr create() override; private: ClangDocContext CDCtx; }; -clang::FrontendAction *MapperActionFactory::create() { +std::unique_ptr MapperActionFactory::create() { class ClangDocAction : public clang::ASTFrontendAction { public: ClangDocAction(ClangDocContext CDCtx) : CDCtx(CDCtx) {} @@ -49,7 +49,7 @@ private: ClangDocContext CDCtx; }; - return new ClangDocAction(CDCtx); + return std::make_unique(CDCtx); } std::unique_ptr diff --git a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h --- a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h +++ b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h @@ -47,8 +47,8 @@ const HeaderMapCollector::RegexHeaderMap *RegexHeaderMap = nullptr) : Reporter(Reporter), RegexHeaderMap(RegexHeaderMap) {} - clang::FrontendAction *create() override { - return new FindAllSymbolsAction(Reporter, RegexHeaderMap); + std::unique_ptr create() override { + return std::make_unique(Reporter, RegexHeaderMap); } private: diff --git a/clang-tools-extra/clang-move/Move.h b/clang-tools-extra/clang-move/Move.h --- a/clang-tools-extra/clang-move/Move.h +++ b/clang-tools-extra/clang-move/Move.h @@ -224,8 +224,8 @@ DeclarationReporter *const Reporter = nullptr) : Context(Context), Reporter(Reporter) {} - clang::FrontendAction *create() override { - return new ClangMoveAction(Context, Reporter); + std::unique_ptr create() override { + return std::make_unique(Context, Reporter); } private: diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -530,7 +530,9 @@ ActionFactory(ClangTidyContext &Context, IntrusiveRefCntPtr BaseFS) : ConsumerFactory(Context, BaseFS) {} - FrontendAction *create() override { return new Action(&ConsumerFactory); } + std::unique_ptr create() override { + return std::make_unique(&ConsumerFactory); + } bool runInvocation(std::shared_ptr Invocation, FileManager *Files, diff --git a/clang-tools-extra/clangd/indexer/IndexerMain.cpp b/clang-tools-extra/clangd/indexer/IndexerMain.cpp --- a/clang-tools-extra/clangd/indexer/IndexerMain.cpp +++ b/clang-tools-extra/clangd/indexer/IndexerMain.cpp @@ -39,37 +39,36 @@ public: IndexActionFactory(IndexFileIn &Result) : Result(Result) {} - clang::FrontendAction *create() override { + std::unique_ptr create() override { SymbolCollector::Options Opts; Opts.CountReferences = true; return createStaticIndexingAction( - Opts, - [&](SymbolSlab S) { - // Merge as we go. - std::lock_guard Lock(SymbolsMu); - for (const auto &Sym : S) { - if (const auto *Existing = Symbols.find(Sym.ID)) - Symbols.insert(mergeSymbol(*Existing, Sym)); - else - Symbols.insert(Sym); - } - }, - [&](RefSlab S) { - std::lock_guard Lock(SymbolsMu); - for (const auto &Sym : S) { - // Deduplication happens during insertion. - for (const auto &Ref : Sym.second) - Refs.insert(Sym.first, Ref); - } - }, - [&](RelationSlab S) { - std::lock_guard Lock(SymbolsMu); - for (const auto &R : S) { - Relations.insert(R); - } - }, - /*IncludeGraphCallback=*/nullptr) - .release(); + Opts, + [&](SymbolSlab S) { + // Merge as we go. + std::lock_guard Lock(SymbolsMu); + for (const auto &Sym : S) { + if (const auto *Existing = Symbols.find(Sym.ID)) + Symbols.insert(mergeSymbol(*Existing, Sym)); + else + Symbols.insert(Sym); + } + }, + [&](RefSlab S) { + std::lock_guard Lock(SymbolsMu); + for (const auto &Sym : S) { + // Deduplication happens during insertion. + for (const auto &Ref : Sym.second) + Refs.insert(Sym.first, Ref); + } + }, + [&](RelationSlab S) { + std::lock_guard Lock(SymbolsMu); + for (const auto &R : S) { + Relations.insert(R); + } + }, + /*IncludeGraphCallback=*/nullptr); } // Awkward: we write the result in the destructor, because the executor diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp --- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp +++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp @@ -200,7 +200,7 @@ CommentHandler *PragmaHandler) : COpts(std::move(COpts)), PragmaHandler(PragmaHandler) {} - clang::FrontendAction *create() override { + std::unique_ptr create() override { class IndexAction : public ASTFrontendAction { public: IndexAction(std::shared_ptr DataConsumer, @@ -232,7 +232,8 @@ index::IndexingOptions::SystemSymbolFilterKind::All; IndexOpts.IndexFunctionLocals = false; Collector = std::make_shared(COpts); - return new IndexAction(Collector, std::move(IndexOpts), PragmaHandler); + return std::make_unique(Collector, std::move(IndexOpts), + PragmaHandler); } std::shared_ptr Collector; diff --git a/clang-tools-extra/modularize/CoverageChecker.cpp b/clang-tools-extra/modularize/CoverageChecker.cpp --- a/clang-tools-extra/modularize/CoverageChecker.cpp +++ b/clang-tools-extra/modularize/CoverageChecker.cpp @@ -58,6 +58,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Driver/Options.h" #include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/FrontendActions.h" #include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Preprocessor.h" @@ -129,8 +130,8 @@ CoverageCheckerFrontendActionFactory(CoverageChecker &Checker) : Checker(Checker) {} - CoverageCheckerAction *create() override { - return new CoverageCheckerAction(Checker); + std::unique_ptr create() override { + return std::make_unique(Checker); } private: diff --git a/clang-tools-extra/modularize/Modularize.cpp b/clang-tools-extra/modularize/Modularize.cpp --- a/clang-tools-extra/modularize/Modularize.cpp +++ b/clang-tools-extra/modularize/Modularize.cpp @@ -233,6 +233,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Driver/Options.h" #include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/FrontendActions.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/CompilationDatabase.h" @@ -721,8 +722,9 @@ : Entities(Entities), PPTracker(preprocessorTracker), HadErrors(HadErrors) {} - CollectEntitiesAction *create() override { - return new CollectEntitiesAction(Entities, PPTracker, HadErrors); + std::unique_ptr create() override { + return std::make_unique(Entities, PPTracker, + HadErrors); } private: @@ -801,8 +803,8 @@ public: CompileCheckFrontendActionFactory() {} - CompileCheckAction *create() override { - return new CompileCheckAction(); + std::unique_ptr create() override { + return std::make_unique(); } }; @@ -886,6 +888,7 @@ CompileCheckTool.appendArgumentsAdjuster( getModularizeArgumentsAdjuster(ModUtil->Dependencies)); int CompileCheckFileErrors = 0; + // FIXME: use newFrontendActionFactory. CompileCheckFrontendActionFactory CompileCheckFactory; CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory); if (CompileCheckFileErrors != 0) { diff --git a/clang-tools-extra/pp-trace/PPTrace.cpp b/clang-tools-extra/pp-trace/PPTrace.cpp --- a/clang-tools-extra/pp-trace/PPTrace.cpp +++ b/clang-tools-extra/pp-trace/PPTrace.cpp @@ -30,6 +30,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Driver/Options.h" #include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/FrontendActions.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/Execution.h" @@ -112,7 +113,9 @@ PPTraceFrontendActionFactory(const FilterType &Filters, raw_ostream &OS) : Filters(Filters), OS(OS) {} - PPTraceAction *create() override { return new PPTraceAction(Filters, OS); } + std::unique_ptr create() override { + return std::make_unique(Filters, OS); + } private: const FilterType &Filters; diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -99,9 +99,7 @@ DiagnosticConsumer *DiagConsumer) override; /// Returns a new clang::FrontendAction. - /// - /// The caller takes ownership of the returned action. - virtual FrontendAction *create() = 0; + virtual std::unique_ptr create() = 0; }; /// Returns a new FrontendActionFactory for a given type. @@ -161,6 +159,14 @@ std::shared_ptr PCHContainerOps = std::make_shared()); +inline bool +runToolOnCode(std::unique_ptr ToolAction, const Twine &Code, + const Twine &FileName = "input.cc", + std::shared_ptr PCHContainerOps = + std::make_shared()) { + return runToolOnCode(ToolAction.release(), Code, FileName, PCHContainerOps); +} + /// The first part of the pair is the filename, the second part the /// file-content. using FileContentMappings = std::vector>; @@ -186,6 +192,17 @@ std::make_shared(), const FileContentMappings &VirtualMappedFiles = FileContentMappings()); +inline bool runToolOnCodeWithArgs( + std::unique_ptr ToolAction, const Twine &Code, + const std::vector &Args, const Twine &FileName = "input.cc", + const Twine &ToolName = "clang-tool", + std::shared_ptr PCHContainerOps = + std::make_shared(), + const FileContentMappings &VirtualMappedFiles = FileContentMappings()) { + return runToolOnCodeWithArgs(ToolAction.release(), Code, Args, FileName, + ToolName, PCHContainerOps, VirtualMappedFiles); +} + // Similar to the overload except this takes a VFS. bool runToolOnCodeWithArgs( FrontendAction *ToolAction, const Twine &Code, @@ -195,6 +212,17 @@ std::shared_ptr PCHContainerOps = std::make_shared()); +inline bool runToolOnCodeWithArgs( + std::unique_ptr ToolAction, const Twine &Code, + llvm::IntrusiveRefCntPtr VFS, + const std::vector &Args, const Twine &FileName = "input.cc", + const Twine &ToolName = "clang-tool", + std::shared_ptr PCHContainerOps = + std::make_shared()) { + return runToolOnCodeWithArgs(ToolAction.release(), Code, VFS, Args, FileName, + ToolName, PCHContainerOps); +} + /// Builds an AST for 'Code'. /// /// \param Code C++ code. @@ -247,6 +275,13 @@ std::shared_ptr PCHContainerOps = std::make_shared()); + ToolInvocation(std::vector CommandLine, + std::unique_ptr FAction, FileManager *Files, + std::shared_ptr PCHContainerOps = + std::make_shared()) + : ToolInvocation(std::move(CommandLine), FAction.release(), Files, + PCHContainerOps) {} + /// Create a tool invocation. /// /// \param CommandLine The command line arguments to clang. @@ -397,7 +432,9 @@ std::unique_ptr newFrontendActionFactory() { class SimpleFrontendActionFactory : public FrontendActionFactory { public: - FrontendAction *create() override { return new T; } + std::unique_ptr create() override { + return std::make_unique(); + } }; return std::unique_ptr( @@ -413,8 +450,9 @@ SourceFileCallbacks *Callbacks) : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} - FrontendAction *create() override { - return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks); + std::unique_ptr create() override { + return std::make_unique(ConsumerFactory, + Callbacks); } private: diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -247,12 +247,15 @@ namespace { class SingleFrontendActionFactory : public FrontendActionFactory { - FrontendAction *Action; + std::unique_ptr Action; public: - SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} + SingleFrontendActionFactory(std::unique_ptr Action) + : Action(std::move(Action)) {} - FrontendAction *create() override { return Action; } + std::unique_ptr create() override { + return std::move(Action); + } }; } // namespace @@ -267,8 +270,10 @@ std::vector CommandLine, FrontendAction *FAction, FileManager *Files, std::shared_ptr PCHContainerOps) : CommandLine(std::move(CommandLine)), - Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), - Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {} + Action(new SingleFrontendActionFactory( + std::unique_ptr(FAction))), + OwnsAction(true), Files(Files), + PCHContainerOps(std::move(PCHContainerOps)) {} ToolInvocation::~ToolInvocation() { if (OwnsAction) diff --git a/clang/tools/clang-refactor/ClangRefactor.cpp b/clang/tools/clang-refactor/ClangRefactor.cpp --- a/clang/tools/clang-refactor/ClangRefactor.cpp +++ b/clang/tools/clang-refactor/ClangRefactor.cpp @@ -461,7 +461,9 @@ ToolActionFactory(TUCallbackType Callback) : Callback(std::move(Callback)) {} - FrontendAction *create() override { return new ToolASTAction(Callback); } + std::unique_ptr create() override { + return std::make_unique(Callback); + } private: TUCallbackType Callback; diff --git a/clang/unittests/Tooling/ExecutionTest.cpp b/clang/unittests/Tooling/ExecutionTest.cpp --- a/clang/unittests/Tooling/ExecutionTest.cpp +++ b/clang/unittests/Tooling/ExecutionTest.cpp @@ -78,7 +78,9 @@ class ReportResultActionFactory : public FrontendActionFactory { public: ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {} - FrontendAction *create() override { return new ReportResultAction(Context); } + std::unique_ptr create() override { + return std::make_unique(Context); + } private: ExecutionContext *const Context;