| 1 | // RUN: %check_clang_tidy %s modernize-raw-string-literal %t -- -config="{CheckOptions: {modernize-raw-string-literal.ReplaceShorterLiterals: true}}" |
| 2 | |
| 3 | char const *const BackSlash("goink\\frob" ); |
| 4 | // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: escaped string literal can be written as a raw string literal [modernize-raw-string-literal] |
| 5 | // CHECK-FIXES: {{^}}char const *const BackSlash(R"(goink\frob)");{{$}} |
| 6 | |
| 7 | char const *const PlainLiteral("plain literal" ); |
| 8 | |
| 9 | // Non-printable ASCII characters. |
| 10 | char const *const Nul("goink\\\000" ); |
| 11 | char const *const Soh("goink\\\001" ); |
| 12 | char const *const Stx("goink\\\002" ); |
| 13 | char const *const Etx("goink\\\003" ); |
| 14 | char const *const Enq("goink\\\004" ); |
| 15 | char const *const Ack("goink\\\005" ); |
| 16 | char const *const Bell("goink\\\afrob" ); |
| 17 | char const *const BackSpace("goink\\\bfrob" ); |
| 18 | char const *const HorizontalTab("goink\\\tfrob" ); |
| 19 | char const *const NewLine("goink\nfrob" ); |
| 20 | char const *const VerticalTab("goink\\\vfrob" ); |
| 21 | char const *const FormFeed("goink\\\ffrob" ); |
| 22 | char const *const CarraigeReturn("goink\\\rfrob" ); |
| 23 | char const *const So("goink\\\016" ); |
| 24 | char const *const Si("goink\\\017" ); |
| 25 | char const *const Dle("goink\\\020" ); |
| 26 | char const *const Dc1("goink\\\021" ); |
| 27 | char const *const Dc2("goink\\\022" ); |
| 28 | char const *const Dc3("goink\\\023" ); |
| 29 | char const *const Dc4("goink\\\024" ); |
| 30 | char const *const Nak("goink\\\025" ); |
| 31 | char const *const Syn("goink\\\026" ); |
| 32 | char const *const Etb("goink\\\027" ); |
| 33 | char const *const Can("goink\\\030" ); |
| 34 | char const *const Em("goink\\\031" ); |
| 35 | char const *const Sub("goink\\\032" ); |
| 36 | char const *const Esc("goink\\\033" ); |
| 37 | char const *const Fs("goink\\\034" ); |
| 38 | char const *const Gs("goink\\\035" ); |
| 39 | char const *const Rs("goink\\\036" ); |
| 40 | char const *const Us("goink\\\037" ); |
| 41 | char const *const HexNonPrintable("\\\x03" ); |
| 42 | char const *const Delete("\\\177" ); |
| 43 | char const *const MultibyteSnowman("\xE2\x98\x83" ); |
| 44 | // CHECK-FIXES: {{^}}char const *const MultibyteSnowman("\xE2\x98\x83");{{$}} |
| 45 | |
| 46 | char const *const TrailingSpace("A line \\with space. \n" ); |
| 47 | char const *const TrailingNewLine("A single \\line.\n" ); |
| 48 | char const *const AlreadyRaw(R"(foobie\\bletch)" ); |
| 49 | auto const *const UTF8Literal(u8"foobie\\bletch" ); |
| 50 | auto const *const UTF8RawLiteral(u8R"(foobie\\bletch)" ); |
| 51 | // TODO: enable these tests once all supported compilers |
| 52 | // support char16_t and char32_t (VS2013 does not) |
| 53 | // char16_t const *const UTF16Literal(u"foobie\\bletch"); |
| 54 | // char16_t const *const UTF16RawLiteral(uR"(foobie\\bletch)"); |
| 55 | // char32_t const *const UTF32Literal(U"foobie\\bletch"); |
| 56 | // char32_t const *const UTF32RawLiteral(UR"(foobie\\bletch)"); |
| 57 | wchar_t const *const WideLiteral(L"foobie\\bletch" ); |
| 58 | wchar_t const *const WideRawLiteral(LR"(foobie\\bletch)" ); |
| 59 | |
| 60 | char const *const SingleQuote("goink\'frob" ); |
| 61 | // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: {{.*}} can be written as a raw string literal |
| 62 | // CHECK-XFIXES: {{^}}char const *const SingleQuote(R"(goink'frob)");{{$}} |
| 63 | |
| 64 | char const *const DoubleQuote("goink\"frob" ); |
| 65 | // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: {{.*}} can be written as a raw string literal |
| 66 | // CHECK-FIXES: {{^}}char const *const DoubleQuote(R"(goink"frob)");{{$}} |
| 67 | |
| 68 | char const *const QuestionMark("goink\?frob" ); |
| 69 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: {{.*}} can be written as a raw string literal |
| 70 | // CHECK-FIXES: {{^}}char const *const QuestionMark(R"(goink?frob)");{{$}} |
| 71 | |
| 72 | char const *const RegEx("goink\\(one|two\\)\\\\\\?.*\\nfrob" ); |
| 73 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: {{.*}} can be written as a raw string literal |
| 74 | // CHECK-FIXES: {{^}}char const *const RegEx(R"(goink\(one|two\)\\\?.*\nfrob)");{{$}} |
| 75 | |
| 76 | char const *const Path("C:\\Program Files\\Vendor\\Application\\Application.exe" ); |
| 77 | // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: {{.*}} can be written as a raw string literal |
| 78 | // CHECK-FIXES: {{^}}char const *const Path(R"(C:\Program Files\Vendor\Application\Application.exe)");{{$}} |
| 79 | |
| 80 | char const *const ContainsSentinel("who\\ops)\"" ); |
| 81 | // CHECK-MESSAGES: :[[@LINE-1]]:36: warning: {{.*}} can be written as a raw string literal |
| 82 | // CHECK-FIXES: {{^}}char const *const ContainsSentinel(R"lit(who\ops)")lit");{{$}} |
| 83 | |
| 84 | char const *const ContainsDelim("whoops)\")lit\"" ); |
| 85 | // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: {{.*}} can be written as a raw string literal |
| 86 | // CHECK-FIXES: {{^}}char const *const ContainsDelim(R"lit1(whoops)")lit")lit1");{{$}} |
| 87 | |
| 88 | char const *const OctalPrintable("\100\\" ); |
| 89 | // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: {{.*}} can be written as a raw string literal |
| 90 | // CHECK-FIXES: {{^}}char const *const OctalPrintable(R"(@\)");{{$}} |
| 91 | |
| 92 | char const *const HexPrintable("\x40\\" ); |
| 93 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: {{.*}} can be written as a raw string literal |
| 94 | // CHECK-FIXES: {{^}}char const *const HexPrintable(R"(@\)");{{$}} |
| 95 | |
| 96 | char const *const prettyFunction(__PRETTY_FUNCTION__); |
| 97 | char const *const function(__FUNCTION__); |
| 98 | char const *const func(__func__); |
| 99 | |
| 100 | #define TRICK(arg_) #arg_ |
| 101 | char const *const MacroBody = TRICK(foo\\bar); |
| 102 | |
| 103 | #define HAT(rabbit_) #rabbit_ "foo\\bar" |
| 104 | char const *const StringizedMacroArgument = HAT(foo\\bar); |
| 105 | |
| 106 | #define SUBST(lit_) lit_ |
| 107 | char const *const MacroArgument = SUBST("foo\\bar" ); |
| 108 | // FIXME: We should be able to replace this string literal macro argument |
| 109 | |
| 110 | template <typename T> |
| 111 | void fn(char const *const Arg) { |
| 112 | char const *const Str("foo\\bar" ); |
| 113 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: {{.*}} can be written as a raw string literal |
| 114 | // CHECK-FIXES: {{^}} char const *const Str(R"(foo\bar)");{{$}} |
| 115 | } |
| 116 | |
| 117 | template <> |
| 118 | void fn<int>(char const *const Arg) { |
| 119 | char const *const Str("foo\\bar" ); |
| 120 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: {{.*}} can be written as a raw string literal |
| 121 | // CHECK-FIXES: {{^}} char const *const Str(R"(foo\bar)");{{$}} |
| 122 | } |
| 123 | |
| 124 | void callFn() { |
| 125 | fn<int>(Arg: "foo\\bar" ); |
| 126 | // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}} can be written as a raw string literal |
| 127 | // CHECK-FIXES: {{^}} fn<int>(R"(foo\bar)");{{$}} |
| 128 | fn<double>(Arg: "foo\\bar" ); |
| 129 | // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}} can be written as a raw string literal |
| 130 | // CHECK-FIXES: {{^}} fn<double>(R"(foo\bar)");{{$}} |
| 131 | } |
| 132 | |
| 133 | namespace std { |
| 134 | using size_t = decltype(sizeof(0)); |
| 135 | namespace ud { |
| 136 | int operator""_abc (const char *str, std::size_t len); |
| 137 | } // namespace ud |
| 138 | } // namespace std |
| 139 | namespace gh97243 { |
| 140 | using namespace std::ud; |
| 141 | auto UserDefinedLiteral = "foo\\bar"_abc ; |
| 142 | // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: {{.*}} can be written as a raw string literal |
| 143 | // CHECK-FIXES: {{^}}auto UserDefinedLiteral = R"(foo\bar)"_abc; |
| 144 | } // namespace gh97243 |
| 145 | |