[PATCH] D156712: [include-cleaner] Handle StdInitializerListExprs

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 02:05:32 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG3736eaa6a0b8: [include-cleaner] Handle StdInitializerListExprs (authored by kadircet).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156712

Files:
  clang-tools-extra/include-cleaner/lib/WalkAST.cpp
  clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -282,7 +282,6 @@
            "using ns::^function;");
 }
 
-
 TEST(WalkAST, Alias) {
   testWalk(R"cpp(
     namespace ns { int x; }
@@ -510,5 +509,14 @@
   testWalk("enum class E : int {};", "enum class ^E : int ;");
 }
 
+TEST(WalkAST, InitializerList) {
+  testWalk(R"cpp(
+       namespace std {
+        template <typename T> struct $implicit^initializer_list {};
+       })cpp",
+           R"cpp(
+       const char* s = "";
+       auto sx = ^{s};)cpp");
+}
 } // namespace
 } // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/lib/WalkAST.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -304,6 +304,16 @@
     }
     return RecursiveASTVisitor::TraverseTemplateArgumentLoc(TL);
   }
+
+  bool VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
+    // Reliance on initializer_lists requires std::initializer_list to be
+    // visible per standard. So report a reference to it, otherwise include of
+    // `<initializer_list>` might not receive any use.
+    report(E->getExprLoc(),
+           const_cast<CXXRecordDecl *>(E->getBestDynamicClassType()),
+           RefType::Implicit);
+    return true;
+  }
 };
 
 } // namespace


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156712.545967.patch
Type: text/x-patch
Size: 1580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230801/576cc861/attachment.bin>


More information about the cfe-commits mailing list