[PATCH] D94468: [test] Demonstrate bad error message

Jordan Rupprecht via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 11 21:30:27 PST 2021


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 {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94468.315991.patch
Type: text/x-patch
Size: 2257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210112/36b3473f/attachment.bin>


More information about the cfe-commits mailing list