10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
22 bool containsEscapes(StringRef HayStack, StringRef Escapes) {
23 size_t BackSlash = HayStack.find(
'\\');
24 if (BackSlash == StringRef::npos)
27 while (BackSlash != StringRef::npos) {
28 if (Escapes.find(HayStack[BackSlash + 1]) == StringRef::npos)
30 BackSlash = HayStack.find(
'\\', BackSlash + 2);
36 bool isRawStringLiteral(StringRef Text) {
38 const size_t QuotePos =
Text.find(
'"');
39 assert(QuotePos != StringRef::npos);
40 return (QuotePos > 0) && (
Text[QuotePos - 1] ==
'R');
43 bool containsEscapedCharacters(
const MatchFinder::MatchResult &Result,
44 const StringLiteral *Literal,
47 if (!Literal->isAscii())
50 for (
const unsigned char C : Literal->getBytes())
51 if (DisallowedChars.test(C))
54 CharSourceRange CharRange = Lexer::makeFileCharRange(
55 CharSourceRange::getTokenRange(Literal->getSourceRange()),
56 *Result.SourceManager, Result.Context->getLangOpts());
57 StringRef
Text = Lexer::getSourceText(CharRange, *Result.SourceManager,
58 Result.Context->getLangOpts());
59 if (isRawStringLiteral(Text))
62 return containsEscapes(Text, R
"('\"?x01)");
65 bool containsDelimiter(StringRef Bytes,
const std::string &Delimiter) {
66 return Bytes.find(Delimiter.empty()
67 ? std::string(R
"lit()")lit")
68 : (")" + Delimiter + R
"(")")) != StringRef::npos;
71 std::string asRawStringLiteral(const StringLiteral *Literal,
72 const std::string &DelimiterStem) {
73 const StringRef Bytes = Literal->getBytes();
74 std::string Delimiter;
75 for (
int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
76 Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
79 if (Delimiter.empty())
80 return (R
"(R"()" + Bytes + R"lit()")lit").str();
82 return (R
"(R")" + Delimiter + "(" + Bytes +
")" + Delimiter + R
"(")").str();
87 RawStringLiteralCheck::RawStringLiteralCheck(StringRef
Name,
90 DelimiterStem(Options.get(
"DelimiterStem",
"lit")),
91 ReplaceShorterLiterals(Options.get(
"ReplaceShorterLiterals", false)) {
101 for (
const unsigned char C : StringRef(
"\000\001\002\003\004\005\006\a"
102 "\b\t\n\v\f\r\016\017"
103 "\020\021\022\023\024\025\026\027"
104 "\030\031\032\033\034\035\036\037"
107 DisallowedChars.set(C);
110 for (
unsigned int C = 0x80u; C <= 0xFFu; ++C)
111 DisallowedChars.set(static_cast<unsigned char>(C));
116 Options.
store(Opts,
"ReplaceShorterLiterals", ReplaceShorterLiterals);
121 stringLiteral(unless(hasParent(predefinedExpr()))).bind(
"lit"),
this);
125 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"lit");
126 if (Literal->getBeginLoc().isMacroID())
129 if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
130 std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
131 if (ReplaceShorterLiterals ||
132 Replacement.length() <=
133 Lexer::MeasureTokenLength(Literal->getBeginLoc(),
135 replaceWithRawStringLiteral(Result, Literal, Replacement);
139 void RawStringLiteralCheck::replaceWithRawStringLiteral(
140 const MatchFinder::MatchResult &Result,
const StringLiteral *Literal,
141 StringRef Replacement) {
142 CharSourceRange CharRange = Lexer::makeFileCharRange(
143 CharSourceRange::getTokenRange(Literal->getSourceRange()),
145 diag(Literal->getBeginLoc(),
146 "escaped string literal can be written as a raw string literal")
147 << FixItHint::CreateReplacement(CharRange, Replacement);