Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 2 | |
| 3 | // C++-specific tests for integral constant expressions. |
| 4 | |
| 5 | const int c = 10; |
| 6 | int ar[c]; |
Douglas Gregor | 59600d8 | 2009-09-10 17:44:23 +0000 | [diff] [blame] | 7 | |
| 8 | struct X0 { |
| 9 | static const int value = static_cast<int>(4.0); |
| 10 | }; |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 11 | |
| 12 | void f() { |
| 13 | if (const int value = 17) { |
| 14 | int array[value]; |
| 15 | } |
| 16 | } |
Eli Friedman | c013118 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 17 | |
| 18 | int a() { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 19 | const int t=t; // expected-note {{declared here}} |
John McCall | 0fb9708 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 20 | switch(1) { // expected-warning {{no case matching constant switch condition '1'}} |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 21 | case t:; // expected-error {{not an integral constant expression}} expected-note {{initializer of 't' is not a constant expression}} |
Eli Friedman | c013118 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 22 | } |
| 23 | } |
John McCall | 1f1b3b3 | 2010-02-06 01:07:37 +0000 | [diff] [blame] | 24 | |
| 25 | // PR6206: out-of-line definitions are legit |
| 26 | namespace pr6206 { |
| 27 | class Foo { |
| 28 | public: |
| 29 | static const int kBar; |
| 30 | }; |
| 31 | |
| 32 | const int Foo::kBar = 20; |
| 33 | |
| 34 | char Test() { |
| 35 | char str[Foo::kBar]; |
| 36 | str[0] = '0'; |
| 37 | return str[0]; |
| 38 | } |
| 39 | } |
John McCall | f604a56 | 2010-02-24 09:03:18 +0000 | [diff] [blame] | 40 | |
| 41 | // PR6373: default arguments don't count. |
| 42 | void pr6373(const unsigned x = 0) { |
| 43 | unsigned max = 80 / x; |
| 44 | } |
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | // rdar://9204520 |
| 48 | namespace rdar9204520 { |
| 49 | |
| 50 | struct A { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 51 | static const int B = int(0.75 * 1000 * 1000); // expected-warning {{not a constant expression; folding it to a constant is a GNU extension}} |
Chris Lattner | 24c38e1 | 2011-06-14 05:46:29 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | int foo() { return A::B; } |
| 55 | } |
| 56 | |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 57 | // PR11040 |
| 58 | const int x = 10; |
| 59 | int* y = reinterpret_cast<const char&>(x); // expected-error {{cannot initialize}} |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 60 | |
| 61 | // This isn't an integral constant expression, but make sure it folds anyway. |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 62 | struct PR8836 { char _; long long a; }; // expected-warning {{long long}} |
| 63 | int PR8836test[(__typeof(sizeof(int)))&reinterpret_cast<const volatile char&>((((PR8836*)0)->a))]; // expected-warning {{folded to constant array as an extension}} |
| 64 | |
| 65 | const int nonconst = 1.0; |
| 66 | int arr[nonconst]; // expected-warning {{folded to constant array as an extension}} |
| 67 | const int castfloat = static_cast<int>(1.0); |
| 68 | int arr2[castfloat]; // ok |