https://github.com/orgads created 
https://github.com/llvm/llvm-project/pull/244599

A header that is not found is a fatal error. It takes effect at the next 
diagnostic: from then on all diagnostics are dropped, including the other 
missing headers, and Sema instantiates no more templates. Every use that needs 
an instantiation fails for the rest of the file, e.g. a static member of a 
class whose base is a class template, with errors that hide the missing 
headers, and include-cleaner then reports the headers of those uses as unused.

Treat fatal errors as errors in the preamble and in the main file, as libclang 
does with CXTranslationUnit_KeepGoing.

Assisted-by: Claude Code

>From 6064aa3e405d818225e0ee94db3746b59050ff50 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <[email protected]>
Date: Fri, 20 Nov 2026 12:13:00 +0200
Subject: [PATCH] [clangd] Keep going after a fatal error

A header that is not found is a fatal error. It takes effect at the next
diagnostic: from then on all diagnostics are dropped, including the
other missing headers, and Sema instantiates no more templates. Every
use that needs an instantiation fails for the rest of the file, e.g. a
static member of a class whose base is a class template, with errors
that hide the missing headers, and include-cleaner then reports the
headers of those uses as unused.

Treat fatal errors as errors in the preamble and in the main file, as
libclang does with CXTranslationUnit_KeepGoing.

Assisted-by: Claude Code
---
 clang-tools-extra/clangd/Compiler.cpp         |  4 ++
 clang-tools-extra/clangd/Preamble.cpp         |  3 ++
 .../clangd/unittests/DiagnosticsTests.cpp     | 37 +++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/clang-tools-extra/clangd/Compiler.cpp 
b/clang-tools-extra/clangd/Compiler.cpp
index aaeaab30b96dbc..983e49b66d908c 100644
--- a/clang-tools-extra/clangd/Compiler.cpp
+++ b/clang-tools-extra/clangd/Compiler.cpp
@@ -151,6 +151,10 @@ 
prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
   auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
   Clang->createVirtualFileSystem(VFS, &DiagsClient);
   Clang->createDiagnostics(&DiagsClient, false);
+  // A fatal error, e.g. a header that is not found, would stop template
+  // instantiation for the rest of the file, and fail every use that needs
+  // one. Keep going, as libclang does with CXTranslationUnit_KeepGoing.
+  Clang->getDiagnostics().setFatalsAsError(true);
   Clang->createFileManager();
   if (!Clang->createTarget())
     return nullptr;
diff --git a/clang-tools-extra/clangd/Preamble.cpp 
b/clang-tools-extra/clangd/Preamble.cpp
index 7e68a721a9fca5..7017f9938c4103 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -610,6 +610,9 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
       CompilerInstance::createDiagnostics(*VFS, CI.getDiagnosticOpts(),
                                           &PreambleDiagnostics,
                                           /*ShouldOwnClient=*/false);
+  // Keep going after a fatal error, as the main file does (see
+  // prepareCompilerInstance).
+  PreambleDiagsEngine->setFatalsAsError(true);
   PreambleDiagnostics.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
                                            const clang::Diagnostic &Info) {
     switch (Info.getID()) {
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp 
b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 982854c1e5c186..84a63477185a55 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -268,6 +268,43 @@ TEST(DiagnosticsTest, DiagnosticPreamble) {
                   diagSource(Diag::Clang), diagName("pp_file_not_found"))));
 }
 
+TEST(DiagnosticsTest, KeepGoingAfterMissingHeader) {
+  // A missing header is a fatal error, which takes effect at the next
+  // diagnostic. All the missing headers are still reported, and templates are
+  // still instantiated: here a base class, in a header of the preamble.
+  Annotations Preamble(R"cpp(
+    #include $first[["not-found.h"]] // error-ok
+    #include $second[["not-found-either.h"]] // error-ok
+    #include "singleton.h"
+    int X = Foo::instance();
+  )cpp");
+  TestTU TU = TestTU::withCode(Preamble.code());
+  TU.AdditionalFiles["singleton.h"] = R"cpp(
+    template <typename T> struct Singleton { static int instance(); };
+    struct Foo : Singleton<Foo> {};
+  )cpp";
+  EXPECT_THAT(
+      TU.build().getDiagnostics(),
+      ElementsAre(Diag(Preamble.range("first"), "'not-found.h' file not 
found"),
+                  Diag(Preamble.range("second"),
+                       "'not-found-either.h' file not found")));
+
+  // The same in the main file, after the preamble.
+  Annotations Main(R"cpp(
+    int Before;
+    #include $first[["not-found.h"]] // error-ok
+    #include $second[["not-found-either.h"]] // error-ok
+    template <typename T> struct S { static int f(); };
+    int X = S<int>::f();
+  )cpp");
+  TU = TestTU::withCode(Main.code());
+  EXPECT_THAT(
+      TU.build().getDiagnostics(),
+      ElementsAre(
+          Diag(Main.range("first"), "'not-found.h' file not found"),
+          Diag(Main.range("second"), "'not-found-either.h' file not found")));
+}
+
 TEST(DiagnosticsTest, DeduplicatedClangTidyDiagnostics) {
   Annotations Test(R"cpp(
     float foo = [[0.1f]];

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to