clang-tools 20.0.0git
RawStringLiteralCheck.cpp
Go to the documentation of this file.
1//===--- RawStringLiteralCheck.cpp - clang-tidy----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::modernize {
17
18namespace {
19
20bool containsEscapes(StringRef HayStack, StringRef Escapes) {
21 size_t BackSlash = HayStack.find('\\');
22 if (BackSlash == StringRef::npos)
23 return false;
24
25 while (BackSlash != StringRef::npos) {
26 if (!Escapes.contains(HayStack[BackSlash + 1]))
27 return false;
28 BackSlash = HayStack.find('\\', BackSlash + 2);
29 }
30
31 return true;
32}
33
34bool isRawStringLiteral(StringRef Text) {
35 // Already a raw string literal if R comes before ".
36 const size_t QuotePos = Text.find('"');
37 assert(QuotePos != StringRef::npos);
38 return (QuotePos > 0) && (Text[QuotePos - 1] == 'R');
39}
40
41bool containsEscapedCharacters(const MatchFinder::MatchResult &Result,
42 const StringLiteral *Literal,
43 const CharsBitSet &DisallowedChars) {
44 // FIXME: Handle L"", u8"", u"" and U"" literals.
45 if (!Literal->isOrdinary())
46 return false;
47
48 for (const unsigned char C : Literal->getBytes())
49 if (DisallowedChars.test(C))
50 return false;
51
52 CharSourceRange CharRange = Lexer::makeFileCharRange(
53 CharSourceRange::getTokenRange(Literal->getSourceRange()),
54 *Result.SourceManager, Result.Context->getLangOpts());
55 StringRef Text = Lexer::getSourceText(CharRange, *Result.SourceManager,
56 Result.Context->getLangOpts());
57 if (Text.empty() || isRawStringLiteral(Text))
58 return false;
59
60 return containsEscapes(Text, R"('\"?x01)");
61}
62
63bool containsDelimiter(StringRef Bytes, const std::string &Delimiter) {
64 return Bytes.find(Delimiter.empty()
65 ? std::string(R"lit()")lit")
66 : (")" + Delimiter + R"(")")) != StringRef::npos;
67}
68
69std::string asRawStringLiteral(const StringLiteral *Literal,
70 const std::string &DelimiterStem) {
71 const StringRef Bytes = Literal->getBytes();
72 std::string Delimiter;
73 for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
74 Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
75 }
76
77 if (Delimiter.empty())
78 return (R"(R"()" + Bytes + R"lit()")lit").str();
79
80 return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
81}
82
83} // namespace
84
86 ClangTidyContext *Context)
87 : ClangTidyCheck(Name, Context),
88 DelimiterStem(Options.get("DelimiterStem", "lit")),
89 ReplaceShorterLiterals(Options.get("ReplaceShorterLiterals", false)) {
90 // Non-printing characters are disallowed:
91 // \007 = \a bell
92 // \010 = \b backspace
93 // \011 = \t horizontal tab
94 // \012 = \n new line
95 // \013 = \v vertical tab
96 // \014 = \f form feed
97 // \015 = \r carriage return
98 // \177 = delete
99 for (const unsigned char C : StringRef("\000\001\002\003\004\005\006\a"
100 "\b\t\n\v\f\r\016\017"
101 "\020\021\022\023\024\025\026\027"
102 "\030\031\032\033\034\035\036\037"
103 "\177",
104 33))
105 DisallowedChars.set(C);
106
107 // Non-ASCII are disallowed too.
108 for (unsigned int C = 0x80U; C <= 0xFFU; ++C)
109 DisallowedChars.set(static_cast<unsigned char>(C));
110}
111
113 Options.store(Opts, "DelimiterStem", DelimiterStem);
114 Options.store(Opts, "ReplaceShorterLiterals", ReplaceShorterLiterals);
115}
116
118 Finder->addMatcher(
119 stringLiteral(unless(hasParent(predefinedExpr()))).bind("lit"), this);
120}
121
122void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
123 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("lit");
124 if (Literal->getBeginLoc().isMacroID())
125 return;
126
127 if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
128 std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
129 if (ReplaceShorterLiterals ||
130 Replacement.length() <=
131 Lexer::MeasureTokenLength(Literal->getBeginLoc(),
132 *Result.SourceManager, getLangOpts()))
133 replaceWithRawStringLiteral(Result, Literal, Replacement);
134 }
135}
136
137void RawStringLiteralCheck::replaceWithRawStringLiteral(
138 const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
139 StringRef Replacement) {
140 CharSourceRange CharRange = Lexer::makeFileCharRange(
141 CharSourceRange::getTokenRange(Literal->getSourceRange()),
142 *Result.SourceManager, getLangOpts());
143 diag(Literal->getBeginLoc(),
144 "escaped string literal can be written as a raw string literal")
145 << FixItHint::CreateReplacement(CharRange, Replacement);
146}
147
148} // namespace clang::tidy::modernize
llvm::SmallString< 256U > Name
const Criteria C
std::string Text
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
RawStringLiteralCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
std::bitset< 1<< CHAR_BIT > CharsBitSet
llvm::StringMap< ClangTidyValue > OptionMap