~ubuntu-branches/ubuntu/trusty/llvm-toolchain-snapshot/trusty-201310232150

« back to all changes in this revision

Viewing changes to clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-27 15:01:57 UTC
  • mfrom: (0.10.1) (0.9.1) (0.8.1) (0.7.1) (0.6.1) (0.5.2)
  • Revision ID: package-import@ubuntu.com-20130527150157-tdkrsjpuvht7v0qx
Tags: 1:3.4~svn182733-1~exp1
* New snapshot release (3.4 release)
* Add a symlink of libLLVM-3.4.so.1 to usr/lib/llvm-3.4/lib/libLLVM-3.4.so
    to fix make the llvm-config-3.4 --libdir work (Closes: #708677)
  * Various packages rename to allow co installations:
    * libclang1 => libclang1-3.4
    * libclang1-dbg => libclang1-3.4-dbg
    * libclang-dev => libclang-3.4-dev
    * libclang-common-dev => libclang-common-3.4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// RUN: %clang_cc1 -verify -std=c++11 %s
 
1
// RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++11 -Werror=c++1y-extensions %s
 
2
// RUN: %clang_cc1 -verify -fcxx-exceptions -triple=x86_64-linux-gnu -std=c++1y -DCXX1Y %s
2
3
 
3
4
namespace N {
4
5
  typedef char C;
8
9
  typedef double D;
9
10
}
10
11
 
11
 
struct NonLiteral { // expected-note 2{{no constexpr constructors}}
 
12
struct NonLiteral { // expected-note 3{{no constexpr constructors}}
12
13
  NonLiteral() {}
13
14
  NonLiteral(int) {}
14
15
};
28
29
// constraints:
29
30
struct T : SS, NonLiteral { // expected-note {{base class 'NonLiteral' of non-literal type}}
30
31
  constexpr T();
31
 
  constexpr int f(); // expected-error {{non-literal type 'T' cannot have constexpr members}}
 
32
  constexpr int f() const; // expected-error {{non-literal type 'T' cannot have constexpr members}}
32
33
 
33
34
  //  - it shall not be virtual;
34
 
  virtual constexpr int ExplicitlyVirtual() { return 0; } // expected-error {{virtual function cannot be constexpr}}
 
35
  virtual constexpr int ExplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
35
36
 
36
 
  constexpr int ImplicitlyVirtual() { return 0; } // expected-error {{virtual function cannot be constexpr}}
 
37
  constexpr int ImplicitlyVirtual() const { return 0; } // expected-error {{virtual function cannot be constexpr}}
37
38
 
38
39
  //  - its return type shall be a literal type;
39
 
  constexpr NonLiteral NonLiteralReturn() { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
40
 
  constexpr void VoidReturn() { return; } // expected-error {{constexpr function's return type 'void' is not a literal type}}
 
40
  constexpr NonLiteral NonLiteralReturn() const { return {}; } // expected-error {{constexpr function's return type 'NonLiteral' is not a literal type}}
 
41
  constexpr void VoidReturn() const { return; }
 
42
#ifndef CXX1Y
 
43
  // expected-error@-2 {{constexpr function's return type 'void' is not a literal type}}
 
44
#endif
41
45
  constexpr ~T(); // expected-error {{destructor cannot be marked constexpr}}
42
 
  typedef NonLiteral F();
 
46
  typedef NonLiteral F() const;
43
47
  constexpr F NonLiteralReturn2; // ok until definition
44
48
 
45
49
  //  - each of its parameter types shall be a literal type;
46
 
  constexpr int NonLiteralParam(NonLiteral) { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
47
 
  typedef int G(NonLiteral);
 
50
  constexpr int NonLiteralParam(NonLiteral) const { return 0; } // expected-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}
 
51
  typedef int G(NonLiteral) const;
48
52
  constexpr G NonLiteralParam2; // ok until definition
49
53
 
50
54
  //  - its function-body shall be = delete, = default,
51
 
  constexpr int Deleted() = delete;
52
 
  // It's not possible for the function-body to legally be "= default" here.
 
55
  constexpr int Deleted() const = delete;
 
56
  // It's not possible for the function-body to legally be "= default" here
 
57
  // (that is, for a non-constructor function) in C++11.
53
58
  // Other than constructors, only the copy- and move-assignment operators and
54
59
  // destructor can be defaulted. Destructors can't be constexpr since they
55
60
  // don't have a literal return type. Defaulted assignment operators can't be
56
61
  // constexpr since they can't be const.
57
 
  constexpr T &operator=(const T&) = default; // expected-error {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
58
 
};
 
62
  constexpr T &operator=(const T&) = default;
 
63
#ifndef CXX1Y
 
64
  // expected-error@-2 {{an explicitly-defaulted copy assignment operator may not have 'const', 'constexpr' or 'volatile' qualifiers}}
 
65
  // expected-warning@-3 {{C++1y}}
 
66
#else
 
67
  // expected-error@-5 {{defaulted definition of copy assignment operator is not constexpr}}
 
68
#endif
 
69
};
 
70
#ifdef CXX1Y
 
71
struct T2 {
 
72
  int n = 0;
 
73
  constexpr T2 &operator=(const T2&) = default; // ok
 
74
};
 
75
struct T3 {
 
76
  constexpr T3 &operator=(const T3&) const = default;
 
77
  // expected-error@-1 {{an explicitly-defaulted copy assignment operator may not have 'const' or 'volatile' qualifiers}}
 
78
};
 
79
#endif
59
80
struct U {
60
 
  constexpr U SelfReturn();
61
 
  constexpr int SelfParam(U);
 
81
  constexpr U SelfReturn() const;
 
82
  constexpr int SelfParam(U) const;
62
83
};
63
84
 
64
85
struct V : virtual U { // expected-note {{here}}
65
 
  constexpr int F() { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
 
86
  constexpr int F() const { return 0; } // expected-error {{constexpr member function not allowed in struct with virtual base class}}
66
87
};
67
88
 
68
 
//  or a compound-statememt that contains only
69
 
constexpr int AllowedStmts() {
 
89
//  or a compound-statememt that contains only [CXX11]
 
90
constexpr int AllowedStmtsCXX11() {
70
91
  //  - null statements
71
92
  ;
72
93
 
91
112
  //  - and exactly one return statement
92
113
  return sizeof(K) + sizeof(C) + sizeof(K);
93
114
}
 
115
 
 
116
//  or a compound-statement that does not contain [CXX1Y]
 
117
constexpr int DisallowedStmtsCXX1Y_1() {
 
118
  //  - an asm-definition
 
119
  asm("int3"); // expected-error {{statement not allowed in constexpr function}}
 
120
  return 0;
 
121
}
 
122
constexpr int DisallowedStmtsCXX1Y_2() {
 
123
  //  - a goto statement
 
124
  goto x; // expected-error {{statement not allowed in constexpr function}}
 
125
x:
 
126
  return 0;
 
127
}
 
128
constexpr int DisallowedStmtsCXX1Y_3() {
 
129
  //  - a try-block,
 
130
  try {} catch (...) {} // expected-error {{statement not allowed in constexpr function}}
 
131
  return 0;
 
132
}
 
133
constexpr int DisallowedStmtsCXX1Y_4() {
 
134
  //  - a definition of a variable of non-literal type
 
135
  NonLiteral nl; // expected-error {{variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function}}
 
136
  return 0;
 
137
}
 
138
constexpr int DisallowedStmtsCXX1Y_5() {
 
139
  //  - a definition of a variable of static storage duration
 
140
  static constexpr int n = 123; // expected-error {{static variable not permitted in a constexpr function}}
 
141
  return n;
 
142
}
 
143
constexpr int DisallowedStmtsCXX1Y_6() {
 
144
  //  - a definition of a variable of thread storage duration
 
145
  thread_local constexpr int n = 123; // expected-error {{thread_local variable not permitted in a constexpr function}}
 
146
  return n;
 
147
}
 
148
constexpr int DisallowedStmtsCXX1Y_7() {
 
149
  //  - a definition of a variable for which no initialization is performed
 
150
  int n; // expected-error {{variables defined in a constexpr function must be initialized}}
 
151
  return 0;
 
152
}
 
153
 
94
154
constexpr int ForStmt() {
95
 
  for (int n = 0; n < 10; ++n) // expected-error {{statement not allowed in constexpr function}}
 
155
  for (int n = 0; n < 10; ++n)
 
156
#ifndef CXX1Y
 
157
  // expected-error@-2 {{statement not allowed in constexpr function}}
 
158
#endif
96
159
    return 0;
97
160
}
98
161
constexpr int VarDecl() {
99
 
  constexpr int a = 0; // expected-error {{variables cannot be declared in a constexpr function}}
100
 
  return 0;
101
 
}
 
162
  int a = 0;
 
163
#ifndef CXX1Y
 
164
  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
 
165
#endif
 
166
  return 0;
 
167
}
 
168
constexpr int ConstexprVarDecl() {
 
169
  constexpr int a = 0;
 
170
#ifndef CXX1Y
 
171
  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
 
172
#endif
 
173
  return 0;
 
174
}
 
175
constexpr int VarWithCtorDecl() {
 
176
  Literal a;
 
177
#ifndef CXX1Y
 
178
  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
 
179
#endif
 
180
  return 0;
 
181
}
 
182
NonLiteral nl;
 
183
constexpr NonLiteral &ExternNonLiteralVarDecl() {
 
184
  extern NonLiteral nl;
 
185
#ifndef CXX1Y
 
186
  // expected-error@-2 {{variable declaration in a constexpr function is a C++1y extension}}
 
187
#endif
 
188
  return nl;
 
189
}
 
190
static_assert(&ExternNonLiteralVarDecl() == &nl, "");
102
191
constexpr int FuncDecl() {
103
 
  constexpr int ForwardDecl(int); // expected-error {{statement not allowed in constexpr function}}
 
192
  constexpr int ForwardDecl(int);
 
193
#ifndef CXX1Y
 
194
  // expected-error@-2 {{use of this statement in a constexpr function is a C++1y extension}}
 
195
#endif
104
196
  return ForwardDecl(42);
105
197
}
106
198
constexpr int ClassDecl1() {
107
 
  typedef struct { } S1; // expected-error {{types cannot be defined in a constexpr function}}
 
199
  typedef struct { } S1;
 
200
#ifndef CXX1Y
 
201
  // expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
 
202
#endif
108
203
  return 0;
109
204
}
110
205
constexpr int ClassDecl2() {
111
 
  using S2 = struct { }; // expected-error {{types cannot be defined in a constexpr function}}
 
206
  using S2 = struct { };
 
207
#ifndef CXX1Y
 
208
  // expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
 
209
#endif
112
210
  return 0;
113
211
}
114
212
constexpr int ClassDecl3() {
115
 
  struct S3 { }; // expected-error {{types cannot be defined in a constexpr function}}
 
213
  struct S3 { };
 
214
#ifndef CXX1Y
 
215
  // expected-error@-2 {{type definition in a constexpr function is a C++1y extension}}
 
216
#endif
116
217
  return 0;
117
218
}
118
219
constexpr int NoReturn() {} // expected-error {{no return statement in constexpr function}}
119
220
constexpr int MultiReturn() {
120
 
  return 0; // expected-note {{return statement}}
121
 
  return 0; // expected-error {{multiple return statements in constexpr function}}
 
221
  return 0;
 
222
  return 0;
 
223
#ifndef CXX1Y
 
224
  // expected-error@-2 {{multiple return statements in constexpr function}}
 
225
  // expected-note@-4 {{return statement}}
 
226
#endif
122
227
}
123
228
 
124
229
//  - every constructor call and implicit conversion used in initializing the
153
258
    }
154
259
  }
155
260
}
 
261
 
 
262
namespace std_example {
 
263
  constexpr int square(int x) {
 
264
    return x * x;
 
265
  }
 
266
  constexpr long long_max() {
 
267
    return 2147483647;
 
268
  }
 
269
  constexpr int abs(int x) {
 
270
    if (x < 0)
 
271
#ifndef CXX1Y
 
272
      // expected-error@-2 {{C++1y}}
 
273
#endif
 
274
      x = -x;
 
275
    return x;
 
276
  }
 
277
  constexpr int first(int n) {
 
278
    static int value = n; // expected-error {{static variable not permitted}}
 
279
    return value;
 
280
  }
 
281
  constexpr int uninit() {
 
282
    int a; // expected-error {{must be initialized}}
 
283
    return a;
 
284
  }
 
285
  constexpr int prev(int x) {
 
286
    return --x;
 
287
  }
 
288
#ifndef CXX1Y
 
289
  // expected-error@-4 {{never produces a constant expression}}
 
290
  // expected-note@-4 {{subexpression}}
 
291
#endif
 
292
  constexpr int g(int x, int n) {
 
293
    int r = 1;
 
294
    while (--n > 0) r *= x;
 
295
    return r;
 
296
  }
 
297
#ifndef CXX1Y
 
298
    // expected-error@-5 {{C++1y}}
 
299
    // expected-error@-5 {{statement not allowed}}
 
300
#endif
 
301
}