diff --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp --- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp +++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "AvoidConstParamsInDecls.h" +#include "../utils/LexerUtils.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Lex/Lexer.h" @@ -20,10 +21,8 @@ namespace { SourceRange getTypeRange(const ParmVarDecl &Param) { - if (Param.getIdentifier() != nullptr) - return SourceRange(Param.getBeginLoc(), - Param.getEndLoc().getLocWithOffset(-1)); - return Param.getSourceRange(); + return SourceRange(Param.getBeginLoc(), + Param.getLocation().getLocWithOffset(-1)); } } // namespace @@ -38,34 +37,6 @@ this); } -// Re-lex the tokens to get precise location of last 'const' -static llvm::Optional constTok(CharSourceRange Range, - const MatchFinder::MatchResult &Result) { - const SourceManager &Sources = *Result.SourceManager; - std::pair LocInfo = - Sources.getDecomposedLoc(Range.getBegin()); - StringRef File = Sources.getBufferData(LocInfo.first); - const char *TokenBegin = File.data() + LocInfo.second; - Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first), - Result.Context->getLangOpts(), File.begin(), TokenBegin, - File.end()); - Token Tok; - llvm::Optional ConstTok; - while (!RawLexer.LexFromRawLexer(Tok)) { - if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation())) - break; - if (Tok.is(tok::raw_identifier)) { - IdentifierInfo &Info = Result.Context->Idents.get(StringRef( - Sources.getCharacterData(Tok.getLocation()), Tok.getLength())); - Tok.setIdentifierInfo(&Info); - Tok.setKind(Info.getTokenID()); - } - if (Tok.is(tok::kw_const)) - ConstTok = Tok; - } - return ConstTok; -} - void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) { const auto *Func = Result.Nodes.getNodeAs("func"); const auto *Param = Result.Nodes.getNodeAs("param"); @@ -101,7 +72,8 @@ if (!FileRange.isValid()) return; - auto Tok = constTok(FileRange, Result); + auto Tok = tidy::utils::lexer::getQualifyingToken( + tok::kw_const, FileRange, *Result.Context, *Result.SourceManager); if (!Tok) return; Diag << FixItHint::CreateRemoval( 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 --- 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 @@ -59,6 +59,25 @@ // CHECK-FIXES: int F13(bool b = true); int f13 = F13(); +template +struct A {}; + +void F14(const A); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified +// CHECK-FIXES: void F14(A); + +void F15(const A Named); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified +// CHECK-FIXES: void F15(A Named); + +void F16(const A *const); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified +// CHECK-FIXES: void F16(const A *); + +void F17(const A *const Named); +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified +// CHECK-FIXES: void F17(const A *Named); + struct Foo { Foo(const int i); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: parameter 'i'