Skip to content

Commit dc2d2f9

Browse files
hokeintru
authored andcommitted
[clangd] Respect IWYU keep pragma for standard headers.
see the issue #64191 Differential Revision: https://reviews.llvm.org/D156650 (cherry picked from commit dcb2824)
1 parent b879e0f commit dc2d2f9

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
@@ -70,6 +70,8 @@ bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) {
7070
bool mayConsiderUnused(
7171
const Inclusion &Inc, ParsedAST &AST,
7272
const include_cleaner::PragmaIncludes *PI) {
73+
if (PI && PI->shouldKeep(Inc.HashLine + 1))
74+
return false;
7375
// FIXME(kirillbobyrev): We currently do not support the umbrella headers.
7476
// System headers are likely to be standard library headers.
7577
// Until we have good support for umbrella headers, don't warn about them.
@@ -81,8 +83,6 @@ bool mayConsiderUnused(
8183
AST.getIncludeStructure().getRealPath(HID));
8284
assert(FE);
8385
if (PI) {
84-
if (PI->shouldKeep(Inc.HashLine + 1))
85-
return false;
8686
// Check if main file is the public interface for a private header. If so we
8787
// shouldn't diagnose it as unused.
8888
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
@@ -76,6 +76,8 @@ TEST(IncludeCleaner, StdlibUnused) {
7676
auto TU = TestTU::withCode(R"cpp(
7777
#include <list>
7878
#include <queue>
79+
#include <vector> // IWYU pragma: keep
80+
#include <string> // IWYU pragma: export
7981
std::list<int> x;
8082
)cpp");
8183
// Layout of std library impl is not relevant.
@@ -84,10 +86,13 @@ TEST(IncludeCleaner, StdlibUnused) {
8486
namespace std {
8587
template <typename> class list {};
8688
template <typename> class queue {};
89+
template <typename> class vector {};
8790
}
8891
)cpp";
8992
TU.AdditionalFiles["list"] = "#include <bits>";
9093
TU.AdditionalFiles["queue"] = "#include <bits>";
94+
TU.AdditionalFiles["vector"] = "#include <bits>";
95+
TU.AdditionalFiles["string"] = "#include <bits>";
9196
TU.ExtraArgs = {"-isystem", testRoot()};
9297
auto AST = TU.build();
9398
IncludeCleanerFindings Findings = computeIncludeCleanerFindings(AST);

0 commit comments

Comments
 (0)