brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 7c90130 Raw
104 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "FasterStringFindCheck.h"10#include "../utils/OptionsUtils.h"11#include "clang/AST/ASTContext.h"12#include "clang/ASTMatchers/ASTMatchFinder.h"13#include "llvm/Support/raw_ostream.h"14#include <optional>15 16using namespace clang::ast_matchers;17 18namespace clang::tidy::performance {19 20static std::optional<std::string>21makeCharacterLiteral(const StringLiteral *Literal) {22  std::string Result;23  {24    llvm::raw_string_ostream OS(Result);25    Literal->outputString(OS);26  }27  // Now replace the " with '.28  auto OpenPos = Result.find_first_of('"');29  if (OpenPos == std::string::npos)30    return std::nullopt;31  Result[OpenPos] = '\'';32 33  auto ClosePos = Result.find_last_of('"');34  if (ClosePos == std::string::npos)35    return std::nullopt;36  Result[ClosePos] = '\'';37 38  // "'" is OK, but ''' is not, so add a backslash39  if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')40    Result.replace(OpenPos + 1, 1, "\\'");41 42  return Result;43}44 45namespace {46 47AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<Expr>,48                     hasSubstitutedType) {49  return hasType(qualType(anyOf(substTemplateTypeParmType(),50                                hasDescendant(substTemplateTypeParmType()))));51}52 53} // namespace54 55FasterStringFindCheck::FasterStringFindCheck(StringRef Name,56                                             ClangTidyContext *Context)57    : ClangTidyCheck(Name, Context),58      StringLikeClasses(utils::options::parseStringList(59          Options.get("StringLikeClasses",60                      "::std::basic_string;::std::basic_string_view"))) {}61 62void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {63  Options.store(Opts, "StringLikeClasses",64                utils::options::serializeStringList(StringLikeClasses));65}66 67void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) {68  const auto SingleChar =69      expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")));70  const auto StringFindFunctions =71      hasAnyName("find", "rfind", "find_first_of", "find_first_not_of",72                 "find_last_of", "find_last_not_of");73 74  Finder->addMatcher(75      cxxMemberCallExpr(76          callee(functionDecl(StringFindFunctions).bind("func")),77          anyOf(argumentCountIs(1), argumentCountIs(2)),78          hasArgument(0, SingleChar),79          on(expr(hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(80                      recordDecl(hasAnyName(StringLikeClasses)))))),81                  unless(hasSubstitutedType())))),82      this);83}84 85void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) {86  const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");87  const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");88 89  auto Replacement = makeCharacterLiteral(Literal);90  if (!Replacement)91    return;92 93  diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "94                               "a single character; consider using the more "95                               "effective overload accepting a character")96      << FindFunc97      << FixItHint::CreateReplacement(98             CharSourceRange::getTokenRange(Literal->getBeginLoc(),99                                            Literal->getEndLoc()),100             *Replacement);101}102 103} // namespace clang::tidy::performance104