Improve ownership of ASTUnits in libTooling by using std::unique_ptr.

llvm-svn: 207229
This commit is contained in:
David Blaikie 2014-04-25 14:49:37 +00:00
parent c435fb07dd
commit 39808ff901
3 changed files with 12 additions and 14 deletions

View File

@ -39,6 +39,7 @@
#include "clang/Tooling/CompilationDatabase.h" #include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h" #include "llvm/ADT/Twine.h"
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -282,7 +283,7 @@ class ClangTool {
/// \brief Create an AST for each file specified in the command line and /// \brief Create an AST for each file specified in the command line and
/// append them to ASTs. /// 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. /// \brief Returns the file manager used in the tool.
/// ///

View File

@ -371,29 +371,29 @@ int ClangTool::run(ToolAction *Action) {
namespace { namespace {
class ASTBuilderAction : public ToolAction { class ASTBuilderAction : public ToolAction {
std::vector<ASTUnit *> &ASTs; std::vector<std::unique_ptr<ASTUnit>> &ASTs;
public: public:
ASTBuilderAction(std::vector<ASTUnit *> &ASTs) : ASTs(ASTs) {} ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, bool runInvocation(CompilerInvocation *Invocation, FileManager *Files,
DiagnosticConsumer *DiagConsumer) override { DiagnosticConsumer *DiagConsumer) override {
// FIXME: This should use the provided FileManager. // FIXME: This should use the provided FileManager.
ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation( std::unique_ptr<ASTUnit> AST(ASTUnit::LoadFromCompilerInvocation(
Invocation, CompilerInstance::createDiagnostics( Invocation, CompilerInstance::createDiagnostics(
&Invocation->getDiagnosticOpts(), DiagConsumer, &Invocation->getDiagnosticOpts(), DiagConsumer,
/*ShouldOwnClient=*/false)); /*ShouldOwnClient=*/false)));
if (!AST) if (!AST)
return false; return false;
ASTs.push_back(AST); ASTs.push_back(std::move(AST));
return true; return true;
} }
}; };
} }
int ClangTool::buildASTs(std::vector<ASTUnit *> &ASTs) { int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
ASTBuilderAction Action(ASTs); ASTBuilderAction Action(ASTs);
return run(&Action); return run(&Action);
} }
@ -408,7 +408,7 @@ ASTUnit *buildASTFromCodeWithArgs(const Twine &Code,
SmallString<16> FileNameStorage; SmallString<16> FileNameStorage;
StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
std::vector<ASTUnit *> ASTs; std::vector<std::unique_ptr<ASTUnit>> ASTs;
ASTBuilderAction Action(ASTs); ASTBuilderAction Action(ASTs);
ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, 0); ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, 0);
@ -419,7 +419,7 @@ ASTUnit *buildASTFromCodeWithArgs(const Twine &Code,
return 0; return 0;
assert(ASTs.size() == 1); assert(ASTs.size() == 1);
return ASTs[0]; return ASTs[0].release();
} }
} // end namespace tooling } // end namespace tooling

View File

@ -310,11 +310,9 @@ TEST(ClangToolTest, BuildASTs) {
Tool.mapVirtualFile("/a.cc", "void a() {}"); Tool.mapVirtualFile("/a.cc", "void a() {}");
Tool.mapVirtualFile("/b.cc", "void b() {}"); 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(0, Tool.buildASTs(ASTs));
EXPECT_EQ(2u, ASTs.size()); EXPECT_EQ(2u, ASTs.size());
llvm::DeleteContainerPointers(ASTs);
} }
struct TestDiagnosticConsumer : public DiagnosticConsumer { struct TestDiagnosticConsumer : public DiagnosticConsumer {
@ -344,11 +342,10 @@ TEST(ClangToolTest, InjectDiagnosticConsumerInBuildASTs) {
Tool.mapVirtualFile("/a.cc", "int x = undeclared;"); Tool.mapVirtualFile("/a.cc", "int x = undeclared;");
TestDiagnosticConsumer Consumer; TestDiagnosticConsumer Consumer;
Tool.setDiagnosticConsumer(&Consumer); Tool.setDiagnosticConsumer(&Consumer);
std::vector<ASTUnit*> ASTs; std::vector<std::unique_ptr<ASTUnit>> ASTs;
Tool.buildASTs(ASTs); Tool.buildASTs(ASTs);
EXPECT_EQ(1u, ASTs.size()); EXPECT_EQ(1u, ASTs.size());
EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
llvm::DeleteContainerPointers(ASTs);
} }
#endif #endif