kadircet created this revision. kadircet added a reviewer: hokein. Herald added a project: All. kadircet requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
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 Repository: rG LLVM Github Monorepo 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
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
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits