1// RUN: %check_clang_tidy %s modernize-raw-string-literal %t -- -config="{CheckOptions: {modernize-raw-string-literal.ReplaceShorterLiterals: true}}"
2
3char 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
7char const *const PlainLiteral("plain literal");
8
9// Non-printable ASCII characters.
10char const *const Nul("goink\\\000");
11char const *const Soh("goink\\\001");
12char const *const Stx("goink\\\002");
13char const *const Etx("goink\\\003");
14char const *const Enq("goink\\\004");
15char const *const Ack("goink\\\005");
16char const *const Bell("goink\\\afrob");
17char const *const BackSpace("goink\\\bfrob");
18char const *const HorizontalTab("goink\\\tfrob");
19char const *const NewLine("goink\nfrob");
20char const *const VerticalTab("goink\\\vfrob");
21char const *const FormFeed("goink\\\ffrob");
22char const *const CarraigeReturn("goink\\\rfrob");
23char const *const So("goink\\\016");
24char const *const Si("goink\\\017");
25char const *const Dle("goink\\\020");
26char const *const Dc1("goink\\\021");
27char const *const Dc2("goink\\\022");
28char const *const Dc3("goink\\\023");
29char const *const Dc4("goink\\\024");
30char const *const Nak("goink\\\025");
31char const *const Syn("goink\\\026");
32char const *const Etb("goink\\\027");
33char const *const Can("goink\\\030");
34char const *const Em("goink\\\031");
35char const *const Sub("goink\\\032");
36char const *const Esc("goink\\\033");
37char const *const Fs("goink\\\034");
38char const *const Gs("goink\\\035");
39char const *const Rs("goink\\\036");
40char const *const Us("goink\\\037");
41char const *const HexNonPrintable("\\\x03");
42char const *const Delete("\\\177");
43char const *const MultibyteSnowman("\xE2\x98\x83");
44// CHECK-FIXES: {{^}}char const *const MultibyteSnowman("\xE2\x98\x83");{{$}}
45
46char const *const TrailingSpace("A line \\with space. \n");
47char const *const TrailingNewLine("A single \\line.\n");
48char const *const AlreadyRaw(R"(foobie\\bletch)");
49auto const *const UTF8Literal(u8"foobie\\bletch");
50auto 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)");
57wchar_t const *const WideLiteral(L"foobie\\bletch");
58wchar_t const *const WideRawLiteral(LR"(foobie\\bletch)");
59
60char 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
64char 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
68char 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
72char 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
76char 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
80char 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
84char 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
88char 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
92char 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
96char const *const prettyFunction(__PRETTY_FUNCTION__);
97char const *const function(__FUNCTION__);
98char const *const func(__func__);
99
100#define TRICK(arg_) #arg_
101char const *const MacroBody = TRICK(foo\\bar);
102
103#define HAT(rabbit_) #rabbit_ "foo\\bar"
104char const *const StringizedMacroArgument = HAT(foo\\bar);
105
106#define SUBST(lit_) lit_
107char const *const MacroArgument = SUBST("foo\\bar");
108// FIXME: We should be able to replace this string literal macro argument
109
110template <typename T>
111void 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
117template <>
118void 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
124void 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

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/raw-string-literal.cpp