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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rGa34532c330f6: [clang-tidy] Fix readability-avoid-const-params-in-decls removing const in… (authored by njames93).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96209/new/

https://reviews.llvm.org/D96209

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


Index: clang-tools-extra/test/clang-tidy/checkers/readability-avoid-const-params-in-decls.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-avoid-const-params-in-decls.cpp
+++ 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<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'
Index: clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
+++ 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<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 @@
   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(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96209.326097.patch
Type: text/x-patch
Size: 3945 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210224/cdd710ac/attachment-0001.bin>


More information about the cfe-commits mailing list