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