xref: /aosp_15_r20/external/clang/test/Sema/complex-init-list.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -verify -fsyntax-only -pedantic
2*67e74705SXin Li 
3*67e74705SXin Li // This file tests the clang extension which allows initializing the components
4*67e74705SXin Li // of a complex number individually using an initialization list. Basically,
5*67e74705SXin Li // if you have an explicit init list for a complex number that contains two
6*67e74705SXin Li // initializers, this extension kicks in to turn it into component-wise
7*67e74705SXin Li // initialization.
8*67e74705SXin Li //
9*67e74705SXin Li // This extension is useful because there isn't any way to accurately build
10*67e74705SXin Li // a complex number at the moment besides setting the components with
11*67e74705SXin Li // __real__ and __imag__, which is inconvenient and not usable for constants.
12*67e74705SXin Li // (Of course, there are other extensions we could implement that would
13*67e74705SXin Li // allow this, like some sort of __builtin_build_complex.)
14*67e74705SXin Li //
15*67e74705SXin Li // FIXME: It would be a good idea to have a warnings for implicit
16*67e74705SXin Li // real->complex and complex->real conversions; as-is, it's way too easy
17*67e74705SXin Li // to get implicit conversions when they are not intended.
18*67e74705SXin Li 
19*67e74705SXin Li // Basic testcase
20*67e74705SXin Li _Complex float valid1 = { 1.0f, 2.0f }; // expected-warning {{specifying real and imaginary components is an extension}}
21*67e74705SXin Li 
22*67e74705SXin Li 
23*67e74705SXin Li // Struct for nesting tests
24*67e74705SXin Li struct teststruct { _Complex float x; };
25*67e74705SXin Li 
26*67e74705SXin Li 
27*67e74705SXin Li // Random other valid stuff
28*67e74705SXin Li _Complex int valid2 = { 1, 2 }; // expected-warning {{complex integer}} expected-warning {{specifying real and imaginary components is an extension}}
29*67e74705SXin Li struct teststruct valid3 = { { 1.0f, 2.0f} }; // expected-warning {{specifying real and imaginary components is an extension}}
30*67e74705SXin Li _Complex float valid4[2] = { {1.0f, 1.0f}, {1.0f, 1.0f} }; // expected-warning 2 {{specifying real and imaginary components is an extension}}
31*67e74705SXin Li // FIXME: We need some sort of warning for valid5
32*67e74705SXin Li _Complex float valid5 = {1.0f, 1.0fi}; // expected-warning {{imaginary constants}} expected-warning {{specifying real and imaginary components is an extension}}
33*67e74705SXin Li 
34*67e74705SXin Li 
35*67e74705SXin Li // Random invalid stuff
36*67e74705SXin Li struct teststruct invalid1 = { 1, 2 }; // expected-warning {{excess elements}}
37*67e74705SXin Li _Complex float invalid2 = { 1, 2, 3 }; // expected-warning {{excess elements}}
38*67e74705SXin Li _Complex float invalid3 = {}; // expected-error {{scalar initializer cannot be empty}} expected-warning {{GNU empty initializer}}
39*67e74705SXin Li 
40*67e74705SXin Li 
41*67e74705SXin Li // Check incomplete array sizing
42*67e74705SXin Li _Complex float sizetest1[] = { {1.0f, 1.0f}, {1.0f, 1.0f} }; // expected-warning 2 {{specifying real and imaginary components is an extension}}
43*67e74705SXin Li _Complex float sizecheck1[(sizeof(sizetest1) == sizeof(*sizetest1)*2) ? 1 : -1];
44*67e74705SXin Li _Complex float sizetest2[] = { 1.0f, 1.0f, {1.0f, 1.0f} };  // expected-warning {{specifying real and imaginary components is an extension}}
45*67e74705SXin Li _Complex float sizecheck2[(sizeof(sizetest2) == sizeof(*sizetest2)*3) ? 1 : -1];
46*67e74705SXin Li 
47*67e74705SXin Li // Constant-folding with init list.
48*67e74705SXin Li _Complex float x = 2 + (_Complex float) { 1, 2 };  // expected-warning {{specifying real and imaginary components is an extension}}
49