summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
diff options
context:
space:
mode:
authorJonas Toth <development@jonas-toth.eu>2020-01-03 20:36:49 +0100
committerJonas Toth <development@jonas-toth.eu>2020-01-03 20:37:47 +0100
commitcf48101200ee192dd82e6ed0512ae42e7b3162a9 (patch)
treee389873c7d814c730d412eb935cf22d1eb0407eb /clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
parentaaaf6c456093101aea50be740ce1598174e6e5aa (diff)
downloadbcm5719-llvm-cf48101200ee192dd82e6ed0512ae42e7b3162a9.tar.gz
bcm5719-llvm-cf48101200ee192dd82e6ed0512ae42e7b3162a9.zip
[clang-tidy] implement utility-function to add 'const' to variables
Summary: This patch extends the already existing facility to add 'const' to variables to be more flexible and correct. The previous version did not consider pointers as value AND pointee. For future automatic introduction for const-correctness this shortcoming needs to be fixed. It always allows configuration where the 'const' token is inserted, either on the left side (if possible) or the right side. It adds many unit-tests to the utility-function that did not exist before, as the function was implicitly tested through clang-tidy checks. These tests were not changed, as the API is still compatible. Reviewers: aaron.ballman, hokein, alexfh, shuaiwang, lebedev.ri Reviewed By: aaron.ballman Subscribers: jdoerfert, mgorny, xazax.hun, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D54395
Diffstat (limited to 'clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp22
1 files changed, 15 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
index e0040094259..67a62022766 100644
--- a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
@@ -13,6 +13,7 @@
#include "../utils/OptionsUtils.h"
#include "../utils/TypeTraits.h"
#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "clang/Basic/Diagnostic.h"
using namespace clang::ast_matchers;
@@ -77,8 +78,11 @@ bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar,
"the loop variable's type is not a reference type; this creates a "
"copy in each iteration; consider making this a reference")
<< utils::fixit::changeVarDeclToReference(LoopVar, Context);
- if (!LoopVar.getType().isConstQualified())
- Diagnostic << utils::fixit::changeVarDeclToConst(LoopVar);
+ if (!LoopVar.getType().isConstQualified()) {
+ if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
+ LoopVar, Context, DeclSpec::TQ::TQ_const))
+ Diagnostic << *Fix;
+ }
return true;
}
@@ -101,11 +105,15 @@ bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
!utils::decl_ref_expr::allDeclRefExprs(LoopVar, *ForRange.getBody(),
Context)
.empty()) {
- diag(LoopVar.getLocation(),
- "loop variable is copied but only used as const reference; consider "
- "making it a const reference")
- << utils::fixit::changeVarDeclToConst(LoopVar)
- << utils::fixit::changeVarDeclToReference(LoopVar, Context);
+ auto Diag = diag(
+ LoopVar.getLocation(),
+ "loop variable is copied but only used as const reference; consider "
+ "making it a const reference");
+
+ if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl(
+ LoopVar, Context, DeclSpec::TQ::TQ_const))
+ Diag << *Fix << utils::fixit::changeVarDeclToReference(LoopVar, Context);
+
return true;
}
return false;
OpenPOWER on IntegriCloud