]> granicus.if.org Git - clang/commitdiff
Improve ownership of ASTUnits in libTooling by using std::unique_ptr.
authorDavid Blaikie <dblaikie@gmail.com>
Fri, 25 Apr 2014 14:49:37 +0000 (14:49 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Fri, 25 Apr 2014 14:49:37 +0000 (14:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@207229 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Tooling.h
lib/Tooling/Tooling.cpp
unittests/Tooling/ToolingTest.cpp

index 097a7a8a0bdadb4e9f077bd3955fdc32e3979c68..46c988b7e7096f5efe87b4bf02834ec831a93f3a 100644 (file)
@@ -39,6 +39,7 @@
 #include "clang/Tooling/CompilationDatabase.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/Twine.h"
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -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<ASTUnit *> &ASTs);
+  int buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs);
 
   /// \brief Returns the file manager used in the tool.
   ///
index 25ade338e1af3769ce44bd121a11a4e6272ac587..9bd85ee357af25663002d30f813dbc7c01f6fdbd 100644 (file)
@@ -371,29 +371,29 @@ int ClangTool::run(ToolAction *Action) {
 namespace {
 
 class ASTBuilderAction : public ToolAction {
-  std::vector<ASTUnit *> &ASTs;
+  std::vector<std::unique_ptr<ASTUnit>> &ASTs;
 
 public:
-  ASTBuilderAction(std::vector<ASTUnit *> &ASTs) : ASTs(ASTs) {}
+  ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &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<ASTUnit> 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<ASTUnit *> &ASTs) {
+int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &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<ASTUnit *> ASTs;
+  std::vector<std::unique_ptr<ASTUnit>> 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
index 641e99634ec7e47c2a7ecc7275160b0370366b42..606eda15df0acb2fa19b91bc561c40281dcc320f 100644 (file)
@@ -310,11 +310,9 @@ TEST(ClangToolTest, BuildASTs) {
   Tool.mapVirtualFile("/a.cc", "void a() {}");
   Tool.mapVirtualFile("/b.cc", "void b() {}");
 
-  std::vector<ASTUnit *> ASTs;
+  std::vector<std::unique_ptr<ASTUnit>> 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<ASTUnit*> ASTs;
+  std::vector<std::unique_ptr<ASTUnit>> ASTs;
   Tool.buildASTs(ASTs);
   EXPECT_EQ(1u, ASTs.size());
   EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
-  llvm::DeleteContainerPointers(ASTs);
 }
 #endif