
Applied fixes enabled by the LLVM's .clang-tidy configs. Reverted files where fixes introduced compile errors: clang-tools-extra/clang-tidy/hicpp/NoAssemblerCheck.cpp clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp $ clang-tools-extra/clang-tidy/tool/run-clang-tidy.py -fix clang-tools-extra/clang-tidy/ Enabled checks: llvm-else-after-return llvm-header-guard llvm-include-order llvm-namespace-comment llvm-prefer-isa-or-dyn-cast-in-conditionals llvm-prefer-register-over-unsigned llvm-qualified-auto llvm-twine-local misc-definitions-in-headers misc-misplaced-const misc-new-delete-overloads misc-no-recursion misc-non-copyable-objects misc-redundant-expression misc-static-assert misc-throw-by-value-catch-by-reference misc-unconventional-assign-operator misc-uniqueptr-reset-release misc-unused-alias-decls misc-unused-using-decls readability-identifier-naming Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D95614
85 lines
3.1 KiB
C++
85 lines
3.1 KiB
C++
//===--- InefficientStringConcatenationCheck.cpp - clang-tidy--------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#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)
|
|
: ClangTidyCheck(Name, Context),
|
|
StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
|
|
|
|
void InefficientStringConcatenationCheck::registerMatchers(
|
|
MatchFinder *Finder) {
|
|
const auto BasicStringType =
|
|
hasType(qualType(hasUnqualifiedDesugaredType(recordType(
|
|
hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))));
|
|
|
|
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 char *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
|