Skip to content

Commit

Permalink
[clangd] Respect IWYU keep pragma for standard headers.
Browse files Browse the repository at this point in the history
see the issue #64191

Differential Revision: https://reviews.llvm.org/D156650
  • Loading branch information
hokein committed Jul 31, 2023
1 parent f752265 commit dcb2824
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) {
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.
Expand All @@ -82,8 +84,6 @@ bool mayConsiderUnused(
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()) {
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ TEST(IncludeCleaner, StdlibUnused) {
auto TU = TestTU::withCode(R"cpp(
#include <list>
#include <queue>
#include <vector> // IWYU pragma: keep
#include <string> // IWYU pragma: export
std::list<int> x;
)cpp");
// Layout of std library impl is not relevant.
Expand All @@ -86,10 +88,13 @@ TEST(IncludeCleaner, StdlibUnused) {
namespace std {
template <typename> class list {};
template <typename> class queue {};
template <typename> class vector {};
}
)cpp";
TU.AdditionalFiles["list"] = "#include <bits>";
TU.AdditionalFiles["queue"] = "#include <bits>";
TU.AdditionalFiles["vector"] = "#include <bits>";
TU.AdditionalFiles["string"] = "#include <bits>";
TU.ExtraArgs = {"-isystem", testRoot()};
auto AST = TU.build();
IncludeCleanerFindings Findings = computeIncludeCleanerFindings(AST);
Expand Down

0 comments on commit dcb2824

Please sign in to comment.