[clang] 523cae3 - [libTooling] Add option for `buildAST` to report diagnostics.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 06:13:49 PST 2020


Author: Yitzhak Mandelbaum
Date: 2020-02-18T09:11:51-05:00
New Revision: 523cae324d79d2e532d2be797058a94e8214ec45

URL: https://github.com/llvm/llvm-project/commit/523cae324d79d2e532d2be797058a94e8214ec45
DIFF: https://github.com/llvm/llvm-project/commit/523cae324d79d2e532d2be797058a94e8214ec45.diff

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h
index f759789170d9..ced2405f6e83 100644
--- a/clang/include/clang/Tooling/Tooling.h
+++ b/clang/include/clang/Tooling/Tooling.h
@@ -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 {

diff  --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index be200f2e3560..bee20ebb878d 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -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));

diff  --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index 59c9b4fb3863..0ff66206e06e 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -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"));


        


More information about the cfe-commits mailing list