From cf48101200ee192dd82e6ed0512ae42e7b3162a9 Mon Sep 17 00:00:00 2001 From: Jonas Toth Date: Fri, 3 Jan 2020 20:36:49 +0100 Subject: [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 --- .../clang-tidy/performance/ForRangeCopyCheck.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp') 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 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 Fix = utils::fixit::addQualifierToVarDecl( + LoopVar, Context, DeclSpec::TQ::TQ_const)) + Diag << *Fix << utils::fixit::changeVarDeclToReference(LoopVar, Context); + return true; } return false; -- cgit v1.2.3