clang-tools 18.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} // namespace
25
27 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
28}
29
31 const auto ConstParamDecl =
32 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
33 Finder->addMatcher(
34 functionDecl(unless(isDefinition()),
35 has(typeLoc(forEach(ConstParamDecl))))
36 .bind("func"),
37 this);
38}
39
40void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
41 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
42 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
43
44 if (!Param->getType().isLocalConstQualified())
45 return;
46
47 if (IgnoreMacros &&
48 (Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) {
49 // Suppress the check if macros are involved.
50 return;
51 }
52
53 auto Diag = diag(Param->getBeginLoc(),
54 "parameter %0 is const-qualified in the function "
55 "declaration; const-qualification of parameters only has an "
56 "effect in function definitions");
57 if (Param->getName().empty()) {
58 for (unsigned int I = 0; I < Func->getNumParams(); ++I) {
59 if (Param == Func->getParamDecl(I)) {
60 Diag << (I + 1);
61 break;
62 }
63 }
64 } else {
65 Diag << Param;
66 }
67
68 if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) {
69 // Do not offer a suggestion if the part of the variable declaration comes
70 // from a macro.
71 return;
72 }
73
74 CharSourceRange FileRange = Lexer::makeFileCharRange(
75 CharSourceRange::getTokenRange(getTypeRange(*Param)),
76 *Result.SourceManager, getLangOpts());
77
78 if (!FileRange.isValid())
79 return;
80
82 tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
83 if (!Tok)
84 return;
85 Diag << FixItHint::CreateRemoval(
86 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
87}
88
89} // 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.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
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