[clang-tools-extra] a34532c - [clang-tidy] Fix readability-avoid-const-params-in-decls removing const in template paramaters

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 24 07:54:20 PST 2021


Author: Nathan James
Date: 2021-02-24T15:54:10Z
New Revision: a34532c330f61c35612bb0c4b753979307020608

URL: https://github.com/llvm/llvm-project/commit/a34532c330f61c35612bb0c4b753979307020608
DIFF: https://github.com/llvm/llvm-project/commit/a34532c330f61c35612bb0c4b753979307020608.diff

LOG: [clang-tidy] Fix readability-avoid-const-params-in-decls removing const in template paramaters

Fixes https://bugs.llvm.org/show_bug.cgi?id=38035

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D96209

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability-avoid-const-params-in-decls.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
index d35cc079d78a..e655f013a254 100644
--- 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 readability {
 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 @@ void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
       this);
 }
 
-// Re-lex the tokens to get precise location of last 'const'
-static llvm::Optional<Token> constTok(CharSourceRange Range,
-                                      const MatchFinder::MatchResult &Result) {
-  const SourceManager &Sources = *Result.SourceManager;
-  std::pair<FileID, unsigned> 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<Token> 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<FunctionDecl>("func");
   const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
@@ -101,7 +72,8 @@ void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
   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
index f3d27e563193..4ba1f2482d2d 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
@@ -59,6 +59,25 @@ int F13(const bool b = true);
 // CHECK-FIXES: int F13(bool b = true);
 int f13 = F13<int>();
 
+template <typename T>
+struct A {};
+
+void F14(const A<const int>);
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified
+// CHECK-FIXES: void F14(A<const int>);
+
+void F15(const A<const int> Named);
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified
+// 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-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-FIXES: void F17(const A<const int> *Named);
+
 struct Foo {
   Foo(const int i);
   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: parameter 'i'


        


More information about the cfe-commits mailing list