[clang-tools-extra] r260870 - [clang-tidy] Only invoke ForRangeCopyCheck on expensive-to-copy types.
Felix Berger via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 14 19:36:23 PST 2016
Author: flx
Date: Sun Feb 14 21:36:23 2016
New Revision: 260870
URL: http://llvm.org/viewvc/llvm-project?rev=260870&view=rev
Log:
[clang-tidy] Only invoke ForRangeCopyCheck on expensive-to-copy types.
Summary:
Fix oversight not checking the value of the Optional<bool> returned by
isExpensiveToCopy().
Reviewers: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D17064
Modified:
clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp
Modified: clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp?rev=260870&r1=260869&r2=260870&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp Sun Feb 14 21:36:23 2016
@@ -135,7 +135,9 @@ bool ForRangeCopyCheck::handleConstValue
} else if (!LoopVar.getType().isConstQualified()) {
return false;
}
- if (!type_traits::isExpensiveToCopy(LoopVar.getType(), Context))
+ llvm::Optional<bool> Expensive =
+ type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
+ if (!Expensive || !*Expensive)
return false;
auto Diagnostic =
diag(LoopVar.getLocation(),
@@ -150,8 +152,9 @@ bool ForRangeCopyCheck::handleConstValue
bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
const VarDecl &LoopVar, const CXXForRangeStmt &ForRange,
ASTContext &Context) {
- if (LoopVar.getType().isConstQualified() ||
- !type_traits::isExpensiveToCopy(LoopVar.getType(), Context)) {
+ llvm::Optional<bool> Expensive =
+ type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
+ if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive) {
return false;
}
// Collect all DeclRefExprs to the loop variable and all CallExprs and
Modified: clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp?rev=260870&r1=260869&r2=260870&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp Sun Feb 14 21:36:23 2016
@@ -127,6 +127,7 @@ bool operator!=(const Mutable& M1, const
}
void use(const Mutable &M);
+void use(int I);
void useTwice(const Mutable &M1, const Mutable &M2);
void useByValue(Mutable M);
void useByConstValue(const Mutable M);
@@ -170,6 +171,30 @@ void negativeNonConstNonMemberOperatorIn
}
}
+void negativeConstCheapToCopy() {
+ for (const int I : View<Iterator<int>>()) {
+ }
+}
+
+void negativeConstCheapToCopyTypedef() {
+ typedef const int ConstInt;
+ for (ConstInt C : View<Iterator<ConstInt>>()) {
+ }
+}
+
+void negativeCheapToCopy() {
+ for (int I : View<Iterator<int>>()) {
+ use(I);
+ }
+}
+
+void negativeCheapToCopyTypedef() {
+ typedef int Int;
+ for (Int I : View<Iterator<Int>>()) {
+ use(I);
+ }
+}
+
void positiveOnlyConstMethodInvoked() {
for (auto M : View<Iterator<Mutable>>()) {
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
More information about the cfe-commits
mailing list