cxx11-user-defined-literals.cpp 6.3 KB

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