complex-init-list.c 2.7 KB

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