[clang] Add error check for HeuristicResolver (PR #155561)

Mythreya Kuricheti via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 26 23:35:52 PDT 2025


================
@@ -31,6 +36,69 @@ template <typename InputNode>
 using ResolveFnT = std::function<std::vector<const NamedDecl *>(
     const HeuristicResolver *, InputNode)>;
 
+struct DiagsConsumer : DiagnosticConsumer {
+  struct PrettyDiagnostic {
+    unsigned int ID;
+    DiagnosticsEngine::Level DiagLevel;
+    llvm::SmallString<64> Message;
+
+    template <typename Stream>
+    friend Stream &operator<<(Stream &OS, const PrettyDiagnostic &D) {
+      if (D.DiagLevel == DiagnosticsEngine::Level::Ignored)
+        OS << "Ignored: ";
+      if (D.DiagLevel == DiagnosticsEngine::Level::Note)
+        OS << "Note: ";
+      if (D.DiagLevel == DiagnosticsEngine::Level::Remark)
+        OS << "Remark: ";
+      if (D.DiagLevel == DiagnosticsEngine::Level::Warning)
+        OS << "Warning: ";
+      if (D.DiagLevel == DiagnosticsEngine::Level::Error)
+        OS << "Error: ";
+      if (D.DiagLevel == DiagnosticsEngine::Level::Fatal)
+        OS << "Fatal: ";
+      OS << "ID: " << D.ID << " Message: " << D.Message.str().str() << '\n';
+      return OS;
+    }
+  };
+
+  llvm::SmallSet<unsigned int, 16> IgnoredDiagnostics{};
+  llvm::SmallVector<PrettyDiagnostic, 16> GeneratedDiagnostics{};
+
+  void HandleDiagnostic(DiagnosticsEngine::Level Level,
+                        const Diagnostic &Info) override {
+    DiagnosticConsumer::HandleDiagnostic(Level, Info);
+    const auto ID = Info.getID();
+
+    if (Level >= DiagnosticsEngine::Level::Warning &&
+        !IgnoredDiagnostics.contains(ID)) {
+      llvm::SmallString<64> Message;
+
+      Info.FormatDiagnostic(Message);
+      GeneratedDiagnostics.emplace_back(PrettyDiagnostic{ID, Level, Message});
+    }
+  }
+};
+
+std::unique_ptr<ASTUnit>
+buildASTUnit(llvm::StringRef Code, std::vector<std::string> Args,
+             llvm::SmallSet<unsigned int, 16> IgnoredDiagnostics) {
+  auto VFS = llvm::vfs::getRealFileSystem();
+  DiagnosticOptions DiagOpts{};
+  DiagsConsumer DiagConsumer{};
+  DiagConsumer.IgnoredDiagnostics = IgnoredDiagnostics;
+
+  auto TU = tooling::buildASTFromCodeWithArgs(
+      Code, Args, "input.cc", "clang-tool",
+      std::make_shared<PCHContainerOperations>(),
+      tooling::getClangStripDependencyFileAdjuster(),
+      tooling::FileContentMappings(), &DiagConsumer, VFS);
+
+  auto Diags =
+      CompilerInstance::createDiagnostics(*VFS, DiagOpts, &DiagConsumer, false);
+  EXPECT_TRUE(DiagConsumer.GeneratedDiagnostics.size() == 0);
----------------
MythreyaK wrote:

Could probably use a better check, perhaps one that writes out the errors? 

https://github.com/llvm/llvm-project/pull/155561


More information about the cfe-commits mailing list