rupprecht created this revision.
rupprecht requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This should not be committed, but demonstrates an issue with cascading errors 
after D84673 <https://reviews.llvm.org/D84673>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94468

Files:
  clang/unittests/Tooling/ToolingTest.cpp


Index: clang/unittests/Tooling/ToolingTest.cpp
===================================================================
--- clang/unittests/Tooling/ToolingTest.cpp
+++ clang/unittests/Tooling/ToolingTest.cpp
@@ -221,6 +221,54 @@
   EXPECT_TRUE(Invocation.run());
 }
 
+class LogDiagnosticConsumer : public DiagnosticConsumer {
+public:
+  LogDiagnosticConsumer() = default;
+
+  void HandleDiagnostic(clang::DiagnosticsEngine::Level,
+                        const clang::Diagnostic &info) override {
+    llvm::SmallString<40> diagnostic_string;
+
+    info.FormatDiagnostic(diagnostic_string);
+    Diagnostics += diagnostic_string.str();
+
+    // The next expression triggers:
+    //   Assertion `SourceMgr && "SourceManager not set!"' failed.
+    // When commented out, will log the diagnostic as normal, which is an
+    // invalid flag:
+    //   "invalid integral value '-1' in '-ferror-limit -1'"
+    info.getSourceManager();
+  }
+  std::string Diagnostics;
+};
+
+TEST(ToolInvocation, DiagCallback) {
+  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+      new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+      new llvm::vfs::InMemoryFileSystem);
+  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
+  llvm::IntrusiveRefCntPtr<FileManager> Files(
+      new FileManager(FileSystemOptions(), OverlayFileSystem));
+  std::vector<std::string> Args;
+  Args.push_back("tool-executable");
+  // Note: intentional error; user probably meant -ferror-limit=0.
+  Args.push_back("-ferror-limit=-1");
+  Args.push_back("-fsyntax-only");
+  Args.push_back("test.cpp");
+  clang::tooling::ToolInvocation Invocation(
+      Args, std::make_unique<SyntaxOnlyAction>(), Files.get());
+  InMemoryFileSystem->addFile(
+      "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main(){}\n"));
+
+  LogDiagnosticConsumer LDC;
+  Invocation.setDiagnosticConsumer(&LDC);
+
+  EXPECT_TRUE(Invocation.run());
+
+  EXPECT_EQ("", LDC.Diagnostics);
+}
+
 struct VerifyEndCallback : public SourceFileCallbacks {
   VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
   bool handleBeginSource(CompilerInstance &CI) override {


Index: clang/unittests/Tooling/ToolingTest.cpp
===================================================================
--- clang/unittests/Tooling/ToolingTest.cpp
+++ clang/unittests/Tooling/ToolingTest.cpp
@@ -221,6 +221,54 @@
   EXPECT_TRUE(Invocation.run());
 }
 
+class LogDiagnosticConsumer : public DiagnosticConsumer {
+public:
+  LogDiagnosticConsumer() = default;
+
+  void HandleDiagnostic(clang::DiagnosticsEngine::Level,
+                        const clang::Diagnostic &info) override {
+    llvm::SmallString<40> diagnostic_string;
+
+    info.FormatDiagnostic(diagnostic_string);
+    Diagnostics += diagnostic_string.str();
+
+    // The next expression triggers:
+    //   Assertion `SourceMgr && "SourceManager not set!"' failed.
+    // When commented out, will log the diagnostic as normal, which is an
+    // invalid flag:
+    //   "invalid integral value '-1' in '-ferror-limit -1'"
+    info.getSourceManager();
+  }
+  std::string Diagnostics;
+};
+
+TEST(ToolInvocation, DiagCallback) {
+  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+      new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+      new llvm::vfs::InMemoryFileSystem);
+  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
+  llvm::IntrusiveRefCntPtr<FileManager> Files(
+      new FileManager(FileSystemOptions(), OverlayFileSystem));
+  std::vector<std::string> Args;
+  Args.push_back("tool-executable");
+  // Note: intentional error; user probably meant -ferror-limit=0.
+  Args.push_back("-ferror-limit=-1");
+  Args.push_back("-fsyntax-only");
+  Args.push_back("test.cpp");
+  clang::tooling::ToolInvocation Invocation(
+      Args, std::make_unique<SyntaxOnlyAction>(), Files.get());
+  InMemoryFileSystem->addFile(
+      "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main(){}\n"));
+
+  LogDiagnosticConsumer LDC;
+  Invocation.setDiagnosticConsumer(&LDC);
+
+  EXPECT_TRUE(Invocation.run());
+
+  EXPECT_EQ("", LDC.Diagnostics);
+}
+
 struct VerifyEndCallback : public SourceFileCallbacks {
   VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
   bool handleBeginSource(CompilerInstance &CI) override {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to