llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-tidy Author: None (Da-Viper) <details> <summary>Changes</summary> Fixes #<!-- -->68492 --- Full diff: https://github.com/llvm/llvm-project/pull/69103.diff 2 Files Affected: - (modified) clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp (+27-16) - (modified) clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp (+7-7) ``````````diff diff --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp index 6476f1d7fdf2b81..24cbbd8bc60a2b5 100644 --- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp +++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp @@ -21,6 +21,24 @@ SourceRange getTypeRange(const ParmVarDecl &Param) { return {Param.getBeginLoc(), Param.getLocation().getLocWithOffset(-1)}; } +// Finds the location of the qualifying `const` token in the `ParmValDecl`'s +// return type. Returns `std::nullopt` when the parm type is not +// `const`-qualified like when the type is an alias or a macro. +static std::optional<Token> +findConstToRemove(const ParmVarDecl &Param, + const MatchFinder::MatchResult &Result) { + + CharSourceRange FileRange = Lexer::makeFileCharRange( + CharSourceRange::getTokenRange(getTypeRange(Param)), + *Result.SourceManager, Result.Context->getLangOpts()); + + if (FileRange.isInvalid()) + return std::nullopt; + + return tidy::utils::lexer::getQualifyingToken( + tok::kw_const, FileRange, *Result.Context, *Result.SourceManager); +} + } // namespace void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) { @@ -30,11 +48,10 @@ void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) { void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) { const auto ConstParamDecl = parmVarDecl(hasType(qualType(isConstQualified()))).bind("param"); - Finder->addMatcher( - functionDecl(unless(isDefinition()), - has(typeLoc(forEach(ConstParamDecl)))) - .bind("func"), - this); + Finder->addMatcher(functionDecl(unless(isDefinition()), + has(typeLoc(forEach(ConstParamDecl)))) + .bind("func"), + this); } void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) { @@ -50,7 +67,10 @@ void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) { return; } - auto Diag = diag(Param->getBeginLoc(), + const auto Tok = findConstToRemove(*Param, Result); + const auto ConstLocation = Tok ? Tok->getLocation() : Param->getBeginLoc(); + + auto Diag = diag(ConstLocation, "parameter %0 is const-qualified in the function " "declaration; const-qualification of parameters only has an " "effect in function definitions"); @@ -70,18 +90,9 @@ void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) { // from a macro. return; } - - CharSourceRange FileRange = Lexer::makeFileCharRange( - CharSourceRange::getTokenRange(getTypeRange(*Param)), - *Result.SourceManager, getLangOpts()); - - if (!FileRange.isValid()) - return; - - auto Tok = tidy::utils::lexer::getQualifyingToken( - tok::kw_const, FileRange, *Result.Context, *Result.SourceManager); if (!Tok) return; + Diag << FixItHint::CreateRemoval( CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation())); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp index d521fd238b7d521..bc098efe3044a5d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp @@ -9,15 +9,15 @@ void F1(const int i); // CHECK-FIXES: void F1(int i); void F2(const int *const i); -// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 'i' is const-qualified // CHECK-FIXES: void F2(const int *i); void F3(int const i); -// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'i' is const-qualified // CHECK-FIXES: void F3(int i); void F4(alias_type const i); -// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 'i' is const-qualified // CHECK-FIXES: void F4(alias_type i); void F5(const int); @@ -25,7 +25,7 @@ void F5(const int); // CHECK-FIXES: void F5(int); void F6(const int *const); -// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 1 is const-qualified // CHECK-FIXES: void F6(const int *); void F7(int, const int); @@ -42,7 +42,7 @@ void F9(const int long_name); // CHECK-FIXES: void F9(int long_name); void F10(const int *const *const long_name); -// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'long_name' +// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: parameter 'long_name' // CHECK-FIXES: void F10(const int *const *long_name); void F11(const unsigned int /*v*/); @@ -71,11 +71,11 @@ void F15(const A<const int> Named); // CHECK-FIXES: void F15(A<const int> Named); void F16(const A<const int> *const); -// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified +// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: parameter 1 is const-qualified // CHECK-FIXES: void F16(const A<const int> *); void F17(const A<const int> *const Named); -// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified +// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: parameter 'Named' is const-qualified // CHECK-FIXES: void F17(const A<const int> *Named); struct Foo { `````````` </details> https://github.com/llvm/llvm-project/pull/69103 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits