[clang-tools-extra] 3736eaa - [include-cleaner] Handle StdInitializerListExprs

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


Author: Kadir Cetinkaya
Date: 2023-08-01T10:58:56+02:00
New Revision: 3736eaa6a0b8396e30978c768c02e43c821a8257

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

LOG: [include-cleaner] Handle StdInitializerListExprs

Per C++ standard, programs omitting the definition for initializer_list
is ill-formed, https://eel.is/c++draft/dcl.init.list#2.

Fixes https://github.com/llvm/llvm-project/issues/64198

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index 8cfda506fc254f..febdf19e695cd9 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -304,6 +304,16 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
     }
     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

diff  --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 525f645ec91efe..3a86f36e3964f6 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -282,7 +282,6 @@ template<> void $ambiguous^function(int);       // full specialization
            "using ns::^function;");
 }
 
-
 TEST(WalkAST, Alias) {
   testWalk(R"cpp(
     namespace ns { int x; }
@@ -510,5 +509,14 @@ TEST(WalkAST, Enums) {
   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


        


More information about the cfe-commits mailing list