diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 005bf99a62457..faac3b17b223f 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -225,6 +225,8 @@ Bug Fixes in This Version enabling short-circuiting coroutines use cases. This fixes (`#56532 `_) in antecipation of `CWG2563 _`. +- Fix highlighting issue with ``_Complex`` and initialization list with more than + 2 items. (`#61518 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 17d8b6c98207b..46517c9dde06a 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -1536,7 +1536,7 @@ void InitListChecker::CheckComplexType(const InitializedEntity &Entity, // the element type of the complex type. The first element initializes // the real part, and the second element intitializes the imaginary part. - if (IList->getNumInits() != 2) + if (IList->getNumInits() < 2) return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, StructuredIndex); diff --git a/clang/test/Sema/caret-diags-complex-init.cpp b/clang/test/Sema/caret-diags-complex-init.cpp new file mode 100644 index 0000000000000..d8a1b7837a640 --- /dev/null +++ b/clang/test/Sema/caret-diags-complex-init.cpp @@ -0,0 +1,39 @@ +// RUN: not %clang_cc1 -std=c++11 -fsyntax-only -fcaret-diagnostics-max-lines 5 %s 2>&1 | FileCheck %s -strict-whitespace + + +//CHECK: {{.*}}: error: excess elements in scalar initializer +//CHECK-NEXT: {{^}}_Complex double gz1 = {1, 2, 3}; +//CHECK-NEXT: {{^}} ^{{$}} +_Complex double gz1 = {1, 2, 3}; + +//CHECK: {{.*}}: error: excess elements in scalar initializer +//CHECK-NEXT: {{^}}_Complex double dd = {1.0, 2.0, 3.0}; +//CHECK-NEXT: {{^}} ^~~{{$}} +_Complex double dd = {1.0, 2.0, 3.0}; + +//CHECK: {{.*}}: error: excess elements in scalar initializer +//CHECK-NEXT: {{^}}_Complex float fd = {1.0, 2.0, 3.0, 4.0, 5.0}; +//CHECK-NEXT: {{^}} ^~~{{$}} +_Complex float fd = {1.0, 2.0, 3.0, 4.0, 5.0}; + +//CHECK: {{.*}}: error: no viable conversion from 'foo' to 'double' +//CHECK-NEXT: {{^}}_Complex double ds = {f, 1.0, b}; +//CHECK-NEXT: {{^}} ^{{$}} +struct foo{}; +struct bar{}; + +foo f; +bar b; +_Complex double ds = {f, 1.0, b}; + +//CHECK: {{.*}}: error: no viable conversion from 'foo' to 'double' +//CHECK-NEXT: {{^}}_Complex double fg = {1.0, f}; +//CHECK-NEXT: {{^}} ^{{$}} +_Complex double fg = {1.0, f}; + + +//CHECK: {{.*}}: error: excess elements in scalar initializer +//CHECK-NEXT: {{^}}_Complex double gg = {1.0, 2.0, f}; +//CHECK-NEXT: {{^}} ^{{$}} +//CHECK-NEXT: {{^}}6 errors generated. +_Complex double gg = {1.0, 2.0, f}; diff --git a/clang/test/Sema/complex-init-list.c b/clang/test/Sema/complex-init-list.c index bfc6899ac235d..b8f87f57f0793 100644 --- a/clang/test/Sema/complex-init-list.c +++ b/clang/test/Sema/complex-init-list.c @@ -25,17 +25,21 @@ struct teststruct { _Complex float x; }; // Random other valid stuff -_Complex int valid2 = { 1, 2 }; // expected-warning {{complex integer}} expected-warning {{specifying real and imaginary components is an extension}} +_Complex int valid2 = { 1, 2 }; // expected-warning {{complex integer}} \ + // expected-warning {{specifying real and imaginary components is an extension}} struct teststruct valid3 = { { 1.0f, 2.0f} }; // expected-warning {{specifying real and imaginary components is an extension}} _Complex float valid4[2] = { {1.0f, 1.0f}, {1.0f, 1.0f} }; // expected-warning 2 {{specifying real and imaginary components is an extension}} // FIXME: We need some sort of warning for valid5 -_Complex float valid5 = {1.0f, 1.0fi}; // expected-warning {{imaginary constants}} expected-warning {{specifying real and imaginary components is an extension}} +_Complex float valid5 = {1.0f, 1.0fi}; // expected-warning {{imaginary constants}} \ + // expected-warning {{specifying real and imaginary components is an extension}} // Random invalid stuff struct teststruct invalid1 = { 1, 2 }; // expected-warning {{excess elements}} -_Complex float invalid2 = { 1, 2, 3 }; // expected-warning {{excess elements}} -_Complex float invalid3 = {}; // expected-error {{scalar initializer cannot be empty}} expected-warning {{GNU empty initializer}} +_Complex float invalid2 = { 1, 2, 3 }; // expected-warning {{specifying real and imaginary components is an extension}} \ + // expected-warning {{excess elements in scalar initializer}} +_Complex float invalid3 = {}; // expected-error {{scalar initializer cannot be empty}} \ + // expected-warning {{GNU empty initializer}} // Check incomplete array sizing @@ -46,3 +50,9 @@ _Complex float sizecheck2[(sizeof(sizetest2) == sizeof(*sizetest2)*3) ? 1 : -1]; // Constant-folding with init list. _Complex float x = 2 + (_Complex float) { 1, 2 }; // expected-warning {{specifying real and imaginary components is an extension}} + +// initialization list +_Complex double cd = {1.0, 2.0, 3.0}; // expected-warning {{specifying real and imaginary components is an extension}} \ + // expected-warning {{excess elements in scalar initializer}} +_Complex float cf = {1.1f, 2.2f, 3.3f, 4.4f}; // expected-warning {{specifying real and imaginary components is an extension}} \ + // expected-warning {{excess elements in scalar initializer}}