From 114b767590ef2a7410edfc425f612697b154e67a Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Thu, 29 Aug 2019 16:38:36 +0000 Subject: [PATCH] Changed FrontendActionFactory::create to return a std::unique_ptr Subscribers: jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66947 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@370379 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Tooling/Tooling.h | 50 ++++++++++++++++++++++---- lib/Tooling/Tooling.cpp | 15 +++++--- tools/clang-refactor/ClangRefactor.cpp | 4 ++- unittests/Tooling/ExecutionTest.cpp | 4 ++- 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index 83fe43ac59..5df816e671 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -99,9 +99,7 @@ public: 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 @@ bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code, 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 @@ bool runToolOnCodeWithArgs( 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 @@ bool runToolOnCodeWithArgs( 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 @@ public: 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 @@ template 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 @@ inline std::unique_ptr newFrontendActionFactory( 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/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp index b21fc33ac3..8c0d13d243 100644 --- a/lib/Tooling/Tooling.cpp +++ b/lib/Tooling/Tooling.cpp @@ -247,12 +247,15 @@ void addTargetAndModeForProgramName(std::vector &CommandLine, 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 @@ ToolInvocation::ToolInvocation( 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/tools/clang-refactor/ClangRefactor.cpp b/tools/clang-refactor/ClangRefactor.cpp index 2a9d6ffefb..8b44c7fa6e 100644 --- a/tools/clang-refactor/ClangRefactor.cpp +++ b/tools/clang-refactor/ClangRefactor.cpp @@ -461,7 +461,9 @@ public: 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/unittests/Tooling/ExecutionTest.cpp b/unittests/Tooling/ExecutionTest.cpp index 3e1e51e98f..16455fb2fd 100644 --- a/unittests/Tooling/ExecutionTest.cpp +++ b/unittests/Tooling/ExecutionTest.cpp @@ -78,7 +78,9 @@ private: 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; -- 2.40.0