11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
23 bool containsEscapes(StringRef HayStack, StringRef Escapes) {
24 size_t BackSlash = HayStack.find(
'\\');
25 if (BackSlash == StringRef::npos)
28 while (BackSlash != StringRef::npos) {
29 if (Escapes.find(HayStack[BackSlash + 1]) == StringRef::npos)
31 BackSlash = HayStack.find(
'\\', BackSlash + 2);
37 bool isRawStringLiteral(StringRef Text) {
39 const size_t QuotePos = Text.find(
'"');
40 assert(QuotePos != StringRef::npos);
41 return (QuotePos > 0) && (Text[QuotePos - 1] ==
'R');
44 bool containsEscapedCharacters(
const MatchFinder::MatchResult &
Result,
45 const StringLiteral *Literal) {
47 if (!Literal->isAscii())
50 StringRef Bytes = Literal->getBytes();
60 if (Bytes.find_first_of(StringRef(
"\000\001\002\003\004\005\006\a"
61 "\b\t\n\v\f\r\016\017"
62 "\020\021\022\023\024\025\026\027"
63 "\030\031\032\033\034\035\036\037"
65 33)) != StringRef::npos)
68 CharSourceRange CharRange = Lexer::makeFileCharRange(
69 CharSourceRange::getTokenRange(Literal->getSourceRange()),
70 *Result.SourceManager, Result.Context->getLangOpts());
71 StringRef Text = Lexer::getSourceText(CharRange, *Result.SourceManager,
72 Result.Context->getLangOpts());
73 if (isRawStringLiteral(Text))
76 return containsEscapes(Text, R
"('\"?x01)");
79 bool containsDelimiter(StringRef Bytes,
const std::string &Delimiter) {
80 return Bytes.find(Delimiter.empty()
81 ? std::string(R
"lit()")lit")
82 : (")" + Delimiter + R
"(")")) != StringRef::npos;
85 std::string asRawStringLiteral(const StringLiteral *Literal,
86 const std::string &DelimiterStem) {
87 const StringRef Bytes = Literal->getBytes();
88 std::string Delimiter;
89 for (
int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
90 Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
93 if (Delimiter.empty())
94 return (R
"(R"()" + Bytes + R"lit()")lit").str();
96 return (R
"(R")" + Delimiter + "(" + Bytes +
")" + Delimiter + R
"(")").str();
101 RawStringLiteralCheck::RawStringLiteralCheck(StringRef
Name,
104 DelimiterStem(Options.get(
"DelimiterStem",
"lit")) {}
112 stringLiteral(unless(hasParent(predefinedExpr()))).bind(
"lit"),
this);
117 if (!Result.Context->getLangOpts().CPlusPlus11)
120 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"lit");
121 if (Literal->getLocStart().isMacroID())
124 if (containsEscapedCharacters(Result, Literal))
125 replaceWithRawStringLiteral(Result, Literal);
128 void RawStringLiteralCheck::replaceWithRawStringLiteral(
129 const MatchFinder::MatchResult &Result,
const StringLiteral *Literal) {
130 CharSourceRange CharRange = Lexer::makeFileCharRange(
131 CharSourceRange::getTokenRange(Literal->getSourceRange()),
132 *Result.SourceManager, Result.Context->getLangOpts());
133 diag(Literal->getLocStart(),
134 "escaped string literal can be written as a raw string literal")
135 << FixItHint::CreateReplacement(
136 CharRange, asRawStringLiteral(Literal, DelimiterStem));
std::unique_ptr< ast_matchers::MatchFinder > Finder
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
std::map< std::string, std::string > OptionMap
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
virtual void storeOptions(ClangTidyOptions::OptionMap &Options)
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.