[PATCH] D140380: [include-cleaner] Respect IWYU pragmas during analyze

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 20 01:54:02 PST 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb76cc30e1585: [include-cleaner] Respect IWYU pragmas during analyze (authored by kadircet).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140380/new/

https://reviews.llvm.org/D140380

Files:
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -26,9 +26,9 @@
 
 namespace clang::include_cleaner {
 namespace {
+using testing::AllOf;
 using testing::Contains;
 using testing::ElementsAre;
-using testing::AllOf;
 using testing::Pair;
 using testing::UnorderedElementsAre;
 
@@ -181,6 +181,7 @@
   Inputs.Code = R"cpp(
 #include "a.h"
 #include "b.h"
+#include "keep.h" // IWYU pragma: keep
 
 int x = a + c;
 )cpp";
@@ -190,28 +191,32 @@
     int b;
   )cpp");
   Inputs.ExtraFiles["c.h"] = guard("int c;");
+  Inputs.ExtraFiles["keep.h"] = guard("");
 
   RecordedPP PP;
-  Inputs.MakeAction = [&PP] {
+  PragmaIncludes PI;
+  Inputs.MakeAction = [&PP, &PI] {
     struct Hook : public SyntaxOnlyAction {
     public:
-      Hook(RecordedPP &PP) : PP(PP) {}
+      Hook(RecordedPP &PP, PragmaIncludes &PI) : PP(PP), PI(PI) {}
       bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
         CI.getPreprocessor().addPPCallbacks(PP.record(CI.getPreprocessor()));
+        PI.record(CI);
         return true;
       }
 
       RecordedPP &PP;
+      PragmaIncludes Π
     };
-    return std::make_unique<Hook>(PP);
+    return std::make_unique<Hook>(PP, PI);
   };
 
   TestAST AST(Inputs);
   auto Decls = AST.context().getTranslationUnitDecl()->decls();
   auto Results =
       analyze(std::vector<Decl *>{Decls.begin(), Decls.end()},
-              PP.MacroReferences, PP.Includes, /*PragmaIncludes=*/nullptr,
-              AST.sourceManager(), AST.preprocessor().getHeaderSearchInfo());
+              PP.MacroReferences, PP.Includes, &PI, AST.sourceManager(),
+              AST.preprocessor().getHeaderSearchInfo());
 
   const Include *B = PP.Includes.atLine(3);
   ASSERT_EQ(B->Spelled, "b.h");
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -101,7 +101,7 @@
 
   AnalysisResults Results;
   for (const Include &I : Inc.all())
-    if (!Used.contains(&I))
+    if (!Used.contains(&I) && PI && !PI->shouldKeep(I.Line))
       Results.Unused.push_back(&I);
   for (llvm::StringRef S : Missing.keys())
     Results.Missing.push_back(S.str());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140380.484192.patch
Type: text/x-patch
Size: 2486 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221220/8d69f33d/attachment.bin>


More information about the cfe-commits mailing list