2016-02-13 03:28:14 +08:00
|
|
|
//===--- FasterStringFindCheck.cpp - clang-tidy----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-02-13 03:28:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FasterStringFindCheck.h"
|
2016-05-10 23:31:15 +08:00
|
|
|
#include "../utils/OptionsUtils.h"
|
2016-02-13 03:28:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace performance {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
llvm::Optional<std::string> MakeCharacterLiteral(const StringLiteral *Literal) {
|
|
|
|
std::string Result;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
|
|
|
Literal->outputString(OS);
|
|
|
|
}
|
|
|
|
// Now replace the " with '.
|
|
|
|
auto pos = Result.find_first_of('"');
|
2016-11-08 15:50:19 +08:00
|
|
|
if (pos == Result.npos)
|
|
|
|
return llvm::None;
|
2016-02-13 03:28:14 +08:00
|
|
|
Result[pos] = '\'';
|
|
|
|
pos = Result.find_last_of('"');
|
2016-11-08 15:50:19 +08:00
|
|
|
if (pos == Result.npos)
|
|
|
|
return llvm::None;
|
2016-02-13 03:28:14 +08:00
|
|
|
Result[pos] = '\'';
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<Expr>,
|
|
|
|
hasSubstitutedType) {
|
|
|
|
return hasType(qualType(anyOf(substTemplateTypeParmType(),
|
|
|
|
hasDescendant(substTemplateTypeParmType()))));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
FasterStringFindCheck::FasterStringFindCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2016-05-10 23:31:15 +08:00
|
|
|
StringLikeClasses(utils::options::parseStringList(
|
2016-11-08 15:50:19 +08:00
|
|
|
Options.get("StringLikeClasses", "std::basic_string"))) {}
|
2016-02-13 03:28:14 +08:00
|
|
|
|
|
|
|
void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "StringLikeClasses",
|
2016-05-10 23:31:15 +08:00
|
|
|
utils::options::serializeStringList(StringLikeClasses));
|
2016-02-13 03:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto SingleChar =
|
2016-05-18 03:36:09 +08:00
|
|
|
expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")));
|
2016-02-13 03:28:14 +08:00
|
|
|
const auto StringFindFunctions =
|
2017-04-25 00:41:00 +08:00
|
|
|
hasAnyName("find", "rfind", "find_first_of", "find_first_not_of",
|
|
|
|
"find_last_of", "find_last_not_of");
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxMemberCallExpr(
|
|
|
|
callee(functionDecl(StringFindFunctions).bind("func")),
|
|
|
|
anyOf(argumentCountIs(1), argumentCountIs(2)),
|
|
|
|
hasArgument(0, SingleChar),
|
2017-08-02 21:13:11 +08:00
|
|
|
on(expr(
|
|
|
|
hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
|
|
|
|
recordDecl(hasAnyName(SmallVector<StringRef, 4>(
|
|
|
|
StringLikeClasses.begin(), StringLikeClasses.end()))))))),
|
|
|
|
unless(hasSubstitutedType())))),
|
2017-04-25 00:41:00 +08:00
|
|
|
this);
|
2016-02-13 03:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");
|
|
|
|
const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
|
|
|
|
|
|
|
|
auto Replacement = MakeCharacterLiteral(Literal);
|
|
|
|
if (!Replacement)
|
|
|
|
return;
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "
|
2016-02-13 03:28:14 +08:00
|
|
|
"a single character; consider using the more "
|
|
|
|
"effective overload accepting a character")
|
2018-08-10 06:42:26 +08:00
|
|
|
<< FindFunc
|
|
|
|
<< FixItHint::CreateReplacement(
|
|
|
|
CharSourceRange::getTokenRange(Literal->getBeginLoc(),
|
2018-08-10 06:43:02 +08:00
|
|
|
Literal->getEndLoc()),
|
2018-08-10 06:42:26 +08:00
|
|
|
*Replacement);
|
2016-02-13 03:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace performance
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|