Index: docs/LibTooling.rst =================================================================== --- docs/LibTooling.rst +++ docs/LibTooling.rst @@ -34,7 +34,7 @@ TEST(runToolOnCode, CanSyntaxCheckCode) { // runToolOnCode returns whether the action was correctly run over the // given code. - EXPECT_TRUE(runToolOnCode(new clang::SyntaxOnlyAction, "class X {};")); + EXPECT_TRUE(runToolOnCode(llvm::make_unique(), "class X {};")); } Writing a standalone tool Index: docs/RAVFrontendAction.rst =================================================================== --- docs/RAVFrontendAction.rst +++ docs/RAVFrontendAction.rst @@ -196,7 +196,7 @@ int main(int argc, char **argv) { if (argc > 1) { - clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]); + clang::tooling::runToolOnCode(llvm::make_unique(), argv[1]); } } Index: include/clang/Tooling/Tooling.h =================================================================== --- include/clang/Tooling/Tooling.h +++ include/clang/Tooling/Tooling.h @@ -94,7 +94,7 @@ /// \brief Returns a new clang::FrontendAction. /// /// The caller takes ownership of the returned action. - virtual clang::FrontendAction *create() = 0; + virtual std::unique_ptr create() = 0; }; /// \brief Returns a new FrontendActionFactory for a given type. @@ -149,8 +149,8 @@ /// clang modules. /// /// \return - True if 'ToolAction' was successfully executed. -bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, - const Twine &FileName = "input.cc", +bool runToolOnCode(std::unique_ptr ToolAction, + const Twine &Code, const Twine &FileName = "input.cc", std::shared_ptr PCHContainerOps = std::make_shared()); @@ -172,7 +172,7 @@ /// /// \return - True if 'ToolAction' was successfully executed. bool runToolOnCodeWithArgs( - clang::FrontendAction *ToolAction, const Twine &Code, + 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 = @@ -226,8 +226,8 @@ /// ownership. /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. - ToolInvocation(std::vector CommandLine, FrontendAction *FAction, - FileManager *Files, + ToolInvocation(std::vector CommandLine, + std::unique_ptr FAction, FileManager *Files, std::shared_ptr PCHContainerOps = std::make_shared()); @@ -367,7 +367,9 @@ std::unique_ptr newFrontendActionFactory() { class SimpleFrontendActionFactory : public FrontendActionFactory { public: - clang::FrontendAction *create() override { return new T; } + std::unique_ptr create() override { + return llvm::make_unique(); + } }; return std::unique_ptr( @@ -383,8 +385,9 @@ SourceFileCallbacks *Callbacks) : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} - clang::FrontendAction *create() override { - return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks); + std::unique_ptr create() override { + return llvm::make_unique(ConsumerFactory, + Callbacks); } private: Index: lib/Tooling/Tooling.cpp =================================================================== --- lib/Tooling/Tooling.cpp +++ lib/Tooling/Tooling.cpp @@ -104,12 +104,12 @@ return Invocation; } -bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, - const Twine &FileName, +bool runToolOnCode(std::unique_ptr ToolAction, + const Twine &Code, const Twine &FileName, std::shared_ptr PCHContainerOps) { - return runToolOnCodeWithArgs(ToolAction, Code, std::vector(), - FileName, "clang-tool", - std::move(PCHContainerOps)); + return runToolOnCodeWithArgs(std::move(ToolAction), Code, + std::vector(), FileName, + "clang-tool", std::move(PCHContainerOps)); } static std::vector @@ -125,7 +125,7 @@ } bool runToolOnCodeWithArgs( - clang::FrontendAction *ToolAction, const Twine &Code, + std::unique_ptr ToolAction, const Twine &Code, const std::vector &Args, const Twine &FileName, const Twine &ToolName, std::shared_ptr PCHContainerOps, @@ -143,8 +143,7 @@ ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(); ToolInvocation Invocation( getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), - ToolAction, Files.get(), - std::move(PCHContainerOps)); + std::move(ToolAction), Files.get(), std::move(PCHContainerOps)); SmallString<1024> CodeStorage; InMemoryFileSystem->addFile(FileNameRef, 0, @@ -204,15 +203,18 @@ 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 ToolInvocation::ToolInvocation( std::vector CommandLine, ToolAction *Action, @@ -222,12 +224,13 @@ DiagConsumer(nullptr) {} ToolInvocation::ToolInvocation( - std::vector CommandLine, FrontendAction *FAction, - FileManager *Files, std::shared_ptr PCHContainerOps) + std::vector CommandLine, + std::unique_ptr FAction, FileManager *Files, + std::shared_ptr PCHContainerOps) : CommandLine(std::move(CommandLine)), - Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), - Files(Files), PCHContainerOps(std::move(PCHContainerOps)), - DiagConsumer(nullptr) {} + Action(new SingleFrontendActionFactory(std::move(FAction))), + OwnsAction(true), Files(Files), + PCHContainerOps(std::move(PCHContainerOps)), DiagConsumer(nullptr) {} ToolInvocation::~ToolInvocation() { if (OwnsAction) Index: tools/clang-refactor/ClangRefactor.cpp =================================================================== --- tools/clang-refactor/ClangRefactor.cpp +++ tools/clang-refactor/ClangRefactor.cpp @@ -462,7 +462,9 @@ ToolActionFactory(TUCallbackType Callback) : Callback(std::move(Callback)) {} - FrontendAction *create() override { return new ToolASTAction(Callback); } + std::unique_ptr create() override { + return llvm::make_unique(Callback); + } private: TUCallbackType Callback; Index: unittests/AST/EvaluateAsRValueTest.cpp =================================================================== --- unittests/AST/EvaluateAsRValueTest.cpp +++ unittests/AST/EvaluateAsRValueTest.cpp @@ -90,7 +90,7 @@ std::vector Args(1, Mode); Args.push_back("-fno-delayed-template-parsing"); ASSERT_TRUE(runToolOnCodeWithArgs( - new EvaluateConstantInitializersAction(), + llvm::make_unique(), "template " "struct vector {" " explicit vector(int size);" Index: unittests/CrossTU/CrossTranslationUnitTest.cpp =================================================================== --- unittests/CrossTU/CrossTranslationUnitTest.cpp +++ unittests/CrossTU/CrossTranslationUnitTest.cpp @@ -103,7 +103,8 @@ TEST(CrossTranslationUnit, CanLoadFunctionDefinition) { bool Success = false; - EXPECT_TRUE(tooling::runToolOnCode(new CTUAction(&Success), "int f(int);")); + EXPECT_TRUE(tooling::runToolOnCode(llvm::make_unique(&Success), + "int f(int);")); EXPECT_TRUE(Success); } Index: unittests/Sema/CodeCompleteTest.cpp =================================================================== --- unittests/Sema/CodeCompleteTest.cpp +++ unittests/Sema/CodeCompleteTest.cpp @@ -100,7 +100,7 @@ auto Action = llvm::make_unique( offsetToPosition(WithoutToken, TokenOffset), Results); - clang::tooling::runToolOnCodeWithArgs(Action.release(), Code, {"-std=c++11"}, + clang::tooling::runToolOnCodeWithArgs(std::move(Action), Code, {"-std=c++11"}, TestCCName); return Results; } Index: unittests/Sema/ExternalSemaSourceTest.cpp =================================================================== --- unittests/Sema/ExternalSemaSourceTest.cpp +++ unittests/Sema/ExternalSemaSourceTest.cpp @@ -221,28 +221,26 @@ // Make sure that the DiagnosticWatcher is not miscounting. TEST(ExternalSemaSource, SanityCheck) { - std::unique_ptr Installer( - new ExternalSemaSourceInstaller); + auto Installer = llvm::make_unique(); DiagnosticWatcher Watcher("AAB", "BBB"); Installer->PushWatcher(&Watcher); std::vector Args(1, "-std=c++11"); ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( - Installer.release(), "namespace AAA { } using namespace AAB;", Args)); + std::move(Installer), "namespace AAA { } using namespace AAB;", Args)); ASSERT_EQ(0, Watcher.SeenCount); } // Check that when we add a NamespaceTypeProvider, we use that suggestion // instead of the usual suggestion we would use above. TEST(ExternalSemaSource, ExternalTypoCorrectionPrioritized) { - std::unique_ptr Installer( - new ExternalSemaSourceInstaller); + auto Installer = llvm::make_unique(); NamespaceTypoProvider Provider("AAB", "BBB"); DiagnosticWatcher Watcher("AAB", "BBB"); Installer->PushSource(&Provider); Installer->PushWatcher(&Watcher); std::vector Args(1, "-std=c++11"); ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( - Installer.release(), "namespace AAA { } using namespace AAB;", Args)); + std::move(Installer), "namespace AAA { } using namespace AAB;", Args)); ASSERT_LE(0, Provider.CallCount); ASSERT_EQ(1, Watcher.SeenCount); } @@ -250,8 +248,7 @@ // Check that we use the first successful TypoCorrection returned from an // ExternalSemaSource. TEST(ExternalSemaSource, ExternalTypoCorrectionOrdering) { - std::unique_ptr Installer( - new ExternalSemaSourceInstaller); + auto Installer = llvm::make_unique(); NamespaceTypoProvider First("XXX", "BBB"); NamespaceTypoProvider Second("AAB", "CCC"); NamespaceTypoProvider Third("AAB", "DDD"); @@ -262,7 +259,7 @@ Installer->PushWatcher(&Watcher); std::vector Args(1, "-std=c++11"); ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( - Installer.release(), "namespace AAA { } using namespace AAB;", Args)); + std::move(Installer), "namespace AAA { } using namespace AAB;", Args)); ASSERT_LE(1, First.CallCount); ASSERT_LE(1, Second.CallCount); ASSERT_EQ(0, Third.CallCount); @@ -270,15 +267,14 @@ } TEST(ExternalSemaSource, ExternalDelayedTypoCorrection) { - std::unique_ptr Installer( - new ExternalSemaSourceInstaller); + auto Installer = llvm::make_unique(); FunctionTypoProvider Provider("aaa", "bbb"); DiagnosticWatcher Watcher("aaa", "bbb"); Installer->PushSource(&Provider); Installer->PushWatcher(&Watcher); std::vector Args(1, "-std=c++11"); ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( - Installer.release(), "namespace AAA { } void foo() { AAA::aaa(); }", + std::move(Installer), "namespace AAA { } void foo() { AAA::aaa(); }", Args)); ASSERT_LE(0, Provider.CallCount); ASSERT_EQ(1, Watcher.SeenCount); @@ -287,15 +283,14 @@ // We should only try MaybeDiagnoseMissingCompleteType if we can't otherwise // solve the problem. TEST(ExternalSemaSource, TryOtherTacticsBeforeDiagnosing) { - std::unique_ptr Installer( - new ExternalSemaSourceInstaller); + auto Installer = llvm::make_unique(); CompleteTypeDiagnoser Diagnoser(false); Installer->PushSource(&Diagnoser); std::vector Args(1, "-std=c++11"); // This code hits the class template specialization/class member of a class // template specialization checks in Sema::RequireCompleteTypeImpl. ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs( - Installer.release(), + std::move(Installer), "template struct S { class C { }; }; S::C SCInst;", Args)); ASSERT_EQ(0, Diagnoser.CallCount); @@ -304,8 +299,7 @@ // The first ExternalSemaSource where MaybeDiagnoseMissingCompleteType returns // true should be the last one called. TEST(ExternalSemaSource, FirstDiagnoserTaken) { - std::unique_ptr Installer( - new ExternalSemaSourceInstaller); + auto Installer = llvm::make_unique(); CompleteTypeDiagnoser First(false); CompleteTypeDiagnoser Second(true); CompleteTypeDiagnoser Third(true); @@ -314,7 +308,7 @@ Installer->PushSource(&Third); std::vector Args(1, "-std=c++11"); ASSERT_FALSE(clang::tooling::runToolOnCodeWithArgs( - Installer.release(), "class Incomplete; Incomplete IncompleteInstance;", + std::move(Installer), "class Incomplete; Incomplete IncompleteInstance;", Args)); ASSERT_EQ(1, First.CallCount); ASSERT_EQ(1, Second.CallCount); Index: unittests/Tooling/CommentHandlerTest.cpp =================================================================== --- unittests/Tooling/CommentHandlerTest.cpp +++ unittests/Tooling/CommentHandlerTest.cpp @@ -56,8 +56,8 @@ CommentVerifier GetVerifier(); protected: - ASTFrontendAction *CreateTestAction() override { - return new CommentHandlerAction(this); + std::unique_ptr CreateTestAction() override { + return llvm::make_unique(this); } private: Index: unittests/Tooling/ExecutionTest.cpp =================================================================== --- unittests/Tooling/ExecutionTest.cpp +++ unittests/Tooling/ExecutionTest.cpp @@ -79,7 +79,9 @@ class ReportResultActionFactory : public FrontendActionFactory { public: ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {} - FrontendAction *create() override { return new ReportResultAction(Context); } + std::unique_ptr create() override { + return llvm::make_unique(Context); + } private: ExecutionContext *const Context; Index: unittests/Tooling/RefactoringTest.cpp =================================================================== --- unittests/Tooling/RefactoringTest.cpp +++ unittests/Tooling/RefactoringTest.cpp @@ -650,7 +650,7 @@ class TestVisitor : public clang::RecursiveASTVisitor { public: bool runOver(StringRef Code) { - return runToolOnCode(new TestAction(this), Code); + return runToolOnCode(llvm::make_unique(this), Code); } protected: Index: unittests/Tooling/TestVisitor.h =================================================================== --- unittests/Tooling/TestVisitor.h +++ unittests/Tooling/TestVisitor.h @@ -82,8 +82,8 @@ } protected: - virtual ASTFrontendAction* CreateTestAction() { - return new TestAction(this); + virtual std::unique_ptr CreateTestAction() { + return llvm::make_unique(this); } class FindConsumer : public ASTConsumer { Index: unittests/Tooling/ToolingTest.cpp =================================================================== --- unittests/Tooling/ToolingTest.cpp +++ unittests/Tooling/ToolingTest.cpp @@ -64,10 +64,10 @@ TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) { bool FoundTopLevelDecl = false; - EXPECT_TRUE( - runToolOnCode(new TestAction(llvm::make_unique( - &FoundTopLevelDecl)), - "")); + EXPECT_TRUE(runToolOnCode( + llvm::make_unique( + llvm::make_unique(&FoundTopLevelDecl)), + "")); EXPECT_FALSE(FoundTopLevelDecl); } @@ -104,17 +104,17 @@ TEST(runToolOnCode, FindsClassDecl) { bool FoundClassDeclX = false; - EXPECT_TRUE( - runToolOnCode(new TestAction(llvm::make_unique( - &FoundClassDeclX)), - "class X;")); + EXPECT_TRUE(runToolOnCode( + llvm::make_unique( + llvm::make_unique(&FoundClassDeclX)), + "class X;")); EXPECT_TRUE(FoundClassDeclX); FoundClassDeclX = false; - EXPECT_TRUE( - runToolOnCode(new TestAction(llvm::make_unique( - &FoundClassDeclX)), - "class Y;")); + EXPECT_TRUE(runToolOnCode( + llvm::make_unique( + llvm::make_unique(&FoundClassDeclX)), + "class Y;")); EXPECT_FALSE(FoundClassDeclX); } @@ -162,8 +162,8 @@ Args.push_back("-Idef"); Args.push_back("-fsyntax-only"); Args.push_back("test.cpp"); - clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, - Files.get()); + clang::tooling::ToolInvocation Invocation( + Args, llvm::make_unique(), Files.get()); InMemoryFileSystem->addFile( "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include \n")); InMemoryFileSystem->addFile("def/abc", 0, @@ -188,8 +188,8 @@ Args.push_back("-Idef"); Args.push_back("-fsyntax-only"); Args.push_back("test.cpp"); - clang::tooling::ToolInvocation Invocation(Args, new SyntaxOnlyAction, - Files.get()); + clang::tooling::ToolInvocation Invocation( + Args, llvm::make_unique(), Files.get()); InMemoryFileSystem->addFile( "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include \n")); InMemoryFileSystem->addFile("def/abc", 0, @@ -259,61 +259,64 @@ std::vector Args = {"-std=c++11"}; std::vector Args2 = {"-fno-delayed-template-parsing"}; - EXPECT_TRUE(runToolOnCode(new SkipBodyAction, + EXPECT_TRUE(runToolOnCode(llvm::make_unique(), "int skipMe() { an_error_here }")); - EXPECT_FALSE(runToolOnCode(new SkipBodyAction, + EXPECT_FALSE(runToolOnCode(llvm::make_unique(), "int skipMeNot() { an_error_here }")); // Test constructors with initializers EXPECT_TRUE(runToolOnCodeWithArgs( - new SkipBodyAction, + llvm::make_unique(), "struct skipMe { skipMe() : an_error() { more error } };", Args)); EXPECT_TRUE(runToolOnCodeWithArgs( - new SkipBodyAction, "struct skipMe { skipMe(); };" - "skipMe::skipMe() : an_error([](){;}) { more error }", + llvm::make_unique(), + "struct skipMe { skipMe(); };" + "skipMe::skipMe() : an_error([](){;}) { more error }", Args)); EXPECT_TRUE(runToolOnCodeWithArgs( - new SkipBodyAction, "struct skipMe { skipMe(); };" - "skipMe::skipMe() : an_error{[](){;}} { more error }", + llvm::make_unique(), + "struct skipMe { skipMe(); };" + "skipMe::skipMe() : an_error{[](){;}} { more error }", Args)); EXPECT_TRUE(runToolOnCodeWithArgs( - new SkipBodyAction, + llvm::make_unique(), "struct skipMe { skipMe(); };" "skipMe::skipMe() : a(e)>>(), f{}, g() { error }", Args)); EXPECT_TRUE(runToolOnCodeWithArgs( - new SkipBodyAction, "struct skipMe { skipMe() : bases()... { error } };", - Args)); + llvm::make_unique(), + "struct skipMe { skipMe() : bases()... { error } };", Args)); EXPECT_FALSE(runToolOnCodeWithArgs( - new SkipBodyAction, "struct skipMeNot { skipMeNot() : an_error() { } };", - Args)); - EXPECT_FALSE(runToolOnCodeWithArgs(new SkipBodyAction, + llvm::make_unique(), + "struct skipMeNot { skipMeNot() : an_error() { } };", Args)); + EXPECT_FALSE(runToolOnCodeWithArgs(llvm::make_unique(), "struct skipMeNot { skipMeNot(); };" "skipMeNot::skipMeNot() : an_error() { }", Args)); // Try/catch EXPECT_TRUE(runToolOnCode( - new SkipBodyAction, + llvm::make_unique(), "void skipMe() try { an_error() } catch(error) { error };")); EXPECT_TRUE(runToolOnCode( - new SkipBodyAction, + llvm::make_unique(), "struct S { void skipMe() try { an_error() } catch(error) { error } };")); EXPECT_TRUE( - runToolOnCode(new SkipBodyAction, + runToolOnCode(llvm::make_unique(), "void skipMe() try { an_error() } catch(error) { error; }" "catch(error) { error } catch (error) { }")); EXPECT_FALSE(runToolOnCode( - new SkipBodyAction, + llvm::make_unique(), "void skipMe() try something;")); // don't crash while parsing // Template - EXPECT_TRUE(runToolOnCode( - new SkipBodyAction, "template int skipMe() { an_error_here }" - "int x = skipMe();")); + EXPECT_TRUE( + runToolOnCode(llvm::make_unique(), + "template int skipMe() { an_error_here }" + "int x = skipMe();")); EXPECT_FALSE(runToolOnCodeWithArgs( - new SkipBodyAction, + llvm::make_unique(), "template int skipMeNot() { an_error_here }", Args2)); } @@ -327,7 +330,8 @@ Args.push_back(DepFilePath.str()); Args.push_back("-MF"); Args.push_back(DepFilePath.str()); - EXPECT_TRUE(runToolOnCodeWithArgs(new SkipBodyAction, "", Args)); + EXPECT_TRUE( + runToolOnCodeWithArgs(llvm::make_unique(), "", Args)); EXPECT_FALSE(llvm::sys::fs::exists(DepFilePath.str())); EXPECT_FALSE(llvm::sys::fs::remove(DepFilePath.str())); } @@ -351,23 +355,26 @@ TEST(runToolOnCodeWithArgs, DiagnosticsColor) { - EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "", - {"-fcolor-diagnostics"})); - EXPECT_TRUE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), - "", {"-fno-color-diagnostics"})); - EXPECT_TRUE( - runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(true), "", - {"-fno-color-diagnostics", "-fcolor-diagnostics"})); - EXPECT_TRUE( - runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), "", - {"-fcolor-diagnostics", "-fno-color-diagnostics"})); EXPECT_TRUE(runToolOnCodeWithArgs( - new CheckColoredDiagnosticsAction(true), "", + llvm::make_unique(true), "", + {"-fcolor-diagnostics"})); + EXPECT_TRUE(runToolOnCodeWithArgs( + llvm::make_unique(false), "", + {"-fno-color-diagnostics"})); + EXPECT_TRUE(runToolOnCodeWithArgs( + llvm::make_unique(true), "", + {"-fno-color-diagnostics", "-fcolor-diagnostics"})); + EXPECT_TRUE(runToolOnCodeWithArgs( + llvm::make_unique(false), "", + {"-fcolor-diagnostics", "-fno-color-diagnostics"})); + EXPECT_TRUE(runToolOnCodeWithArgs( + llvm::make_unique(true), "", {"-fno-color-diagnostics", "-fdiagnostics-color=always"})); // Check that this test would fail if ShowColors is not what it should. - EXPECT_FALSE(runToolOnCodeWithArgs(new CheckColoredDiagnosticsAction(false), - "", {"-fcolor-diagnostics"})); + EXPECT_FALSE(runToolOnCodeWithArgs( + llvm::make_unique(false), "", + {"-fcolor-diagnostics"})); } TEST(ClangToolTest, ArgumentAdjusters) { @@ -603,7 +610,7 @@ // Should not crash EXPECT_FALSE( - runToolOnCode(new ResetDiagnosticAction, + runToolOnCode(llvm::make_unique(), "struct Foo { Foo(int); ~Foo(); struct Fwd _fwd; };" "void func() { long x; Foo f(x); }")); }