xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/cxx11-user-defined-literals.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -verify %s -fms-extensions -triple x86_64-apple-darwin9.0.0
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // A ud-suffix cannot be used on string literals in a whole bunch of contexts:
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc #include "foo"_bar // expected-error {{expected "FILENAME" or <FILENAME>}}
6*f4a2713aSLionel Sambuc #line 1 "foo"_bar // expected-error {{user-defined suffix cannot be used here}}
7*f4a2713aSLionel Sambuc # 1 "foo"_bar 1 // expected-error {{user-defined suffix cannot be used here}}
8*f4a2713aSLionel Sambuc #ident "foo"_bar // expected-error {{user-defined suffix cannot be used here}}
9*f4a2713aSLionel Sambuc _Pragma("foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
10*f4a2713aSLionel Sambuc #pragma comment(lib, "foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
11*f4a2713aSLionel Sambuc _Pragma("comment(lib, \"foo\"_bar)") // expected-error {{user-defined suffix cannot be used here}}
12*f4a2713aSLionel Sambuc #pragma message "hi"_there // expected-error {{user-defined suffix cannot be used here}} expected-warning {{hi}}
13*f4a2713aSLionel Sambuc #pragma push_macro("foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
14*f4a2713aSLionel Sambuc #if __has_warning("-Wan-island-to-discover"_bar) // expected-error {{user-defined suffix cannot be used here}}
15*f4a2713aSLionel Sambuc #elif __has_include("foo"_bar) // expected-error {{expected "FILENAME" or <FILENAME>}}
16*f4a2713aSLionel Sambuc #endif
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc extern "C++"_x {} // expected-error {{user-defined suffix cannot be used here}} expected-error {{unknown linkage language}}
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc int f() {
21*f4a2713aSLionel Sambuc   asm("mov %eax, %rdx"_foo); // expected-error {{user-defined suffix cannot be used here}}
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc static_assert(true, "foo"_bar); // expected-error {{user-defined suffix cannot be used here}}
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc int cake() __attribute__((availability(macosx, unavailable, message = "is a lie"_x))); // expected-error {{user-defined suffix cannot be used here}}
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc // A ud-suffix cannot be used on character literals in preprocessor constant
29*f4a2713aSLionel Sambuc // expressions:
30*f4a2713aSLionel Sambuc #if 'x'_y - u'x'_z // expected-error 2{{character literal with user-defined suffix cannot be used in preprocessor constant expression}}
31*f4a2713aSLionel Sambuc #error error
32*f4a2713aSLionel Sambuc #endif
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc // A ud-suffix cannot be used on integer literals in preprocessor constant
35*f4a2713aSLionel Sambuc // expressions:
36*f4a2713aSLionel Sambuc #if 0_foo // expected-error {{integer literal with user-defined suffix cannot be used in preprocessor constant expression}}
37*f4a2713aSLionel Sambuc #error error
38*f4a2713aSLionel Sambuc #endif
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // But they can appear in expressions.
41*f4a2713aSLionel Sambuc constexpr char operator"" _id(char c) { return c; }
42*f4a2713aSLionel Sambuc constexpr wchar_t operator"" _id(wchar_t c) { return c; }
43*f4a2713aSLionel Sambuc constexpr char16_t operator"" _id(char16_t c) { return c; }
44*f4a2713aSLionel Sambuc constexpr char32_t operator"" _id(char32_t c) { return c; }
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc using size_t = decltype(sizeof(int));
47*f4a2713aSLionel Sambuc constexpr const char operator"" _id(const char *p, size_t n) { return *p; }
48*f4a2713aSLionel Sambuc constexpr const wchar_t operator"" _id(const wchar_t *p, size_t n) { return *p; }
49*f4a2713aSLionel Sambuc constexpr const char16_t operator"" _id(const char16_t *p, size_t n) { return *p; }
50*f4a2713aSLionel Sambuc constexpr const char32_t operator"" _id(const char32_t *p, size_t n) { return *p; }
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc constexpr unsigned long long operator"" _id(unsigned long long n) { return n; }
53*f4a2713aSLionel Sambuc constexpr long double operator"" _id(long double d) { return d; }
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc template<int n> struct S {};
56*f4a2713aSLionel Sambuc S<"a"_id> sa;
57*f4a2713aSLionel Sambuc S<L"b"_id> sb;
58*f4a2713aSLionel Sambuc S<u8"c"_id> sc;
59*f4a2713aSLionel Sambuc S<u"d"_id> sd;
60*f4a2713aSLionel Sambuc S<U"e"_id> se;
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc S<'w'_id> sw;
63*f4a2713aSLionel Sambuc S<L'x'_id> sx;
64*f4a2713aSLionel Sambuc S<u'y'_id> sy;
65*f4a2713aSLionel Sambuc S<U'z'_id> sz;
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc S<100_id> sn;
68*f4a2713aSLionel Sambuc S<(int)1.3_id> sf;
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc void h() {
71*f4a2713aSLionel Sambuc   (void)"test"_id "test" L"test";
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc // Test source location for suffix is known
75*f4a2713aSLionel Sambuc const char *p =
76*f4a2713aSLionel Sambuc   "foo\nbar" R"x(
77*f4a2713aSLionel Sambuc   erk
78*f4a2713aSLionel Sambuc   flux
79*f4a2713aSLionel Sambuc   )x" "eep\x1f"\
80*f4a2713aSLionel Sambuc _no_such_suffix // expected-error {{'operator "" _no_such_suffix'}}
81*f4a2713aSLionel Sambuc "and a bit more"
82*f4a2713aSLionel Sambuc "and another suffix"_no_such_suffix;
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc char c =
85*f4a2713aSLionel Sambuc   '\x14'\
86*f4a2713aSLionel Sambuc _no_such_suffix; // expected-error {{'operator "" _no_such_suffix'}}
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc int &r =
89*f4a2713aSLionel Sambuc 1234567\
90*f4a2713aSLionel Sambuc _no_such_suffix; // expected-error {{'operator "" _no_such_suffix'}}
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc int k =
93*f4a2713aSLionel Sambuc 1234567.89\
94*f4a2713aSLionel Sambuc _no_such_suffix; // expected-error {{'operator "" _no_such_suffix'}}
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc // Make sure we handle more interesting ways of writing a string literal which
97*f4a2713aSLionel Sambuc // is "" in translation phase 7.
98*f4a2713aSLionel Sambuc void operator "\
99*f4a2713aSLionel Sambuc " _foo(unsigned long long); // ok
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc void operator R"xyzzy()xyzzy" _foo(long double); // ok
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc void operator"" "" R"()" "" _foo(const char *); // ok
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc void operator ""_no_space(const char *); // ok
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc // Ensure we diagnose the bad cases.
108*f4a2713aSLionel Sambuc void operator "\0" _non_empty(const char *); // expected-error {{must be '""'}}
109*f4a2713aSLionel Sambuc void operator L"" _not_char(const char *); // expected-error {{cannot have an encoding prefix}}
110*f4a2713aSLionel Sambuc void operator "" ""
111*f4a2713aSLionel Sambuc U"" // expected-error {{cannot have an encoding prefix}}
112*f4a2713aSLionel Sambuc "" _also_not_char(const char *);
113*f4a2713aSLionel Sambuc void operator "" u8"" "\u0123" "hello"_all_of_the_things ""(const char*); // expected-error {{must be '""'}}
114