clang-tools 20.0.0git
AvoidConstParamsInDecls.cpp
Go to the documentation of this file.
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
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
18namespace {
19
20SourceRange 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.
27static std::optional<Token>
28findConstToRemove(const ParmVarDecl &Param,
29 const MatchFinder::MatchResult &Result) {
30
31 CharSourceRange FileRange = Lexer::makeFileCharRange(
32 CharSourceRange::getTokenRange(getTypeRange(Param)),
33 *Result.SourceManager, Result.Context->getLangOpts());
34
35 if (FileRange.isInvalid())
36 return std::nullopt;
37
39 tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
40}
41
42} // namespace
43
45 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
46}
47
49 const auto ConstParamDecl =
50 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
51 Finder->addMatcher(functionDecl(unless(isDefinition()),
52 has(typeLoc(forEach(ConstParamDecl))))
53 .bind("func"),
54 this);
55}
56
57void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
58 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
59 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("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, 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)) {
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 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
98}
99
100} // namespace clang::tidy::readability
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
static std::optional< Token > findConstToRemove(const FunctionDecl *Def, 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...
Definition: LexerUtils.cpp:149
llvm::StringMap< ClangTidyValue > OptionMap