Richard Thomson | 8930aab | 2016-03-27 16:43:44 +0000 | [diff] [blame^] | 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 | // FIXME: Handle L"", u8"", u"" and U"" literals. |
| 47 | if (!Literal->isAscii()) |
| 48 | return false; |
| 49 | |
| 50 | StringRef Bytes = Literal->getBytes(); |
| 51 | // Non-printing characters disqualify this literal: |
| 52 | // \007 = \a bell |
| 53 | // \010 = \b backspace |
| 54 | // \011 = \t horizontal tab |
| 55 | // \012 = \n new line |
| 56 | // \013 = \v vertical tab |
| 57 | // \014 = \f form feed |
| 58 | // \015 = \r carriage return |
| 59 | // \177 = delete |
| 60 | if (Bytes.find_first_of(StringRef("\000\001\002\003\004\005\006\a" |
| 61 | "\b\t\n\v\f\r\016\017" |
| 62 | "\020\021\022\023\024\025\026\027" |
| 63 | "\030\031\032\033\034\035\036\037" |
| 64 | "\177", |
| 65 | 33)) != StringRef::npos) |
| 66 | return false; |
| 67 | |
| 68 | CharSourceRange CharRange = Lexer::makeFileCharRange( |
| 69 | CharSourceRange::getTokenRange(Literal->getSourceRange()), |
| 70 | *Result.SourceManager, Result.Context->getLangOpts()); |
| 71 | StringRef Text = Lexer::getSourceText(CharRange, *Result.SourceManager, |
| 72 | Result.Context->getLangOpts()); |
| 73 | if (isRawStringLiteral(Text)) |
| 74 | return false; |
| 75 | |
| 76 | return containsEscapes(Text, R"('\"?x01)"); |
| 77 | } |
| 78 | |
| 79 | bool containsDelimiter(StringRef Bytes, const std::string &Delimiter) { |
| 80 | return Bytes.find(Delimiter.empty() |
| 81 | ? std::string(R"lit()")lit") |
| 82 | : (")" + Delimiter + R"(")")) != StringRef::npos; |
| 83 | } |
| 84 | |
| 85 | std::string asRawStringLiteral(const StringLiteral *Literal, |
| 86 | const std::string &DelimiterStem) { |
| 87 | const StringRef Bytes = Literal->getBytes(); |
| 88 | std::string Delimiter; |
| 89 | for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) { |
| 90 | Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I); |
| 91 | } |
| 92 | |
| 93 | if (Delimiter.empty()) |
| 94 | return (R"(R"()" + Bytes + R"lit()")lit").str(); |
| 95 | |
| 96 | return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str(); |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | |
| 101 | RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name, |
| 102 | ClangTidyContext *Context) |
| 103 | : ClangTidyCheck(Name, Context), |
| 104 | DelimiterStem(Options.get("DelimiterStem", "lit")) {} |
| 105 | |
| 106 | void RawStringLiteralCheck::storeOptions(ClangTidyOptions::OptionMap &Options) { |
| 107 | ClangTidyCheck::storeOptions(Options); |
| 108 | } |
| 109 | |
| 110 | void RawStringLiteralCheck::registerMatchers(MatchFinder *Finder) { |
| 111 | Finder->addMatcher(stringLiteral().bind("lit"), this); |
| 112 | } |
| 113 | |
| 114 | void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) { |
| 115 | // Raw string literals require C++11 or later. |
| 116 | if (!Result.Context->getLangOpts().CPlusPlus11) |
| 117 | return; |
| 118 | |
| 119 | const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("lit"); |
| 120 | if (Literal->getLocStart().isMacroID()) |
| 121 | return; |
| 122 | |
| 123 | if (containsEscapedCharacters(Result, Literal)) |
| 124 | replaceWithRawStringLiteral(Result, Literal); |
| 125 | } |
| 126 | |
| 127 | void RawStringLiteralCheck::replaceWithRawStringLiteral( |
| 128 | const MatchFinder::MatchResult &Result, const StringLiteral *Literal) { |
| 129 | CharSourceRange CharRange = Lexer::makeFileCharRange( |
| 130 | CharSourceRange::getTokenRange(Literal->getSourceRange()), |
| 131 | *Result.SourceManager, Result.Context->getLangOpts()); |
| 132 | diag(Literal->getLocStart(), |
| 133 | "escaped string literal can be written as a raw string literal") |
| 134 | << FixItHint::CreateReplacement( |
| 135 | CharRange, asRawStringLiteral(Literal, DelimiterStem)); |
| 136 | } |
| 137 | |
| 138 | } // namespace modernize |
| 139 | } // namespace tidy |
| 140 | } // namespace clang |