xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/static-assert.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -triple=x86_64-linux-gnu
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc int f(); // expected-note {{declared here}}
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc static_assert(f(), "f"); // expected-error {{static_assert expression is not an integral constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
6f4a2713aSLionel Sambuc static_assert(true, "true is not false");
7f4a2713aSLionel Sambuc static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
8f4a2713aSLionel Sambuc 
g()9f4a2713aSLionel Sambuc void g() {
10f4a2713aSLionel Sambuc     static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
11f4a2713aSLionel Sambuc }
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc class C {
14f4a2713aSLionel Sambuc     static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
15f4a2713aSLionel Sambuc };
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc template<int N> struct T {
18f4a2713aSLionel Sambuc     static_assert(N == 2, "N is not 2!"); // expected-error {{static_assert failed "N is not 2!"}}
19f4a2713aSLionel Sambuc };
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}}
22f4a2713aSLionel Sambuc T<2> t2;
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc template<typename T> struct S {
25f4a2713aSLionel Sambuc     static_assert(sizeof(T) > sizeof(char), "Type not big enough!"); // expected-error {{static_assert failed "Type not big enough!"}}
26f4a2713aSLionel Sambuc };
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc S<char> s1; // expected-note {{in instantiation of template class 'S<char>' requested here}}
29f4a2713aSLionel Sambuc S<int> s2;
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc static_assert(false, L"\xFFFFFFFF"); // expected-error {{static_assert failed L"\xFFFFFFFF"}}
32f4a2713aSLionel Sambuc static_assert(false, u"\U000317FF"); // expected-error {{static_assert failed u"\U000317FF"}}
33f4a2713aSLionel Sambuc // FIXME: render this as u8"\u03A9"
34f4a2713aSLionel Sambuc static_assert(false, u8"Ω"); // expected-error {{static_assert failed u8"\316\251"}}
35f4a2713aSLionel Sambuc static_assert(false, L"\u1234"); // expected-error {{static_assert failed L"\x1234"}}
36f4a2713aSLionel Sambuc static_assert(false, L"\x1ff" "0\x123" "fx\xfffff" "goop"); // expected-error {{static_assert failed L"\x1FF""0\x123""fx\xFFFFFgoop"}}
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc template<typename T> struct AlwaysFails {
39f4a2713aSLionel Sambuc   // Only give one error here.
40f4a2713aSLionel Sambuc   static_assert(false, ""); // expected-error {{static_assert failed}}
41f4a2713aSLionel Sambuc };
42f4a2713aSLionel Sambuc AlwaysFails<int> alwaysFails;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc template<typename T> struct StaticAssertProtected {
45f4a2713aSLionel Sambuc   static_assert(__is_literal(T), ""); // expected-error {{static_assert failed}}
46f4a2713aSLionel Sambuc   static constexpr T t = {}; // no error here
47f4a2713aSLionel Sambuc };
48f4a2713aSLionel Sambuc struct X { ~X(); };
49f4a2713aSLionel Sambuc StaticAssertProtected<int> sap1;
50f4a2713aSLionel Sambuc StaticAssertProtected<X> sap2; // expected-note {{instantiation}}
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc static_assert(true); // expected-warning {{C++1z extension}}
53*0a6a1f1dSLionel Sambuc static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}}
54