forked from OSchip/llvm-project
[libTooling] Add option for `buildAST` to report diagnostics.
Summary: Currently, `buildAST[WithArgs]` either succeeds or fails. This patch adds support for the caller to pass a `DiagnosticConsumer` to receive all relevant diagnostics. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D74763
This commit is contained in:
parent
1cff2aa512
commit
523cae324d
|
@ -225,7 +225,8 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
|
|||
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
|
||||
std::make_shared<PCHContainerOperations>(),
|
||||
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(),
|
||||
const FileContentMappings &VirtualMappedFiles = FileContentMappings());
|
||||
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
|
||||
DiagnosticConsumer *DiagConsumer = nullptr);
|
||||
|
||||
/// Utility to run a FrontendAction in a single clang invocation.
|
||||
class ToolInvocation {
|
||||
|
|
|
@ -619,7 +619,8 @@ buildASTFromCode(StringRef Code, StringRef FileName,
|
|||
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
|
||||
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
|
||||
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
||||
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles) {
|
||||
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
|
||||
DiagnosticConsumer *DiagConsumer) {
|
||||
std::vector<std::unique_ptr<ASTUnit>> ASTs;
|
||||
ASTBuilderAction Action(ASTs);
|
||||
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
|
||||
|
@ -633,6 +634,7 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
|
|||
ToolInvocation Invocation(
|
||||
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName),
|
||||
&Action, Files.get(), std::move(PCHContainerOps));
|
||||
Invocation.setDiagnosticConsumer(DiagConsumer);
|
||||
|
||||
InMemoryFileSystem->addFile(FileName, 0,
|
||||
llvm::MemoryBuffer::getMemBufferCopy(Code));
|
||||
|
|
|
@ -101,6 +101,15 @@ bool FindClassDeclX(ASTUnit *AST) {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
struct TestDiagnosticConsumer : public DiagnosticConsumer {
|
||||
TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {}
|
||||
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
||||
const Diagnostic &Info) override {
|
||||
++NumDiagnosticsSeen;
|
||||
}
|
||||
unsigned NumDiagnosticsSeen;
|
||||
};
|
||||
} // end namespace
|
||||
|
||||
TEST(runToolOnCode, FindsClassDecl) {
|
||||
|
@ -129,6 +138,16 @@ TEST(buildASTFromCode, FindsClassDecl) {
|
|||
EXPECT_FALSE(FindClassDeclX(AST.get()));
|
||||
}
|
||||
|
||||
TEST(buildASTFromCode, ReportsErrors) {
|
||||
TestDiagnosticConsumer Consumer;
|
||||
std::unique_ptr<ASTUnit> AST = buildASTFromCodeWithArgs(
|
||||
"int x = \"A\";", {}, "input.cc", "clang-tool",
|
||||
std::make_shared<PCHContainerOperations>(),
|
||||
getClangStripDependencyFileAdjuster(), FileContentMappings(), &Consumer);
|
||||
EXPECT_TRUE(AST.get());
|
||||
EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
|
||||
}
|
||||
|
||||
TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
|
||||
std::unique_ptr<FrontendActionFactory> Factory(
|
||||
newFrontendActionFactory<SyntaxOnlyAction>());
|
||||
|
@ -639,15 +658,6 @@ TEST(ClangToolTest, BuildASTs) {
|
|||
EXPECT_EQ(2u, ASTs.size());
|
||||
}
|
||||
|
||||
struct TestDiagnosticConsumer : public DiagnosticConsumer {
|
||||
TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {}
|
||||
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
||||
const Diagnostic &Info) override {
|
||||
++NumDiagnosticsSeen;
|
||||
}
|
||||
unsigned NumDiagnosticsSeen;
|
||||
};
|
||||
|
||||
TEST(ClangToolTest, InjectDiagnosticConsumer) {
|
||||
FixedCompilationDatabase Compilations("/", std::vector<std::string>());
|
||||
ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
|
||||
|
|
Loading…
Reference in New Issue