10#include "../utils/LexerUtils.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Lex/Lexer.h"
20SourceRange getTypeRange(
const ParmVarDecl &Param) {
21 return {Param.getBeginLoc(), Param.getLocation().getLocWithOffset(-1)};
27static std::optional<Token>
29 const MatchFinder::MatchResult &Result) {
31 CharSourceRange FileRange = Lexer::makeFileCharRange(
32 CharSourceRange::getTokenRange(getTypeRange(Param)),
33 *Result.SourceManager, Result.Context->getLangOpts());
35 if (FileRange.isInvalid())
39 tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
49 const auto ConstParamDecl =
50 parmVarDecl(hasType(qualType(isConstQualified()))).bind(
"param");
51 Finder->addMatcher(functionDecl(unless(isDefinition()),
52 has(typeLoc(forEach(ConstParamDecl))))
58 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
59 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
61 if (!Param->getType().isLocalConstQualified())
65 (Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) {
71 const auto ConstLocation = Tok ? Tok->getLocation() : Param->getBeginLoc();
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)) {
88 if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) {
96 Diag << FixItHint::CreateRemoval(
97 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
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...
llvm::StringMap< ClangTidyValue > OptionMap