blob: 91e1495e060c89ec58f409806f86fca9626bb8aa [file] [log] [blame]
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +00001//===--- AvoidConstParamsInDecls.cpp - clang-tidy--------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "AvoidConstParamsInDecls.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/Lex/Lexer.h"
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000013#include "llvm/ADT/Optional.h"
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000014
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace readability {
20namespace {
21
22SourceRange getTypeRange(const ParmVarDecl &Param) {
23 if (Param.getIdentifier() != nullptr)
Stephen Kelly43465bf2018-08-09 22:42:26 +000024 return SourceRange(Param.getBeginLoc(),
Stephen Kellyc09197e2018-08-09 22:43:02 +000025 Param.getEndLoc().getLocWithOffset(-1));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000026 return Param.getSourceRange();
27}
28
29} // namespace
30
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000031void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
32 const auto ConstParamDecl =
33 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
Samuel Benzaquen25cd6132016-06-28 14:19:41 +000034 Finder->addMatcher(
35 functionDecl(unless(isDefinition()),
36 // Lambdas are always their own definition, but they
37 // generate a non-definition FunctionDecl too. Ignore those.
Malcolm Parsons8cb9b022016-10-11 12:02:16 +000038 // Class template instantiations have a non-definition
39 // CXXMethodDecl for methods that aren't used in this
40 // translation unit. Ignore those, as the template will have
41 // already been checked.
42 unless(cxxMethodDecl(ofClass(cxxRecordDecl(anyOf(
43 isLambda(), ast_matchers::isTemplateInstantiation()))))),
Samuel Benzaquen25cd6132016-06-28 14:19:41 +000044 has(typeLoc(forEach(ConstParamDecl))))
45 .bind("func"),
46 this);
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000047}
48
Matthias Gehre018c1d42016-04-12 05:45:13 +000049// Re-lex the tokens to get precise location of last 'const'
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000050static llvm::Optional<Token> ConstTok(CharSourceRange Range,
51 const MatchFinder::MatchResult &Result) {
Matthias Gehre018c1d42016-04-12 05:45:13 +000052 const SourceManager &Sources = *Result.SourceManager;
53 std::pair<FileID, unsigned> LocInfo =
54 Sources.getDecomposedLoc(Range.getBegin());
55 StringRef File = Sources.getBufferData(LocInfo.first);
56 const char *TokenBegin = File.data() + LocInfo.second;
57 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
58 Result.Context->getLangOpts(), File.begin(), TokenBegin,
59 File.end());
60 Token Tok;
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000061 llvm::Optional<Token> ConstTok;
Matthias Gehre018c1d42016-04-12 05:45:13 +000062 while (!RawLexer.LexFromRawLexer(Tok)) {
63 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
64 break;
65 if (Tok.is(tok::raw_identifier)) {
66 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
67 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
68 Tok.setIdentifierInfo(&Info);
69 Tok.setKind(Info.getTokenID());
70 }
71 if (Tok.is(tok::kw_const))
72 ConstTok = Tok;
73 }
74 return ConstTok;
75}
76
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000077void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
78 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
79 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
80
Matthias Gehre018c1d42016-04-12 05:45:13 +000081 if (!Param->getType().isLocalConstQualified())
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000082 return;
83
Stephen Kelly43465bf2018-08-09 22:42:26 +000084 auto Diag = diag(Param->getBeginLoc(),
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000085 "parameter %0 is const-qualified in the function "
86 "declaration; const-qualification of parameters only has an "
87 "effect in function definitions");
88 if (Param->getName().empty()) {
89 for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
90 if (Param == Func->getParamDecl(i)) {
91 Diag << (i + 1);
92 break;
93 }
94 }
95 } else {
96 Diag << Param;
97 }
Matthias Gehre018c1d42016-04-12 05:45:13 +000098
Stephen Kellyc09197e2018-08-09 22:43:02 +000099 if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) {
Samuel Benzaquen79c76102016-06-06 14:21:11 +0000100 // Do not offer a suggestion if the part of the variable declaration comes
101 // from a macro.
102 return;
103 }
104
Matthias Gehre018c1d42016-04-12 05:45:13 +0000105 CharSourceRange FileRange = Lexer::makeFileCharRange(
106 CharSourceRange::getTokenRange(getTypeRange(*Param)),
Gabor Horvathafad84c2016-09-24 02:13:45 +0000107 *Result.SourceManager, getLangOpts());
Matthias Gehre018c1d42016-04-12 05:45:13 +0000108
109 if (!FileRange.isValid())
110 return;
111
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000112 auto Tok = ConstTok(FileRange, Result);
113 if (!Tok)
114 return;
Matthias Gehre018c1d42016-04-12 05:45:13 +0000115 Diag << FixItHint::CreateRemoval(
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000116 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +0000117}
118
119} // namespace readability
120} // namespace tidy
121} // namespace clang