[clang-tidy] Use hasLocalStorage() to identify unnecessary copy initializations to exclude static local variables.

Summary: Since local static variables can outlive other local variables exclude them from the unnecessary copy initialization check.

Reviewers: alexfh

Patch by Felix Berger!

Differential Revision: http://reviews.llvm.org/D15822

llvm-svn: 256636
This commit is contained in:
Alexander Kornienko 2015-12-30 11:35:50 +00:00
parent 72a96c66b2
commit 8d228dda41
2 changed files with 5 additions and 2 deletions

View File

@ -19,7 +19,6 @@ namespace performance {
using namespace ::clang::ast_matchers;
namespace {
AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
AST_MATCHER(QualType, isPointerType) { return Node->isPointerType(); }
} // namespace
@ -43,7 +42,7 @@ void UnnecessaryCopyInitialization::registerMatchers(
unless(callee(cxxMethodDecl())));
Finder->addMatcher(
varDecl(
isLocalVarDecl(), hasType(isConstQualified()),
hasLocalStorage(), hasType(isConstQualified()),
hasType(matchers::isExpensiveToCopy()),
hasInitializer(cxxConstructExpr(
hasDeclaration(cxxConstructorDecl(isCopyConstructor())),

View File

@ -110,6 +110,10 @@ void NegativeFunctionCallTrivialType() {
const TrivialToCopyType VarCopyConstructed(TrivialTypeReference());
}
void NegativeStaticLocalVar(const ExpensiveToCopyType &Obj) {
static const auto StaticVar = Obj.reference();
}
void NegativeFunctionCallExpensiveTypeNonConstVariable() {
auto AutoAssigned = ExpensiveTypeReference();
auto AutoCopyConstructed(ExpensiveTypeReference());