[clang-tools-extra] b673bc3 - [clangd] Suppress IncludeCleaner warnings for headers behind pragma keep

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 8 09:07:53 PST 2021


Author: Kirill Bobyrev
Date: 2021-12-08T18:07:40+01:00
New Revision: b673bc36eacd68c15262a222ce940f36f4be0cec

URL: https://github.com/llvm/llvm-project/commit/b673bc36eacd68c15262a222ce940f36f4be0cec
DIFF: https://github.com/llvm/llvm-project/commit/b673bc36eacd68c15262a222ce940f36f4be0cec.diff

LOG: [clangd] Suppress IncludeCleaner warnings for headers behind pragma keep

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.

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D115345

Added: 
    

Modified: 
    clang-tools-extra/clangd/IncludeCleaner.cpp
    clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index 0e44c080625a..043fb8c3c6a5 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -223,7 +223,7 @@ bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST) {
   // 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.

diff  --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
index 1313485f481d..5c82332c9baf 100644
--- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -9,6 +9,7 @@
 #include "Annotations.h"
 #include "IncludeCleaner.h"
 #include "TestTU.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
@@ -17,6 +18,7 @@ namespace clangd {
 namespace {
 
 using ::testing::ElementsAre;
+using ::testing::IsEmpty;
 using ::testing::UnorderedElementsAre;
 
 std::string guard(llvm::StringRef Code) {
@@ -320,7 +322,7 @@ TEST(IncludeCleaner, VirtualBuffers) {
   EXPECT_THAT(ReferencedHeaderNames, ElementsAre(testPath("macros.h")));
 
   // Sanity check.
-  EXPECT_THAT(getUnused(AST, ReferencedHeaders), ::testing::IsEmpty());
+  EXPECT_THAT(getUnused(AST, ReferencedHeaders), IsEmpty());
 }
 
 TEST(IncludeCleaner, DistinctUnguardedInclusions) {
@@ -397,6 +399,21 @@ TEST(IncludeCleaner, NonSelfContainedHeaders) {
               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());
+  EXPECT_TRUE(ReferencedFiles.empty());
+  EXPECT_THAT(AST.getDiagnostics(), llvm::ValueIs(IsEmpty()));
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang


        


More information about the cfe-commits mailing list