clang-tools  7.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 
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->getLocStart().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->getLocStart(),
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->getLocStart(),
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
llvm::StringRef Name
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
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.
Definition: ClangTidy.h:127
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&#39;s name.
Definition: ClangTidy.cpp:427
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
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.