diff options
author | Alexander Kornienko <alexfh@google.com> | 2015-12-30 10:24:40 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2015-12-30 10:24:40 +0000 |
commit | b959f4c33882b1f5150bb54a1664befad4251af8 (patch) | |
tree | 62369d8b12b8d3c3cc8e91e29816205cc8099d2b /clang-tools-extra/test/clang-tidy/performance-unnecessary-copy-initialization.cpp | |
parent | 779c66f3ca9e51efd0b8eaac75347df4b109c7d2 (diff) | |
download | bcm5719-llvm-b959f4c33882b1f5150bb54a1664befad4251af8.tar.gz bcm5719-llvm-b959f4c33882b1f5150bb54a1664befad4251af8.zip |
[clang-tidy] Add UnnecessaryCopyInitialization check to new "performance" module in ClangTidy
Summary:
The patch adds a new ClangTidy check that detects when expensive-to-copy types are unnecessarily copy initialized from a const reference that has the same or are larger scope than the copy.
It currently only detects this when the copied variable is const qualified. But this will be extended to non const variables if they are only used in a const fashion.
Reviewers: alexfh
Subscribers: cfe-commits
Patch by Felix Berger!
Differential Revision: http://reviews.llvm.org/D15623
llvm-svn: 256632
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/performance-unnecessary-copy-initialization.cpp')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/performance-unnecessary-copy-initialization.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/performance-unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/performance-unnecessary-copy-initialization.cpp new file mode 100644 index 00000000000..7eb7d2fef67 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/performance-unnecessary-copy-initialization.cpp @@ -0,0 +1,152 @@ +// RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t + +struct ExpensiveToCopyType { + ExpensiveToCopyType() {} + virtual ~ExpensiveToCopyType() {} + const ExpensiveToCopyType &reference() const { return *this; } +}; + +struct TrivialToCopyType { + const TrivialToCopyType &reference() const { return *this; } +}; + +const ExpensiveToCopyType &ExpensiveTypeReference() { + static const ExpensiveToCopyType *Type = new ExpensiveToCopyType(); + return *Type; +} + +const TrivialToCopyType &TrivialTypeReference() { + static const TrivialToCopyType *Type = new TrivialToCopyType(); + return *Type; +} + +void PositiveFunctionCall() { + const auto AutoAssigned = ExpensiveTypeReference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] + // CHECK-FIXES: const auto& AutoAssigned = ExpensiveTypeReference(); + const auto AutoCopyConstructed(ExpensiveTypeReference()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveTypeReference()); + const ExpensiveToCopyType VarAssigned = ExpensiveTypeReference(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveTypeReference(); + const ExpensiveToCopyType VarCopyConstructed(ExpensiveTypeReference()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveTypeReference()); +} + +void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) { + const auto AutoAssigned = Obj.reference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoAssigned = Obj.reference(); + const auto AutoCopyConstructed(Obj.reference()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoCopyConstructed(Obj.reference()); + const ExpensiveToCopyType VarAssigned = Obj.reference(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj.reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj.reference()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj.reference()); +} + +void PositiveMethodCallConstParam(const ExpensiveToCopyType Obj) { + const auto AutoAssigned = Obj.reference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoAssigned = Obj.reference(); + const auto AutoCopyConstructed(Obj.reference()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoCopyConstructed(Obj.reference()); + const ExpensiveToCopyType VarAssigned = Obj.reference(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj.reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj.reference()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj.reference()); +} + +void PositiveMethodCallConstPointerParam(const ExpensiveToCopyType *const Obj) { + const auto AutoAssigned = Obj->reference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoAssigned = Obj->reference(); + const auto AutoCopyConstructed(Obj->reference()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& AutoCopyConstructed(Obj->reference()); + const ExpensiveToCopyType VarAssigned = Obj->reference(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj->reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj->reference()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj->reference()); +} + +void PositiveLocalConstValue() { + const ExpensiveToCopyType Obj; + const auto UnnecessaryCopy = Obj.reference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& UnnecessaryCopy = Obj.reference(); +} + +void PositiveLocalConstRef() { + const ExpensiveToCopyType Obj; + const ExpensiveToCopyType &ConstReference = Obj.reference(); + const auto UnnecessaryCopy = ConstReference.reference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& UnnecessaryCopy = ConstReference.reference(); +} + +void PositiveLocalConstPointer() { + const ExpensiveToCopyType Obj; + const ExpensiveToCopyType *const ConstPointer = &Obj; + const auto UnnecessaryCopy = ConstPointer->reference(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable + // CHECK-FIXES: const auto& UnnecessaryCopy = ConstPointer->reference(); +} + +void NegativeFunctionCallTrivialType() { + const auto AutoAssigned = TrivialTypeReference(); + const auto AutoCopyConstructed(TrivialTypeReference()); + const TrivialToCopyType VarAssigned = TrivialTypeReference(); + const TrivialToCopyType VarCopyConstructed(TrivialTypeReference()); +} + +void NegativeFunctionCallExpensiveTypeNonConstVariable() { + auto AutoAssigned = ExpensiveTypeReference(); + auto AutoCopyConstructed(ExpensiveTypeReference()); + ExpensiveToCopyType VarAssigned = ExpensiveTypeReference(); + ExpensiveToCopyType VarCopyConstructed(ExpensiveTypeReference()); +} + +void NegativeMethodCallNonConstRef(ExpensiveToCopyType &Obj) { + const auto AutoAssigned = Obj.reference(); + const auto AutoCopyConstructed(Obj.reference()); + const ExpensiveToCopyType VarAssigned = Obj.reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj.reference()); +} + +void NegativeMethodCallNonConst(ExpensiveToCopyType Obj) { + const auto AutoAssigned = Obj.reference(); + const auto AutoCopyConstructed(Obj.reference()); + const ExpensiveToCopyType VarAssigned = Obj.reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj.reference()); +} + +void NegativeMethodCallNonConstPointer(ExpensiveToCopyType *const Obj) { + const auto AutoAssigned = Obj->reference(); + const auto AutoCopyConstructed(Obj->reference()); + const ExpensiveToCopyType VarAssigned = Obj->reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj->reference()); +} + +void NegativeObjIsNotParam() { + ExpensiveToCopyType Obj; + const auto AutoAssigned = Obj.reference(); + const auto AutoCopyConstructed(Obj.reference()); + const ExpensiveToCopyType VarAssigned = Obj.reference(); + const ExpensiveToCopyType VarCopyConstructed(Obj.reference()); +} + +struct NegativeConstructor { + NegativeConstructor(const ExpensiveToCopyType &Obj) : Obj(Obj) {} + ExpensiveToCopyType Obj; +}; |