blob: d3188f8f43ebd04a35cf401746949be3b2a39037 [file] [log] [blame]
Richard Smithd67aea22012-03-06 03:21:47 +00001// RUN: %clang_cc1 -std=c++11 -verify %s -fms-extensions -triple x86_64-apple-darwin9.0.0
2
3// A ud-suffix cannot be used on string literals in a whole bunch of contexts:
4
5#include "foo"_bar // expected-error {{expected "FILENAME" or <FILENAME>}}
6#line 1 "foo"_bar // expected-error {{user-defined suffix cannot be used here}}
7# 1 "foo"_bar 1 // expected-error {{user-defined suffix cannot be used here}}
8#ident "foo"_bar // expected-error {{user-defined suffix cannot be used here}}
9_Pragma("foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
10#pragma comment(lib, "foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
11_Pragma("comment(lib, \"foo\"_bar)") // expected-error {{user-defined suffix cannot be used here}}
12#pragma message "hi"_there // expected-error {{user-defined suffix cannot be used here}} expected-warning {{hi}}
13#pragma push_macro("foo"_bar) // expected-error {{user-defined suffix cannot be used here}}
14#if __has_warning("-Wan-island-to-discover"_bar) // expected-error {{user-defined suffix cannot be used here}}
15#elif __has_include("foo"_bar) // expected-error {{expected "FILENAME" or <FILENAME>}}
16#endif
17
Richard Smith4ee696d2014-02-17 23:25:27 +000018extern "C++"_x {} // expected-error {{user-defined suffix cannot be used here}}
Richard Smithd67aea22012-03-06 03:21:47 +000019
20int f() {
21 asm("mov %eax, %rdx"_foo); // expected-error {{user-defined suffix cannot be used here}}
22}
23
24static_assert(true, "foo"_bar); // expected-error {{user-defined suffix cannot be used here}}
25
26int cake() __attribute__((availability(macosx, unavailable, message = "is a lie"_x))); // expected-error {{user-defined suffix cannot be used here}}
27
28// A ud-suffix cannot be used on character literals in preprocessor constant
29// expressions:
30#if 'x'_y - u'x'_z // expected-error 2{{character literal with user-defined suffix cannot be used in preprocessor constant expression}}
31#error error
32#endif
33
Richard Smith39570d002012-03-08 08:45:32 +000034// A ud-suffix cannot be used on integer literals in preprocessor constant
35// expressions:
36#if 0_foo // expected-error {{integer literal with user-defined suffix cannot be used in preprocessor constant expression}}
37#error error
38#endif
39
Richard Smithd67aea22012-03-06 03:21:47 +000040// But they can appear in expressions.
41constexpr char operator"" _id(char c) { return c; }
42constexpr wchar_t operator"" _id(wchar_t c) { return c; }
43constexpr char16_t operator"" _id(char16_t c) { return c; }
44constexpr char32_t operator"" _id(char32_t c) { return c; }
45
46using size_t = decltype(sizeof(int));
47constexpr const char operator"" _id(const char *p, size_t n) { return *p; }
48constexpr const wchar_t operator"" _id(const wchar_t *p, size_t n) { return *p; }
49constexpr const char16_t operator"" _id(const char16_t *p, size_t n) { return *p; }
50constexpr const char32_t operator"" _id(const char32_t *p, size_t n) { return *p; }
51
Richard Smith39570d002012-03-08 08:45:32 +000052constexpr unsigned long long operator"" _id(unsigned long long n) { return n; }
53constexpr long double operator"" _id(long double d) { return d; }
54
Richard Smithd67aea22012-03-06 03:21:47 +000055template<int n> struct S {};
Richard Smithc67fdd42012-03-07 08:35:16 +000056S<"a"_id> sa;
57S<L"b"_id> sb;
58S<u8"c"_id> sc;
59S<u"d"_id> sd;
60S<U"e"_id> se;
Richard Smithd67aea22012-03-06 03:21:47 +000061
62S<'w'_id> sw;
63S<L'x'_id> sx;
64S<u'y'_id> sy;
65S<U'z'_id> sz;
66
Richard Smith39570d002012-03-08 08:45:32 +000067S<100_id> sn;
68S<(int)1.3_id> sf;
69
Richard Smithd67aea22012-03-06 03:21:47 +000070void h() {
71 (void)"test"_id "test" L"test";
72}
Richard Smithc67fdd42012-03-07 08:35:16 +000073
Richard Smith75b67d62012-03-08 01:34:56 +000074// Test source location for suffix is known
75const char *p =
76 "foo\nbar" R"x(
77 erk
78 flux
79 )x" "eep\x1f"\
Richard Smithe87aeb32015-10-08 00:17:59 +000080_no_such_suffix // expected-error {{'operator""_no_such_suffix'}}
Richard Smith75b67d62012-03-08 01:34:56 +000081"and a bit more"
82"and another suffix"_no_such_suffix;
83
Richard Smith75b67d62012-03-08 01:34:56 +000084char c =
85 '\x14'\
Richard Smithe87aeb32015-10-08 00:17:59 +000086_no_such_suffix; // expected-error {{'operator""_no_such_suffix'}}
Richard Smith39570d002012-03-08 08:45:32 +000087
88int &r =
891234567\
Richard Smithe87aeb32015-10-08 00:17:59 +000090_no_such_suffix; // expected-error {{'operator""_no_such_suffix'}}
Richard Smith39570d002012-03-08 08:45:32 +000091
92int k =
931234567.89\
Richard Smithe87aeb32015-10-08 00:17:59 +000094_no_such_suffix; // expected-error {{'operator""_no_such_suffix'}}
Richard Smith7d182a72012-03-08 23:06:02 +000095
96// Make sure we handle more interesting ways of writing a string literal which
97// is "" in translation phase 7.
98void operator "\
99" _foo(unsigned long long); // ok
100
101void operator R"xyzzy()xyzzy" _foo(long double); // ok
102
103void operator"" "" R"()" "" _foo(const char *); // ok
104
Richard Smith6f212062012-10-20 08:41:10 +0000105void operator ""_no_space(const char *); // ok
106
Richard Smith7d182a72012-03-08 23:06:02 +0000107// Ensure we diagnose the bad cases.
108void operator "\0" _non_empty(const char *); // expected-error {{must be '""'}}
Richard Smith7d182a72012-03-08 23:06:02 +0000109void operator L"" _not_char(const char *); // expected-error {{cannot have an encoding prefix}}
110void operator "" ""
111U"" // expected-error {{cannot have an encoding prefix}}
112"" _also_not_char(const char *);
113void operator "" u8"" "\u0123" "hello"_all_of_the_things ""(const char*); // expected-error {{must be '""'}}
Richard Smith8b7258b2014-02-17 21:52:30 +0000114
115// Make sure we treat UCNs and UTF-8 as equivalent.
116int operator""_µs(unsigned long long) {} // expected-note {{previous}}
117int hundred_µs = 50_µs + 50_\u00b5s;
Richard Smithe87aeb32015-10-08 00:17:59 +0000118int operator""_\u00b5s(unsigned long long) {} // expected-error {{redefinition of 'operator""_µs'}}
Richard Smith8b7258b2014-02-17 21:52:30 +0000119
120int operator""_\U0000212B(long double) {} // expected-note {{previous}}
121int hundred_ = 50.0_ + 50._\U0000212B;
Richard Smithe87aeb32015-10-08 00:17:59 +0000122int operator""_Å(long double) {} // expected-error {{redefinition of 'operator""_Å'}}
Richard Smith8b7258b2014-02-17 21:52:30 +0000123
124int operator""_𐀀(char) {} // expected-note {{previous}}
125int 𐀀 = '4'_𐀀 + '2'_\U00010000;
Richard Smithe87aeb32015-10-08 00:17:59 +0000126int operator""_\U00010000(char) {} // expected-error {{redefinition of 'operator""_𐀀'}}
Richard Smith8b7258b2014-02-17 21:52:30 +0000127
128// These all declare the same function.
129int operator""_""_\u212e""_\U0000212e""(const char*, size_t);
130int operator""_\u212e""_\U0000212e""_""(const char*, size_t);
131int operator""_\U0000212e""_""_\u212e""(const char*, size_t);
132int mix_ucn_utf8 = ""_""_\u212e""_\U0000212e"";
133
134void operator""_""_ℯ(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
135void operator""_""_\u212f(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
136void operator""_\u212e""_ℯ(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
137void operator""_\u212e""_\u212f(unsigned long long) {} // expected-error {{differing user-defined suffixes ('_℮' and '_ℯ') in string literal concatenation}}
138
139void operator""_""_℮(unsigned long long) {} // expected-note {{previous}}
140void operator""_\u212e""_\u212e(unsigned long long) {} // expected-error {{redefinition}}
141
Alp Tokerb05e0b52014-05-21 06:13:51 +0000142#define ¢ *0.01 // expected-error {{macro name must be an identifier}}
Richard Smith8b7258b2014-02-17 21:52:30 +0000143constexpr int operator""_¢(long double d) { return d * 100; } // expected-error {{non-ASCII}}
144constexpr int operator""_¢(unsigned long long n) { return n; } // expected-error {{non-ASCII}}
145static_assert(0.02_¢ == 2_¢, ""); // expected-error 2{{non-ASCII}}