2016-08-04 07:06:03 +08:00
|
|
|
//===--- InefficientStringConcatenationCheck.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-08-04 07:06:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InefficientStringConcatenationCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace performance {
|
|
|
|
|
|
|
|
void InefficientStringConcatenationCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "StrictMode", StrictMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
InefficientStringConcatenationCheck::InefficientStringConcatenationCheck(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
2017-05-29 21:59:27 +08:00
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
StrictMode(Options.getLocalOrGlobal("StrictMode", 0)) {}
|
2016-08-04 07:06:03 +08:00
|
|
|
|
|
|
|
void InefficientStringConcatenationCheck::registerMatchers(
|
|
|
|
MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto BasicStringType =
|
2017-08-02 21:13:11 +08:00
|
|
|
hasType(qualType(hasUnqualifiedDesugaredType(recordType(
|
|
|
|
hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))));
|
2016-08-04 07:06:03 +08:00
|
|
|
|
|
|
|
const auto BasicStringPlusOperator = cxxOperatorCallExpr(
|
|
|
|
hasOverloadedOperatorName("+"),
|
|
|
|
hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))));
|
|
|
|
|
|
|
|
const auto PlusOperator =
|
|
|
|
cxxOperatorCallExpr(
|
|
|
|
hasOverloadedOperatorName("+"),
|
|
|
|
hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))),
|
|
|
|
hasDescendant(BasicStringPlusOperator))
|
|
|
|
.bind("plusOperator");
|
|
|
|
|
|
|
|
const auto AssignOperator = cxxOperatorCallExpr(
|
|
|
|
hasOverloadedOperatorName("="),
|
|
|
|
hasArgument(0, declRefExpr(BasicStringType,
|
|
|
|
hasDeclaration(decl().bind("lhsStrT")))
|
|
|
|
.bind("lhsStr")),
|
|
|
|
hasArgument(1, stmt(hasDescendant(declRefExpr(
|
|
|
|
hasDeclaration(decl(equalsBoundNode("lhsStrT"))))))),
|
|
|
|
hasDescendant(BasicStringPlusOperator));
|
|
|
|
|
|
|
|
if (StrictMode) {
|
|
|
|
Finder->addMatcher(cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator)),
|
|
|
|
this);
|
|
|
|
} else {
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxOperatorCallExpr(anyOf(AssignOperator, PlusOperator),
|
|
|
|
hasAncestor(stmt(anyOf(cxxForRangeStmt(),
|
|
|
|
whileStmt(), forStmt())))),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InefficientStringConcatenationCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *LhsStr = Result.Nodes.getNodeAs<DeclRefExpr>("lhsStr");
|
|
|
|
const auto *PlusOperator =
|
|
|
|
Result.Nodes.getNodeAs<CXXOperatorCallExpr>("plusOperator");
|
|
|
|
const auto DiagMsg =
|
|
|
|
"string concatenation results in allocation of unnecessary temporary "
|
|
|
|
"strings; consider using 'operator+=' or 'string::append()' instead";
|
|
|
|
|
|
|
|
if (LhsStr)
|
|
|
|
diag(LhsStr->getExprLoc(), DiagMsg);
|
|
|
|
else if (PlusOperator)
|
|
|
|
diag(PlusOperator->getExprLoc(), DiagMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace performance
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|