[PATCH] D74763: [libTooling] Add option for `buildAST` to report diagnostics.

Yitzhak Mandelbaum via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 06:08:10 PST 2020


ymandel created this revision.
ymandel added a reviewer: gribozavr.
Herald added a project: clang.

Currently, `buildAST[WithArgs]` either succeeds or fails.  This patch adds
support for the caller to pass a `DiagnosticConsumer` to receive all relevant
diagnostics.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74763

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


Index: clang/unittests/Tooling/ToolingTest.cpp
===================================================================
--- clang/unittests/Tooling/ToolingTest.cpp
+++ clang/unittests/Tooling/ToolingTest.cpp
@@ -101,6 +101,15 @@
   }
   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 @@
   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 @@
   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"));
Index: clang/lib/Tooling/Tooling.cpp
===================================================================
--- clang/lib/Tooling/Tooling.cpp
+++ clang/lib/Tooling/Tooling.cpp
@@ -619,7 +619,8 @@
 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 @@
   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));
Index: clang/include/clang/Tooling/Tooling.h
===================================================================
--- clang/include/clang/Tooling/Tooling.h
+++ clang/include/clang/Tooling/Tooling.h
@@ -225,7 +225,8 @@
     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 {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74763.245145.patch
Type: text/x-patch
Size: 3652 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200218/4d43a639/attachment.bin>


More information about the cfe-commits mailing list