11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 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,
48 if (!Literal->isAscii())
51 for (
const unsigned char C : Literal->getBytes())
52 if (DisallowedChars.test(C))
55 CharSourceRange CharRange = Lexer::makeFileCharRange(
56 CharSourceRange::getTokenRange(Literal->getSourceRange()),
57 *Result.SourceManager, Result.Context->getLangOpts());
58 StringRef Text = Lexer::getSourceText(CharRange, *Result.SourceManager,
59 Result.Context->getLangOpts());
60 if (isRawStringLiteral(Text))
63 return containsEscapes(Text, R
"('\"?x01)"); 66 bool containsDelimiter(StringRef Bytes,
const std::string &Delimiter) {
67 return Bytes.find(Delimiter.empty()
68 ? std::string(R
"lit()")lit") 69 : (")" + Delimiter + R
"(")")) != StringRef::npos; 72 std::string asRawStringLiteral(const StringLiteral *Literal,
73 const std::string &DelimiterStem) {
74 const StringRef Bytes = Literal->getBytes();
75 std::string Delimiter;
76 for (
int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
77 Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
80 if (Delimiter.empty())
81 return (R
"(R"()" + Bytes + R"lit()")lit").str(); 83 return (R
"(R")" + Delimiter + "(" + Bytes +
")" + Delimiter + R
"(")").str(); 88 RawStringLiteralCheck::RawStringLiteralCheck(StringRef
Name,
91 DelimiterStem(Options.get(
"DelimiterStem",
"lit")),
92 ReplaceShorterLiterals(Options.get(
"ReplaceShorterLiterals", false)) {
102 for (
const unsigned char C : StringRef(
"\000\001\002\003\004\005\006\a" 103 "\b\t\n\v\f\r\016\017" 104 "\020\021\022\023\024\025\026\027" 105 "\030\031\032\033\034\035\036\037" 108 DisallowedChars.set(C);
111 for (
unsigned int C = 0x80u; C <= 0xFFu; ++C)
112 DisallowedChars.set(static_cast<unsigned char>(C));
117 this->Options.store(Options,
"ReplaceShorterLiterals",
118 ReplaceShorterLiterals);
127 stringLiteral(unless(hasParent(predefinedExpr()))).bind(
"lit"),
this);
131 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"lit");
132 if (Literal->getLocStart().isMacroID())
135 if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
136 std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
137 if (ReplaceShorterLiterals ||
138 Replacement.length() <=
139 Lexer::MeasureTokenLength(Literal->getLocStart(),
141 replaceWithRawStringLiteral(Result, Literal, Replacement);
145 void RawStringLiteralCheck::replaceWithRawStringLiteral(
146 const MatchFinder::MatchResult &Result,
const StringLiteral *Literal,
147 StringRef Replacement) {
148 CharSourceRange CharRange = Lexer::makeFileCharRange(
149 CharSourceRange::getTokenRange(Literal->getSourceRange()),
151 diag(Literal->getLocStart(),
152 "escaped string literal can be written as a raw string literal")
153 << FixItHint::CreateReplacement(CharRange, Replacement);
LangOptions getLangOpts() const
Returns the language options from the context.
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::bitset< 1<< CHAR_BIT > CharsBitSet
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...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.