Skip to content

Commit dcb2824

Browse files
committed
[clangd] Respect IWYU keep pragma for standard headers.
see the issue llvm#64191 Differential Revision: https://reviews.llvm.org/D156650
1 parent f752265 commit dcb2824

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) {
7171
bool mayConsiderUnused(
7272
const Inclusion &Inc, ParsedAST &AST,
7373
const include_cleaner::PragmaIncludes *PI) {
74+
if (PI && PI->shouldKeep(Inc.HashLine + 1))
75+
return false;
7476
// FIXME(kirillbobyrev): We currently do not support the umbrella headers.
7577
// System headers are likely to be standard library headers.
7678
// Until we have good support for umbrella headers, don't warn about them.
@@ -82,8 +84,6 @@ bool mayConsiderUnused(
8284
AST.getIncludeStructure().getRealPath(HID));
8385
assert(FE);
8486
if (PI) {
85-
if (PI->shouldKeep(Inc.HashLine + 1))
86-
return false;
8787
// Check if main file is the public interface for a private header. If so we
8888
// shouldn't diagnose it as unused.
8989
if (auto PHeader = PI->getPublic(*FE); !PHeader.empty()) {

clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ TEST(IncludeCleaner, StdlibUnused) {
7878
auto TU = TestTU::withCode(R"cpp(
7979
#include <list>
8080
#include <queue>
81+
#include <vector> // IWYU pragma: keep
82+
#include <string> // IWYU pragma: export
8183
std::list<int> x;
8284
)cpp");
8385
// Layout of std library impl is not relevant.
@@ -86,10 +88,13 @@ TEST(IncludeCleaner, StdlibUnused) {
8688
namespace std {
8789
template <typename> class list {};
8890
template <typename> class queue {};
91+
template <typename> class vector {};
8992
}
9093
)cpp";
9194
TU.AdditionalFiles["list"] = "#include <bits>";
9295
TU.AdditionalFiles["queue"] = "#include <bits>";
96+
TU.AdditionalFiles["vector"] = "#include <bits>";
97+
TU.AdditionalFiles["string"] = "#include <bits>";
9398
TU.ExtraArgs = {"-isystem", testRoot()};
9499
auto AST = TU.build();
95100
IncludeCleanerFindings Findings = computeIncludeCleanerFindings(AST);

0 commit comments

Comments
 (0)