From 39808ff9015d8ebe252c7f6e7741595a5416d404 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Fri, 25 Apr 2014 14:49:37 +0000 Subject: [PATCH] Improve ownership of ASTUnits in libTooling by using std::unique_ptr. llvm-svn: 207229 --- clang/include/clang/Tooling/Tooling.h | 3 ++- clang/lib/Tooling/Tooling.cpp | 16 ++++++++-------- clang/unittests/Tooling/ToolingTest.cpp | 7 ++----- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h index 097a7a8a0bda..46c988b7e709 100644 --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -39,6 +39,7 @@ #include "clang/Tooling/CompilationDatabase.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" +#include #include #include @@ -282,7 +283,7 @@ class ClangTool { /// \brief Create an AST for each file specified in the command line and /// append them to ASTs. - int buildASTs(std::vector &ASTs); + int buildASTs(std::vector> &ASTs); /// \brief Returns the file manager used in the tool. /// diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 25ade338e1af..9bd85ee357af 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -371,29 +371,29 @@ int ClangTool::run(ToolAction *Action) { namespace { class ASTBuilderAction : public ToolAction { - std::vector &ASTs; + std::vector> &ASTs; public: - ASTBuilderAction(std::vector &ASTs) : ASTs(ASTs) {} + ASTBuilderAction(std::vector> &ASTs) : ASTs(ASTs) {} bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, DiagnosticConsumer *DiagConsumer) override { // FIXME: This should use the provided FileManager. - ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation( + std::unique_ptr AST(ASTUnit::LoadFromCompilerInvocation( Invocation, CompilerInstance::createDiagnostics( &Invocation->getDiagnosticOpts(), DiagConsumer, - /*ShouldOwnClient=*/false)); + /*ShouldOwnClient=*/false))); if (!AST) return false; - ASTs.push_back(AST); + ASTs.push_back(std::move(AST)); return true; } }; } -int ClangTool::buildASTs(std::vector &ASTs) { +int ClangTool::buildASTs(std::vector> &ASTs) { ASTBuilderAction Action(ASTs); return run(&Action); } @@ -408,7 +408,7 @@ ASTUnit *buildASTFromCodeWithArgs(const Twine &Code, SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); - std::vector ASTs; + std::vector> ASTs; ASTBuilderAction Action(ASTs); ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, 0); @@ -419,7 +419,7 @@ ASTUnit *buildASTFromCodeWithArgs(const Twine &Code, return 0; assert(ASTs.size() == 1); - return ASTs[0]; + return ASTs[0].release(); } } // end namespace tooling diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp index 641e99634ec7..606eda15df0a 100644 --- a/clang/unittests/Tooling/ToolingTest.cpp +++ b/clang/unittests/Tooling/ToolingTest.cpp @@ -310,11 +310,9 @@ TEST(ClangToolTest, BuildASTs) { Tool.mapVirtualFile("/a.cc", "void a() {}"); Tool.mapVirtualFile("/b.cc", "void b() {}"); - std::vector ASTs; + std::vector> ASTs; EXPECT_EQ(0, Tool.buildASTs(ASTs)); EXPECT_EQ(2u, ASTs.size()); - - llvm::DeleteContainerPointers(ASTs); } struct TestDiagnosticConsumer : public DiagnosticConsumer { @@ -344,11 +342,10 @@ TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) { Tool.mapVirtualFile("/a.cc", "int x = undeclared;"); TestDiagnosticConsumer Consumer; Tool.setDiagnosticConsumer(&Consumer); - std::vector ASTs; + std::vector> ASTs; Tool.buildASTs(ASTs); EXPECT_EQ(1u, ASTs.size()); EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); - llvm::DeleteContainerPointers(ASTs); } #endif