Skip to content

Commit 2b3abb9

Browse files
MitalAshokAlexisPerry
authored andcommitted
[Clang] [Sema] Diagnose unknown std::initializer_list layout in SemaInit (llvm#95580)
This checks if the layout of `std::initializer_list` is something Clang can handle much earlier and deduplicates the checks in CodeGen/CGExprAgg.cpp and AST/ExprConstant.cpp Also now diagnose `union initializer_list` (Fixes llvm#95495), bit-field for the size (Fixes a crash that would happen during codegen if it were unnamed), base classes (that wouldn't be initialized) and polymorphic classes (whose vtable pointer wouldn't be initialized).
1 parent 5e38809 commit 2b3abb9

33 files changed

+195
-108
lines changed

clang-tools-extra/clangd/unittests/ASTTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
8080
namespace std
8181
{
8282
template<class _E>
83-
class [[initializer_list]] {};
83+
class [[initializer_list]] { const _E *a, *b; };
8484
}
8585
8686
^auto i = {1,2};

clang-tools-extra/clangd/unittests/HoverTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ TEST(Hover, All) {
22842284
namespace std
22852285
{
22862286
template<class _E>
2287-
class initializer_list {};
2287+
class initializer_list { const _E *a, *b; };
22882288
}
22892289
void foo() {
22902290
^[[auto]] i = {1,2};

clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ TEST(ParameterHints, ConstructorStdInitList) {
945945
// Do not show hints for std::initializer_list constructors.
946946
assertParameterHints(R"cpp(
947947
namespace std {
948-
template <typename> class initializer_list {};
948+
template <typename E> class initializer_list { const E *a, *b; };
949949
}
950950
struct S {
951951
S(std::initializer_list<int> param);

clang-tools-extra/clangd/unittests/XRefsTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ TEST(LocateSymbol, All) {
771771
namespace std
772772
{
773773
template<class _E>
774-
class [[initializer_list]] {};
774+
class [[initializer_list]] { const _E *a, *b; };
775775
}
776776
777777
^auto i = {1,2};

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ TEST(WalkAST, Enums) {
534534
TEST(WalkAST, InitializerList) {
535535
testWalk(R"cpp(
536536
namespace std {
537-
template <typename T> struct $implicit^initializer_list {};
537+
template <typename T> struct $implicit^initializer_list { const T *a, *b; };
538538
})cpp",
539539
R"cpp(
540540
const char* s = "";

clang-tools-extra/test/clang-tidy/checkers/modernize/min-max-use-initializer-list.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ T max(T a, T b) {
1111
namespace std {
1212
template< class T >
1313
struct initializer_list {
14+
const T *a, *b;
1415
initializer_list()=default;
1516
initializer_list(T*,int){}
1617
const T* begin() const {return nullptr;}

clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace-ignore-implicit-constructors.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// RUN: true}}"
55

66
namespace std {
7-
template <typename>
7+
template <typename E>
88
class initializer_list
99
{
1010
public:
11+
const E *a, *b;
1112
initializer_list() noexcept {}
1213
};
1314

clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
// RUN: '::std::make_pair; ::std::make_tuple; ::test::MakeSingle'}}"
99

1010
namespace std {
11-
template <typename>
11+
template <typename E>
1212
class initializer_list {
1313
public:
14+
const E *a, *b;
1415
initializer_list() noexcept {}
1516
};
1617

clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace std {
77

8-
typedef int size_t;
8+
typedef decltype(sizeof 0) size_t;
99

1010
template<class E> class initializer_list {
1111
public:
@@ -15,6 +15,8 @@ template<class E> class initializer_list {
1515
using size_type = size_t;
1616
using iterator = const E*;
1717
using const_iterator = const E*;
18+
iterator p;
19+
size_t sz;
1820
initializer_list();
1921
size_t size() const; // number of elements
2022
const E* begin() const; // first element

clang-tools-extra/test/clang-tidy/checkers/readability/isolate-declaration-cxx17.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct SomeClass {
3131

3232
namespace std {
3333
template <typename T>
34-
class initializer_list {};
34+
class initializer_list { const T *a, *b; };
3535

3636
template <typename T>
3737
class vector {

0 commit comments

Comments
 (0)