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