kbobyrev created this revision. kbobyrev added a reviewer: kadircet. Herald added subscribers: usaxena95, arphaman. kbobyrev requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang-tools-extra.
D114072 <https://reviews.llvm.org/D114072> allows filtering out the warnings for headers behind `// IWYU pragma: keep`. This is the first step towards more useful IWYU pragmas support and fine-grained control over the IncludeCleaner warnings. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D115345 Files: clang-tools-extra/clangd/IncludeCleaner.cpp clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -397,6 +397,25 @@ UnorderedElementsAre(testPath("foo.h"))); } +TEST(IncludeCleaner, IWYUPragmas) { + TestTU TU; + TU.Code = R"cpp( + #include "behind_keep.h" // IWYU pragma: keep + )cpp"; + TU.AdditionalFiles["behind_keep.h"] = guard(""); + ParsedAST AST = TU.build(); + + auto ReferencedFiles = + findReferencedFiles(findReferencedLocations(AST), + AST.getIncludeStructure(), AST.getSourceManager()); + llvm::StringSet<> ReferencedFileNames; + EXPECT_TRUE(ReferencedFiles.empty()); + auto &Includes = AST.getIncludeStructure(); + EXPECT_THAT(getUnused(AST, translateToHeaderIDs(ReferencedFiles, Includes, + AST.getSourceManager())), + ::testing::IsEmpty()); +} + } // namespace } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/IncludeCleaner.cpp =================================================================== --- clang-tools-extra/clangd/IncludeCleaner.cpp +++ clang-tools-extra/clangd/IncludeCleaner.cpp @@ -223,7 +223,7 @@ // headers are likely to be the Standard Library headers. Until we have a // good support for umbrella headers and Standard Library headers, don't warn // about them. - if (Inc.Written.front() == '<') + if (Inc.Written.front() == '<' || Inc.BehindPragmaKeep) return false; // Headers without include guards have side effects and are not // self-contained, skip them.
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -397,6 +397,25 @@ UnorderedElementsAre(testPath("foo.h"))); } +TEST(IncludeCleaner, IWYUPragmas) { + TestTU TU; + TU.Code = R"cpp( + #include "behind_keep.h" // IWYU pragma: keep + )cpp"; + TU.AdditionalFiles["behind_keep.h"] = guard(""); + ParsedAST AST = TU.build(); + + auto ReferencedFiles = + findReferencedFiles(findReferencedLocations(AST), + AST.getIncludeStructure(), AST.getSourceManager()); + llvm::StringSet<> ReferencedFileNames; + EXPECT_TRUE(ReferencedFiles.empty()); + auto &Includes = AST.getIncludeStructure(); + EXPECT_THAT(getUnused(AST, translateToHeaderIDs(ReferencedFiles, Includes, + AST.getSourceManager())), + ::testing::IsEmpty()); +} + } // namespace } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/IncludeCleaner.cpp =================================================================== --- clang-tools-extra/clangd/IncludeCleaner.cpp +++ clang-tools-extra/clangd/IncludeCleaner.cpp @@ -223,7 +223,7 @@ // headers are likely to be the Standard Library headers. Until we have a // good support for umbrella headers and Standard Library headers, don't warn // about them. - if (Inc.Written.front() == '<') + if (Inc.Written.front() == '<' || Inc.BehindPragmaKeep) return false; // Headers without include guards have side effects and are not // self-contained, skip them.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits