diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp --- a/clang-tools-extra/clangd/IncludeCleaner.cpp +++ b/clang-tools-extra/clangd/IncludeCleaner.cpp @@ -71,6 +71,8 @@ bool mayConsiderUnused( const Inclusion &Inc, ParsedAST &AST, const include_cleaner::PragmaIncludes *PI) { + if (PI && PI->shouldKeep(Inc.HashLine + 1)) + return false; // FIXME(kirillbobyrev): We currently do not support the umbrella headers. // System headers are likely to be standard library headers. // Until we have good support for umbrella headers, don't warn about them. @@ -82,8 +84,6 @@ AST.getIncludeStructure().getRealPath(HID)); assert(FE); if (PI) { - if (PI->shouldKeep(Inc.HashLine + 1)) - return false; // Check if main file is the public interface for a private header. If so we // shouldn't diagnose it as unused. if (auto PHeader = PI->getPublic(*FE); !PHeader.empty()) { diff --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp --- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -78,6 +78,8 @@ auto TU = TestTU::withCode(R"cpp( #include #include + #include // IWYU pragma: keep + #include // IWYU pragma: export std::list x; )cpp"); // Layout of std library impl is not relevant. @@ -86,10 +88,13 @@ namespace std { template class list {}; template class queue {}; + template class vector {}; } )cpp"; TU.AdditionalFiles["list"] = "#include "; TU.AdditionalFiles["queue"] = "#include "; + TU.AdditionalFiles["vector"] = "#include "; + TU.AdditionalFiles["string"] = "#include "; TU.ExtraArgs = {"-isystem", testRoot()}; auto AST = TU.build(); IncludeCleanerFindings Findings = computeIncludeCleanerFindings(AST);