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 "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.
///

View 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

View 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