1 | //===--- AvoidConstParamsInDecls.cpp - clang-tidy--------------------------===// |
2 | // |
3 | // 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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "AvoidConstParamsInDecls.h" |
10 | #include "../utils/LexerUtils.h" |
11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
12 | #include "clang/ASTMatchers/ASTMatchers.h" |
13 | #include "clang/Lex/Lexer.h" |
14 | |
15 | using namespace clang::ast_matchers; |
16 | |
17 | namespace clang::tidy::readability { |
18 | namespace { |
19 | |
20 | SourceRange getTypeRange(const ParmVarDecl &Param) { |
21 | return {Param.getBeginLoc(), Param.getLocation().getLocWithOffset(-1)}; |
22 | } |
23 | |
24 | // Finds the location of the qualifying `const` token in the `ParmValDecl`'s |
25 | // return type. Returns `std::nullopt` when the parm type is not |
26 | // `const`-qualified like when the type is an alias or a macro. |
27 | static std::optional<Token> |
28 | findConstToRemove(const ParmVarDecl &Param, |
29 | const MatchFinder::MatchResult &Result) { |
30 | |
31 | CharSourceRange FileRange = Lexer::makeFileCharRange( |
32 | Range: CharSourceRange::getTokenRange(R: getTypeRange(Param)), |
33 | SM: *Result.SourceManager, LangOpts: Result.Context->getLangOpts()); |
34 | |
35 | if (FileRange.isInvalid()) |
36 | return std::nullopt; |
37 | |
38 | return tidy::utils::lexer::getQualifyingToken( |
39 | TK: tok::kw_const, Range: FileRange, Context: *Result.Context, SM: *Result.SourceManager); |
40 | } |
41 | |
42 | } // namespace |
43 | |
44 | void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
45 | Options.store(Options&: Opts, LocalName: "IgnoreMacros" , Value: IgnoreMacros); |
46 | } |
47 | |
48 | void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) { |
49 | const auto ConstParamDecl = |
50 | parmVarDecl(hasType(InnerMatcher: qualType(isConstQualified()))).bind(ID: "param" ); |
51 | Finder->addMatcher(NodeMatch: functionDecl(unless(isDefinition()), |
52 | has(typeLoc(forEach(ConstParamDecl)))) |
53 | .bind(ID: "func" ), |
54 | Action: this); |
55 | } |
56 | |
57 | void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) { |
58 | const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(ID: "func" ); |
59 | const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(ID: "param" ); |
60 | |
61 | if (!Param->getType().isLocalConstQualified()) |
62 | return; |
63 | |
64 | if (IgnoreMacros && |
65 | (Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) { |
66 | // Suppress the check if macros are involved. |
67 | return; |
68 | } |
69 | |
70 | const auto Tok = findConstToRemove(Param: *Param, Result); |
71 | const auto ConstLocation = Tok ? Tok->getLocation() : Param->getBeginLoc(); |
72 | |
73 | auto Diag = diag(ConstLocation, |
74 | "parameter %0 is const-qualified in the function " |
75 | "declaration; const-qualification of parameters only has an " |
76 | "effect in function definitions" ); |
77 | if (Param->getName().empty()) { |
78 | for (unsigned int I = 0; I < Func->getNumParams(); ++I) { |
79 | if (Param == Func->getParamDecl(i: I)) { |
80 | Diag << (I + 1); |
81 | break; |
82 | } |
83 | } |
84 | } else { |
85 | Diag << Param; |
86 | } |
87 | |
88 | if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) { |
89 | // Do not offer a suggestion if the part of the variable declaration comes |
90 | // from a macro. |
91 | return; |
92 | } |
93 | if (!Tok) |
94 | return; |
95 | |
96 | Diag << FixItHint::CreateRemoval( |
97 | RemoveRange: CharSourceRange::getTokenRange(B: Tok->getLocation(), E: Tok->getLocation())); |
98 | } |
99 | |
100 | } // namespace clang::tidy::readability |
101 | |