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

When a header is not found, the declarations that depend on it are invalid or 
missing, so uses of other headers may not resolve to them. Include-cleaner then 
reports those headers as unused, and its fix removes includes that the file 
needs.

Don't report unused includes in a file that has a header not found.

Assisted-by: Claude Code

>From 77210cc95c3e13c78244195b3da8f7e5ae637621 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <[email protected]>
Date: Fri, 20 Nov 2026 12:21:25 +0200
Subject: [PATCH] [clangd] Report no unused includes behind a missing header

When a header is not found, the declarations that depend on it are
invalid or missing, so uses of other headers may not resolve to them.
Include-cleaner then reports those headers as unused, and its fix
removes includes that the file needs.

Don't report unused includes in a file that has a header not found.

Assisted-by: Claude Code
---
 clang-tools-extra/clangd/ParsedAST.cpp        |  7 +++++++
 .../clangd/unittests/DiagnosticsTests.cpp     | 20 +++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/clang-tools-extra/clangd/ParsedAST.cpp 
b/clang-tools-extra/clangd/ParsedAST.cpp
index 52db921f660aeb..b8e125badb7135 100644
--- a/clang-tools-extra/clangd/ParsedAST.cpp
+++ b/clang-tools-extra/clangd/ParsedAST.cpp
@@ -29,6 +29,7 @@
 #include "clang/AST/ExternalASTSource.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/DiagnosticLex.h"
 #include "clang/Basic/DiagnosticSema.h"
 #include "clang/Basic/FileEntry.h"
 #include "clang/Basic/LLVM.h"
@@ -273,6 +274,12 @@ std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, 
llvm::StringRef Code,
   bool SuppressUnused =
       Cfg.Diagnostics.Suppress.contains("unused-includes") ||
       Cfg.Diagnostics.UnusedIncludes == Config::IncludesPolicy::None;
+  // Behind a header that is not found, uses may not resolve to the headers
+  // that provide them, which would then seem unused.
+  if (llvm::any_of(AST.getDiagnostics(), [](const Diag &D) {
+        return D.Source == Diag::Clang && D.ID == diag::err_pp_file_not_found;
+      }))
+    SuppressUnused = true;
   if (SuppressMissing && SuppressUnused)
     return {};
   auto Findings = computeIncludeCleanerFindings(
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp 
b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 982854c1e5c186..b1d1c3c1eafeb2 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -2344,6 +2344,26 @@ TEST(DiagnosticsTest, IncludeCleaner) {
   EXPECT_THAT(TU.build().getDiagnostics(), IsEmpty());
 }
 
+TEST(DiagnosticsTest, IncludeCleanerWithMissingHeader) {
+  // Behind a header that is not found, no include seems unused.
+  Annotations Test(R"cpp(
+    #include $[["not-found.h"]] // error-ok
+    #include "unused.h"
+  )cpp");
+  TestTU TU = TestTU::withCode(Test.code());
+  TU.AdditionalFiles["unused.h"] = R"cpp(
+    #pragma once
+    void unused() {}
+  )cpp";
+  Config Cfg;
+  Cfg.Diagnostics.UnusedIncludes = Config::IncludesPolicy::Strict;
+  WithContextValue WithCfg(Config::Key, std::move(Cfg));
+  EXPECT_THAT(TU.build().getDiagnostics(),
+              ElementsAre(::testing::AllOf(
+                  Diag(Test.range(), "'not-found.h' file not found"),
+                  diagName("pp_file_not_found"))));
+}
+
 TEST(DiagnosticsTest, FixItFromHeader) {
   llvm::StringLiteral Header(R"cpp(
     void foo(int *);

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

Reply via email to