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