[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
Sat Feb 6 10:43:46 PST 2021
njames93 created this revision.
njames93 added reviewers: aaron.ballman, alexfh.
Herald added subscribers: nullptr.cpp, xazax.hun.
njames93 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Fixes https://bugs.llvm.org/show_bug.cgi?id=49063
Repository:
rG LLVM Github Monorepo
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
@@ -46,34 +45,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");
@@ -109,7 +80,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.321952.patch
Type: text/x-patch
Size: 3945 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210206/ef65000c/attachment.bin>
More information about the cfe-commits
mailing list