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(
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 this->Options.store(Options,
"ReplaceShorterLiterals",
117 ReplaceShorterLiterals);
126 stringLiteral(unless(hasParent(predefinedExpr()))).bind(
"lit"),
this);
130 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"lit");
131 if (Literal->getBeginLoc().isMacroID())
134 if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
135 std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
136 if (ReplaceShorterLiterals ||
137 Replacement.length() <=
138 Lexer::MeasureTokenLength(Literal->getBeginLoc(),
140 replaceWithRawStringLiteral(Result, Literal, Replacement);
144 void RawStringLiteralCheck::replaceWithRawStringLiteral(
145 const MatchFinder::MatchResult &Result,
const StringLiteral *Literal,
146 StringRef Replacement) {
147 CharSourceRange CharRange = Lexer::makeFileCharRange(
150 diag(Literal->getBeginLoc(),
151 "escaped string literal can be written as a raw string literal")
152 << FixItHint::CreateReplacement(CharRange, Replacement);
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.
const LangOptions & getLangOpts() const
Returns the language options from the context.
std::bitset< 1<< CHAR_BIT > CharsBitSet
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
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.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.