clang-tools 8.0.0

RawStringLiteralCheck.cpp
Go to the documentation of this file.
1 //===--- RawStringLiteralCheck.cpp - clang-tidy----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "RawStringLiteralCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace modernize {
20
21 namespace {
22
23 bool containsEscapes(StringRef HayStack, StringRef Escapes) {
24 size_t BackSlash = HayStack.find('\\');
25 if (BackSlash == StringRef::npos)
26 return false;
27
28 while (BackSlash != StringRef::npos) {
29 if (Escapes.find(HayStack[BackSlash + 1]) == StringRef::npos)
30 return false;
31 BackSlash = HayStack.find('\\', BackSlash + 2);
32 }
33
34 return true;
35 }
36
37 bool isRawStringLiteral(StringRef Text) {
38 // Already a raw string literal if R comes before ".
39 const size_t QuotePos = Text.find('"');
40 assert(QuotePos != StringRef::npos);
41 return (QuotePos > 0) && (Text[QuotePos - 1] == 'R');
42 }
43
44 bool containsEscapedCharacters(const MatchFinder::MatchResult &Result,
45 const StringLiteral *Literal,
46 const CharsBitSet &DisallowedChars) {
47 // FIXME: Handle L"", u8"", u"" and U"" literals.
48 if (!Literal->isAscii())
49 return false;
50
51 for (const unsigned char C : Literal->getBytes())
52 if (DisallowedChars.test(C))
53 return false;
54
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))
61 return false;
62
63 return containsEscapes(Text, R"('\"?x01)");
64 }
65
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;
70 }
71
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);
78 }
79
80 if (Delimiter.empty())
81 return (R"(R"()" + Bytes + R"lit()")lit").str();
82
83 return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
84 }
85
86 } // namespace
87
88 RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name,
89 ClangTidyContext *Context)
90 : ClangTidyCheck(Name, Context),
91 DelimiterStem(Options.get("DelimiterStem", "lit")),
92 ReplaceShorterLiterals(Options.get("ReplaceShorterLiterals", false)) {
93 // Non-printing characters are disallowed:
94 // \007 = \a bell
95 // \010 = \b backspace
96 // \011 = \t horizontal tab
97 // \012 = \n new line
98 // \013 = \v vertical tab
99 // \014 = \f form feed
100 // \015 = \r carriage return
101 // \177 = delete
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"
106 "\177",
107 33))
108 DisallowedChars.set(C);
109
110 // Non-ASCII are disallowed too.
111 for (unsigned int C = 0x80u; C <= 0xFFu; ++C)
112 DisallowedChars.set(static_cast<unsigned char>(C));
113 }
114
115 void RawStringLiteralCheck::storeOptions(ClangTidyOptions::OptionMap &Options) {
116 ClangTidyCheck::storeOptions(Options);
117 this->Options.store(Options, "ReplaceShorterLiterals",
118 ReplaceShorterLiterals);
119 }
120
121 void RawStringLiteralCheck::registerMatchers(MatchFinder *Finder) {
122 // Raw string literals require C++11 or later.
123 if (!getLangOpts().CPlusPlus11)
124 return;
125
126 Finder->addMatcher(
127 stringLiteral(unless(hasParent(predefinedExpr()))).bind("lit"), this);
128 }
129
130 void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
131 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("lit");
132 if (Literal->getBeginLoc().isMacroID())
133 return;
134
135 if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
136 std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
137 if (ReplaceShorterLiterals ||
138 Replacement.length() <=
139 Lexer::MeasureTokenLength(Literal->getBeginLoc(),
140 *Result.SourceManager, getLangOpts()))
141 replaceWithRawStringLiteral(Result, Literal, Replacement);
142 }
143 }
144
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()),
150 *Result.SourceManager, getLangOpts());
151 diag(Literal->getBeginLoc(),
152 "escaped string literal can be written as a raw string literal")
153 << FixItHint::CreateReplacement(CharRange, Replacement);
154 }
155
156 } // namespace modernize
157 } // namespace tidy
158 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
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:130
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::modernize::CharsBitSet
std::bitset< 1<< CHAR_BIT > CharsBitSet
Definition: RawStringLiteralCheck.h:20
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
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:115
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:100
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: ClangTidy.cpp:438
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: ClangTidy.h:174
clang::tidy::modernize::RawStringLiteralCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: RawStringLiteralCheck.cpp:121