xref: /aosp_15_r20/external/clang/test/Parser/cxx11-user-defined-literals.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -verify %s -fms-extensions -triple x86_64-apple-darwin9.0.0
2*67e74705SXin Li 
3*67e74705SXin Li // A ud-suffix cannot be used on string literals in a whole bunch of contexts:
4*67e74705SXin Li 
5*67e74705SXin Li #include "foo"_bar // expected-error {{expected "FILENAME" or <FILENAME>}}
6*67e74705SXin Li #line 1 "foo"_bar // expected-error {{user-defined suffix cannot be used here}}
7*67e74705SXin Li # 1 "foo"_bar 1 // expected-error {{user-defined suffix cannot be used here}}
8*67e74705SXin Li #ident "foo"_bar // expected-error {{user-defined suffix cannot be used here}}
9*67e74705SXin Li _Pragma("foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
10*67e74705SXin Li #pragma comment(lib, "foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
11*67e74705SXin Li _Pragma("comment(lib, \"foo\"_bar)") // expected-error {{user-defined suffix cannot be used here}}
12*67e74705SXin Li #pragma message "hi"_there // expected-error {{user-defined suffix cannot be used here}} expected-warning {{hi}}
13*67e74705SXin Li #pragma push_macro("foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
14*67e74705SXin Li #if __has_warning("-Wan-island-to-discover"_bar) // expected-error {{user-defined suffix cannot be used here}}
15*67e74705SXin Li #elif __has_include("foo"_bar) // expected-error {{expected "FILENAME" or <FILENAME>}}
16*67e74705SXin Li #endif
17*67e74705SXin Li 
18*67e74705SXin Li extern "C++"_x {} // expected-error {{user-defined suffix cannot be used here}}
19*67e74705SXin Li 
f()20*67e74705SXin Li int f() {
21*67e74705SXin Li   asm("mov %eax, %rdx"_foo); // expected-error {{user-defined suffix cannot be used here}}
22*67e74705SXin Li }
23*67e74705SXin Li 
24*67e74705SXin Li static_assert(true, "foo"_bar); // expected-error {{user-defined suffix cannot be used here}}
25*67e74705SXin Li 
26*67e74705SXin Li int cake() __attribute__((availability(macosx, unavailable, message = "is a lie"_x))); // expected-error {{user-defined suffix cannot be used here}}
27*67e74705SXin Li 
28*67e74705SXin Li // A ud-suffix cannot be used on character literals in preprocessor constant
29*67e74705SXin Li // expressions:
30*67e74705SXin Li #if 'x'_y - u'x'_z // expected-error 2{{character literal with user-defined suffix cannot be used in preprocessor constant expression}}
31*67e74705SXin Li #error error
32*67e74705SXin Li #endif
33*67e74705SXin Li 
34*67e74705SXin Li // A ud-suffix cannot be used on integer literals in preprocessor constant
35*67e74705SXin Li // expressions:
36*67e74705SXin Li #if 0_foo // expected-error {{integer literal with user-defined suffix cannot be used in preprocessor constant expression}}
37*67e74705SXin Li #error error
38*67e74705SXin Li #endif
39*67e74705SXin Li 
40*67e74705SXin Li // But they can appear in expressions.
operator ""_id(char c)41*67e74705SXin Li constexpr char operator"" _id(char c) { return c; }
operator ""_id(wchar_t c)42*67e74705SXin Li constexpr wchar_t operator"" _id(wchar_t c) { return c; }
operator ""_id(char16_t c)43*67e74705SXin Li constexpr char16_t operator"" _id(char16_t c) { return c; }
operator ""_id(char32_t c)44*67e74705SXin Li constexpr char32_t operator"" _id(char32_t c) { return c; }
45*67e74705SXin Li 
46*67e74705SXin Li using size_t = decltype(sizeof(int));
operator ""_id(const char * p,size_t n)47*67e74705SXin Li constexpr const char operator"" _id(const char *p, size_t n) { return *p; }
operator ""_id(const wchar_t * p,size_t n)48*67e74705SXin Li constexpr const wchar_t operator"" _id(const wchar_t *p, size_t n) { return *p; }
operator ""_id(const char16_t * p,size_t n)49*67e74705SXin Li constexpr const char16_t operator"" _id(const char16_t *p, size_t n) { return *p; }
operator ""_id(const char32_t * p,size_t n)50*67e74705SXin Li constexpr const char32_t operator"" _id(const char32_t *p, size_t n) { return *p; }
51*67e74705SXin Li 
operator ""_id(unsigned long long n)52*67e74705SXin Li constexpr unsigned long long operator"" _id(unsigned long long n) { return n; }
operator ""_id(long double d)53*67e74705SXin Li constexpr long double operator"" _id(long double d) { return d; }
54*67e74705SXin Li 
55*67e74705SXin Li template<int n> struct S {};
56*67e74705SXin Li S<"a"_id> sa;
57*67e74705SXin Li S<L"b"_id> sb;
58*67e74705SXin Li S<u8"c"_id> sc;
59*67e74705SXin Li S<u"d"_id> sd;
60*67e74705SXin Li S<U"e"_id> se;
61*67e74705SXin Li 
62*67e74705SXin Li S<'w'_id> sw;
63*67e74705SXin Li S<L'x'_id> sx;
64*67e74705SXin Li S<u'y'_id> sy;
65*67e74705SXin Li S<U'z'_id> sz;
66*67e74705SXin Li 
67*67e74705SXin Li S<100_id> sn;
68*67e74705SXin Li S<(int)1.3_id> sf;
69*67e74705SXin Li 
h()70*67e74705SXin Li void h() {
71*67e74705SXin Li   (void)"test"_id "test" L"test";
72*67e74705SXin Li }
73*67e74705SXin Li 
74*67e74705SXin Li // Test source location for suffix is known
75*67e74705SXin Li const char *p =
76*67e74705SXin Li   "foo\nbar" R"x(
77*67e74705SXin Li   erk
78*67e74705SXin Li   flux
79*67e74705SXin Li   )x" "eep\x1f"\
80*67e74705SXin Li _no_such_suffix // expected-error {{'operator""_no_such_suffix'}}
81*67e74705SXin Li "and a bit more"
82*67e74705SXin Li "and another suffix"_no_such_suffix;
83*67e74705SXin Li 
84*67e74705SXin Li char c =
85*67e74705SXin Li   '\x14'\
86*67e74705SXin Li _no_such_suffix; // expected-error {{'operator""_no_such_suffix'}}
87*67e74705SXin Li 
88*67e74705SXin Li int &r =
89*67e74705SXin Li 1234567\
90*67e74705SXin Li _no_such_suffix; // expected-error {{'operator""_no_such_suffix'}}
91*67e74705SXin Li 
92*67e74705SXin Li int k =
93*67e74705SXin Li 1234567.89\
94*67e74705SXin Li _no_such_suffix; // expected-error {{'operator""_no_such_suffix'}}
95*67e74705SXin Li 
96*67e74705SXin Li // Make sure we handle more interesting ways of writing a string literal which
97*67e74705SXin Li // is "" in translation phase 7.
98*67e74705SXin Li void operator "\
99*67e74705SXin Li " _foo(unsigned long long); // ok
100*67e74705SXin Li 
101*67e74705SXin Li void operator R"xyzzy()xyzzy" _foo(long double); // ok
102*67e74705SXin Li 
103*67e74705SXin Li void operator"" "" R"()" "" _foo(const char *); // ok
104*67e74705SXin Li 
105*67e74705SXin Li void operator ""_no_space(const char *); // ok
106*67e74705SXin Li 
107*67e74705SXin Li // Ensure we diagnose the bad cases.
108*67e74705SXin Li void operator "\0" _non_empty(const char *); // expected-error {{must be '""'}}
109*67e74705SXin Li void operator L"" _not_char(const char *); // expected-error {{cannot have an encoding prefix}}
110*67e74705SXin Li void operator "" ""
111*67e74705SXin Li U"" // expected-error {{cannot have an encoding prefix}}
112*67e74705SXin Li "" _also_not_char(const char *);
113*67e74705SXin Li void operator "" u8"" "\u0123" "hello"_all_of_the_things ""(const char*); // expected-error {{must be '""'}}
114*67e74705SXin Li 
115*67e74705SXin Li // Make sure we treat UCNs and UTF-8 as equivalent.
116*67e74705SXin Li int operator""_µs(unsigned long long) {} // expected-note {{previous}}
117*67e74705SXin Li int hundred_µs = 50_µs + 50_\u00b5s;
118*67e74705SXin Li int operator""_\u00b5s(unsigned long long) {} // expected-error {{redefinition of 'operator""_µs'}}
119*67e74705SXin Li 
120*67e74705SXin Li int operator""_\U0000212B(long double) {} // expected-note {{previous}}
121*67e74705SXin Li int hundred_Å = 50.0_Å + 50._\U0000212B;
122*67e74705SXin Li int operator""_Å(long double) {} // expected-error {{redefinition of 'operator""_Å'}}
123*67e74705SXin Li 
124*67e74705SXin Li int operator""_��(char) {} // expected-note {{previous}}
125*67e74705SXin Li int �� = '4'_�� + '2'_\U00010000;
126*67e74705SXin Li int operator""_\U00010000(char) {} // expected-error {{redefinition of 'operator""_��'}}
127*67e74705SXin Li 
128*67e74705SXin Li // These all declare the same function.
129*67e74705SXin Li int operator""_""_\u212e""_\U0000212e""(const char*, size_t);
130*67e74705SXin Li int operator""_\u212e""_\U0000212e""_""(const char*, size_t);
131*67e74705SXin Li int operator""_\U0000212e""_""_\u212e""(const char*, size_t);
132*67e74705SXin Li int mix_ucn_utf8 = ""_""_\u212e""_\U0000212e"";
133*67e74705SXin Li 
134*67e74705SXin Li void operator""_""_ℯ(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
135*67e74705SXin Li void operator""_""_\u212f(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
136*67e74705SXin Li void operator""_\u212e""_ℯ(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
137*67e74705SXin Li void operator""_\u212e""_\u212f(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
138*67e74705SXin Li 
139*67e74705SXin Li void operator""_""_℮(unsigned long long) {} // expected-note {{previous}}
140*67e74705SXin Li void operator""_\u212e""_\u212e(unsigned long long) {} // expected-error {{redefinition}}
141*67e74705SXin Li 
142*67e74705SXin Li #define ¢ *0.01 // expected-error {{macro name must be an identifier}}
143*67e74705SXin Li constexpr int operator""_¢(long double d) { return d * 100; } // expected-error {{non-ASCII}}
144*67e74705SXin Li constexpr int operator""_¢(unsigned long long n) { return n; } // expected-error {{non-ASCII}}
145*67e74705SXin Li static_assert(0.02_¢ == 2_¢, ""); // expected-error 2{{non-ASCII}}
146