From 6beb6aa8f0f6fa20e8c8de7a17f39b101ed0da59 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Sun, 10 Aug 2014 19:56:51 +0000 Subject: Recommit 213307: unique_ptr-ify ownership of ASTConsumers (reverted in r213325) After post-commit review and community discussion, this seems like a reasonable direction to continue, making ownership semantics explicit in the source using the type system. llvm-svn: 215323 --- clang/unittests/Tooling/ToolingTest.cpp | 50 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'clang/unittests/Tooling/ToolingTest.cpp') diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp index 9aede044f69..85ab942387b 100644 --- a/clang/unittests/Tooling/ToolingTest.cpp +++ b/clang/unittests/Tooling/ToolingTest.cpp @@ -28,20 +28,20 @@ namespace { /// Takes an ast consumer and returns it from CreateASTConsumer. This only /// works with single translation unit compilations. class TestAction : public clang::ASTFrontendAction { - public: +public: /// Takes ownership of TestConsumer. - explicit TestAction(clang::ASTConsumer *TestConsumer) - : TestConsumer(TestConsumer) {} + explicit TestAction(std::unique_ptr TestConsumer) + : TestConsumer(std::move(TestConsumer)) {} - protected: - virtual clang::ASTConsumer* CreateASTConsumer( - clang::CompilerInstance& compiler, StringRef dummy) { +protected: + virtual std::unique_ptr + CreateASTConsumer(clang::CompilerInstance &compiler, StringRef dummy) { /// TestConsumer will be deleted by the framework calling us. - return TestConsumer; + return std::move(TestConsumer); } - private: - clang::ASTConsumer * const TestConsumer; +private: + std::unique_ptr TestConsumer; }; class FindTopLevelDeclConsumer : public clang::ASTConsumer { @@ -59,8 +59,10 @@ class FindTopLevelDeclConsumer : public clang::ASTConsumer { TEST(runToolOnCode, FindsNoTopLevelDeclOnEmptyCode) { bool FoundTopLevelDecl = false; - EXPECT_TRUE(runToolOnCode( - new TestAction(new FindTopLevelDeclConsumer(&FoundTopLevelDecl)), "")); + EXPECT_TRUE( + runToolOnCode(new TestAction(llvm::make_unique( + &FoundTopLevelDecl)), + "")); EXPECT_FALSE(FoundTopLevelDecl); } @@ -97,13 +99,17 @@ bool FindClassDeclX(ASTUnit *AST) { TEST(runToolOnCode, FindsClassDecl) { bool FoundClassDeclX = false; - EXPECT_TRUE(runToolOnCode(new TestAction( - new FindClassDeclXConsumer(&FoundClassDeclX)), "class X;")); + EXPECT_TRUE( + runToolOnCode(new TestAction(llvm::make_unique( + &FoundClassDeclX)), + "class X;")); EXPECT_TRUE(FoundClassDeclX); FoundClassDeclX = false; - EXPECT_TRUE(runToolOnCode(new TestAction( - new FindClassDeclXConsumer(&FoundClassDeclX)), "class Y;")); + EXPECT_TRUE( + runToolOnCode(new TestAction(llvm::make_unique( + &FoundClassDeclX)), + "class Y;")); EXPECT_FALSE(FoundClassDeclX); } @@ -125,8 +131,8 @@ TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) { } struct IndependentFrontendActionCreator { - ASTConsumer *newASTConsumer() { - return new FindTopLevelDeclConsumer(nullptr); + std::unique_ptr newASTConsumer() { + return llvm::make_unique(nullptr); } }; @@ -185,8 +191,8 @@ struct VerifyEndCallback : public SourceFileCallbacks { virtual void handleEndSource() { ++EndCalled; } - ASTConsumer *newASTConsumer() { - return new FindTopLevelDeclConsumer(&Matched); + std::unique_ptr newASTConsumer() { + return llvm::make_unique(&Matched); } unsigned BeginCalled; unsigned EndCalled; @@ -225,10 +231,10 @@ struct SkipBodyConsumer : public clang::ASTConsumer { }; struct SkipBodyAction : public clang::ASTFrontendAction { - virtual ASTConsumer *CreateASTConsumer(CompilerInstance &Compiler, - StringRef) { + virtual std::unique_ptr + CreateASTConsumer(CompilerInstance &Compiler, StringRef) { Compiler.getFrontendOpts().SkipFunctionBodies = true; - return new SkipBodyConsumer; + return llvm::make_unique(); } }; -- cgit v1.2.3