blob: 51fc489501e8a31a4140067abc0107ea04bffe13 [file] [log] [blame]
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +00001//===--- AvoidConstParamsInDecls.cpp - clang-tidy--------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "AvoidConstParamsInDecls.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Lex/Lexer.h"
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000014#include "llvm/ADT/Optional.h"
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000015
16using namespace clang::ast_matchers;
17
18namespace clang {
19namespace tidy {
20namespace readability {
21namespace {
22
23SourceRange getTypeRange(const ParmVarDecl &Param) {
24 if (Param.getIdentifier() != nullptr)
Stephen Kelly43465bf2018-08-09 22:42:26 +000025 return SourceRange(Param.getBeginLoc(),
Stephen Kellyc09197e2018-08-09 22:43:02 +000026 Param.getEndLoc().getLocWithOffset(-1));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000027 return Param.getSourceRange();
28}
29
30} // namespace
31
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000032void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
33 const auto ConstParamDecl =
34 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
Samuel Benzaquen25cd6132016-06-28 14:19:41 +000035 Finder->addMatcher(
36 functionDecl(unless(isDefinition()),
37 // Lambdas are always their own definition, but they
38 // generate a non-definition FunctionDecl too. Ignore those.
Malcolm Parsons8cb9b022016-10-11 12:02:16 +000039 // Class template instantiations have a non-definition
40 // CXXMethodDecl for methods that aren't used in this
41 // translation unit. Ignore those, as the template will have
42 // already been checked.
43 unless(cxxMethodDecl(ofClass(cxxRecordDecl(anyOf(
44 isLambda(), ast_matchers::isTemplateInstantiation()))))),
Samuel Benzaquen25cd6132016-06-28 14:19:41 +000045 has(typeLoc(forEach(ConstParamDecl))))
46 .bind("func"),
47 this);
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000048}
49
Matthias Gehre018c1d42016-04-12 05:45:13 +000050// Re-lex the tokens to get precise location of last 'const'
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000051static llvm::Optional<Token> ConstTok(CharSourceRange Range,
52 const MatchFinder::MatchResult &Result) {
Matthias Gehre018c1d42016-04-12 05:45:13 +000053 const SourceManager &Sources = *Result.SourceManager;
54 std::pair<FileID, unsigned> LocInfo =
55 Sources.getDecomposedLoc(Range.getBegin());
56 StringRef File = Sources.getBufferData(LocInfo.first);
57 const char *TokenBegin = File.data() + LocInfo.second;
58 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
59 Result.Context->getLangOpts(), File.begin(), TokenBegin,
60 File.end());
61 Token Tok;
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000062 llvm::Optional<Token> ConstTok;
Matthias Gehre018c1d42016-04-12 05:45:13 +000063 while (!RawLexer.LexFromRawLexer(Tok)) {
64 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
65 break;
66 if (Tok.is(tok::raw_identifier)) {
67 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
68 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
69 Tok.setIdentifierInfo(&Info);
70 Tok.setKind(Info.getTokenID());
71 }
72 if (Tok.is(tok::kw_const))
73 ConstTok = Tok;
74 }
75 return ConstTok;
76}
77
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000078void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
79 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
80 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
81
Matthias Gehre018c1d42016-04-12 05:45:13 +000082 if (!Param->getType().isLocalConstQualified())
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000083 return;
84
Stephen Kelly43465bf2018-08-09 22:42:26 +000085 auto Diag = diag(Param->getBeginLoc(),
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000086 "parameter %0 is const-qualified in the function "
87 "declaration; const-qualification of parameters only has an "
88 "effect in function definitions");
89 if (Param->getName().empty()) {
90 for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
91 if (Param == Func->getParamDecl(i)) {
92 Diag << (i + 1);
93 break;
94 }
95 }
96 } else {
97 Diag << Param;
98 }
Matthias Gehre018c1d42016-04-12 05:45:13 +000099
Stephen Kellyc09197e2018-08-09 22:43:02 +0000100 if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) {
Samuel Benzaquen79c76102016-06-06 14:21:11 +0000101 // Do not offer a suggestion if the part of the variable declaration comes
102 // from a macro.
103 return;
104 }
105
Matthias Gehre018c1d42016-04-12 05:45:13 +0000106 CharSourceRange FileRange = Lexer::makeFileCharRange(
107 CharSourceRange::getTokenRange(getTypeRange(*Param)),
Gabor Horvathafad84c2016-09-24 02:13:45 +0000108 *Result.SourceManager, getLangOpts());
Matthias Gehre018c1d42016-04-12 05:45:13 +0000109
110 if (!FileRange.isValid())
111 return;
112
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000113 auto Tok = ConstTok(FileRange, Result);
114 if (!Tok)
115 return;
Matthias Gehre018c1d42016-04-12 05:45:13 +0000116 Diag << FixItHint::CreateRemoval(
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000117 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +0000118}
119
120} // namespace readability
121} // namespace tidy
122} // namespace clang