Author: gribozavr Date: Thu Aug 29 09:38:36 2019 New Revision: 370379 URL: http://llvm.org/viewvc/llvm-project?rev=370379&view=rev Log: Changed FrontendActionFactory::create to return a std::unique_ptr
Subscribers: jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66947 Modified: cfe/trunk/include/clang/Tooling/Tooling.h cfe/trunk/lib/Tooling/Tooling.cpp cfe/trunk/tools/clang-refactor/ClangRefactor.cpp cfe/trunk/unittests/Tooling/ExecutionTest.cpp Modified: cfe/trunk/include/clang/Tooling/Tooling.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=370379&r1=370378&r2=370379&view=diff ============================================================================== --- cfe/trunk/include/clang/Tooling/Tooling.h (original) +++ cfe/trunk/include/clang/Tooling/Tooling.h Thu Aug 29 09:38:36 2019 @@ -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<FrontendAction> create() = 0; }; /// Returns a new FrontendActionFactory for a given type. @@ -161,6 +159,14 @@ bool runToolOnCode(FrontendAction *ToolA std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>()); +inline bool +runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, + const Twine &FileName = "input.cc", + std::shared_ptr<PCHContainerOperations> PCHContainerOps = + std::make_shared<PCHContainerOperations>()) { + 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<std::pair<std::string, std::string>>; @@ -186,6 +192,17 @@ bool runToolOnCodeWithArgs( std::make_shared<PCHContainerOperations>(), const FileContentMappings &VirtualMappedFiles = FileContentMappings()); +inline bool runToolOnCodeWithArgs( + std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, + const std::vector<std::string> &Args, const Twine &FileName = "input.cc", + const Twine &ToolName = "clang-tool", + std::shared_ptr<PCHContainerOperations> PCHContainerOps = + std::make_shared<PCHContainerOperations>(), + 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<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>()); +inline bool runToolOnCodeWithArgs( + std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, + llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, + const std::vector<std::string> &Args, const Twine &FileName = "input.cc", + const Twine &ToolName = "clang-tool", + std::shared_ptr<PCHContainerOperations> PCHContainerOps = + std::make_shared<PCHContainerOperations>()) { + 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<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>()); + ToolInvocation(std::vector<std::string> CommandLine, + std::unique_ptr<FrontendAction> FAction, FileManager *Files, + std::shared_ptr<PCHContainerOperations> PCHContainerOps = + std::make_shared<PCHContainerOperations>()) + : 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 <typename T> std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() { class SimpleFrontendActionFactory : public FrontendActionFactory { public: - FrontendAction *create() override { return new T; } + std::unique_ptr<FrontendAction> create() override { + return std::make_unique<T>(); + } }; return std::unique_ptr<FrontendActionFactory>( @@ -413,8 +450,9 @@ inline std::unique_ptr<FrontendActionFac SourceFileCallbacks *Callbacks) : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} - FrontendAction *create() override { - return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks); + std::unique_ptr<FrontendAction> create() override { + return std::make_unique<ConsumerFactoryAdaptor>(ConsumerFactory, + Callbacks); } private: Modified: cfe/trunk/lib/Tooling/Tooling.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?rev=370379&r1=370378&r2=370379&view=diff ============================================================================== --- cfe/trunk/lib/Tooling/Tooling.cpp (original) +++ cfe/trunk/lib/Tooling/Tooling.cpp Thu Aug 29 09:38:36 2019 @@ -247,12 +247,15 @@ void addTargetAndModeForProgramName(std: namespace { class SingleFrontendActionFactory : public FrontendActionFactory { - FrontendAction *Action; + std::unique_ptr<FrontendAction> Action; public: - SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} + SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action) + : Action(std::move(Action)) {} - FrontendAction *create() override { return Action; } + std::unique_ptr<FrontendAction> create() override { + return std::move(Action); + } }; } // namespace @@ -267,8 +270,10 @@ ToolInvocation::ToolInvocation( std::vector<std::string> CommandLine, FrontendAction *FAction, FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : CommandLine(std::move(CommandLine)), - Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), - Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {} + Action(new SingleFrontendActionFactory( + std::unique_ptr<FrontendAction>(FAction))), + OwnsAction(true), Files(Files), + PCHContainerOps(std::move(PCHContainerOps)) {} ToolInvocation::~ToolInvocation() { if (OwnsAction) Modified: cfe/trunk/tools/clang-refactor/ClangRefactor.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-refactor/ClangRefactor.cpp?rev=370379&r1=370378&r2=370379&view=diff ============================================================================== --- cfe/trunk/tools/clang-refactor/ClangRefactor.cpp (original) +++ cfe/trunk/tools/clang-refactor/ClangRefactor.cpp Thu Aug 29 09:38:36 2019 @@ -461,7 +461,9 @@ public: ToolActionFactory(TUCallbackType Callback) : Callback(std::move(Callback)) {} - FrontendAction *create() override { return new ToolASTAction(Callback); } + std::unique_ptr<FrontendAction> create() override { + return std::make_unique<ToolASTAction>(Callback); + } private: TUCallbackType Callback; Modified: cfe/trunk/unittests/Tooling/ExecutionTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/ExecutionTest.cpp?rev=370379&r1=370378&r2=370379&view=diff ============================================================================== --- cfe/trunk/unittests/Tooling/ExecutionTest.cpp (original) +++ cfe/trunk/unittests/Tooling/ExecutionTest.cpp Thu Aug 29 09:38:36 2019 @@ -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<FrontendAction> create() override { + return std::make_unique<ReportResultAction>(Context); + } private: ExecutionContext *const Context; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits