clang-tools 10.0.0

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
9 #include "RawStringLiteralCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace modernize {
19
20 namespace {
21
22 bool containsEscapes(StringRef HayStack, StringRef Escapes) {
23 size_t BackSlash = HayStack.find('\\');
24 if (BackSlash == StringRef::npos)
25 return false;
26
27 while (BackSlash != StringRef::npos) {
28 if (Escapes.find(HayStack[BackSlash + 1]) == StringRef::npos)
29 return false;
30 BackSlash = HayStack.find('\\', BackSlash + 2);
31 }
32
33 return true;
34 }
35
36 bool isRawStringLiteral(StringRef Text) {
37 // Already a raw string literal if R comes before ".
38 const size_t QuotePos = Text.find('"');
39 assert(QuotePos != StringRef::npos);
40 return (QuotePos > 0) && (Text[QuotePos - 1] == 'R');
41 }
42
43 bool containsEscapedCharacters(const MatchFinder::MatchResult &Result,
44 const StringLiteral *Literal,
45 const CharsBitSet &DisallowedChars) {
46 // FIXME: Handle L"", u8"", u"" and U"" literals.
47 if (!Literal->isAscii())
48 return false;
49
50 for (const unsigned char C : Literal->getBytes())
51 if (DisallowedChars.test(C))
52 return false;
53
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))
60 return false;
61
62 return containsEscapes(Text, R"('\"?x01)");
63 }
64
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;
69 }
70
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);
77 }
78
79 if (Delimiter.empty())
80 return (R"(R"()" + Bytes + R"lit()")lit").str();
81
82 return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
83 }
84
85 } // namespace
86
87 RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name,
88 ClangTidyContext *Context)
89 : ClangTidyCheck(Name, Context),
90 DelimiterStem(Options.get("DelimiterStem", "lit")),
91 ReplaceShorterLiterals(Options.get("ReplaceShorterLiterals", false)) {
92 // Non-printing characters are disallowed:
93 // \007 = \a bell
94 // \010 = \b backspace
95 // \011 = \t horizontal tab
96 // \012 = \n new line
97 // \013 = \v vertical tab
98 // \014 = \f form feed
99 // \015 = \r carriage return
100 // \177 = delete
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"
105 "\177",
106 33))
107 DisallowedChars.set(C);
108
109 // Non-ASCII are disallowed too.
110 for (unsigned int C = 0x80u; C <= 0xFFu; ++C)
111 DisallowedChars.set(static_cast<unsigned char>(C));
112 }
113
114 void RawStringLiteralCheck::storeOptions(ClangTidyOptions::OptionMap &Options) {
115 ClangTidyCheck::storeOptions(Options);
116 this->Options.store(Options, "ReplaceShorterLiterals",
117 ReplaceShorterLiterals);
118 }
119
120 void RawStringLiteralCheck::registerMatchers(MatchFinder *Finder) {
121 // Raw string literals require C++11 or later.
122 if (!getLangOpts().CPlusPlus11)
123 return;
124
125 Finder->addMatcher(
126 stringLiteral(unless(hasParent(predefinedExpr()))).bind("lit"), this);
127 }
128
129 void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
130 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("lit");
131 if (Literal->getBeginLoc().isMacroID())
132 return;
133
134 if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
135 std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
136 if (ReplaceShorterLiterals ||
137 Replacement.length() <=
138 Lexer::MeasureTokenLength(Literal->getBeginLoc(),
139 *Result.SourceManager, getLangOpts()))
140 replaceWithRawStringLiteral(Result, Literal, Replacement);
141 }
142 }
143
144 void RawStringLiteralCheck::replaceWithRawStringLiteral(
145 const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
146 StringRef Replacement) {
147 CharSourceRange CharRange = Lexer::makeFileCharRange(
148 CharSourceRange::getTokenRange(Literal->getSourceRange()),
149 *Result.SourceManager, getLangOpts());
150 diag(Literal->getBeginLoc(),
151 "escaped string literal can be written as a raw string literal")
152 << FixItHint::CreateReplacement(CharRange, Replacement);
153 }
154
155 } // namespace modernize
156 } // namespace tidy
157 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
RawStringLiteralCheck.h
clang::tidy::modernize::RawStringLiteralCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: RawStringLiteralCheck.cpp:129
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::modernize::CharsBitSet
std::bitset< 1<< CHAR_BIT > CharsBitSet
Definition: RawStringLiteralCheck.h:19
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::clangd::getTokenRange
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
clang::tidy::modernize::RawStringLiteralCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
Definition: RawStringLiteralCheck.cpp:114
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::ClangTidyCheck::storeOptions
virtual void storeOptions(ClangTidyOptions::OptionMap &Options)
Should store all options supported by this check with their current values or default values for opti...
Definition: ClangTidyCheck.h:101
clang::tidy::modernize::RawStringLiteralCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: RawStringLiteralCheck.cpp:120