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 SourceRange(Param.getBeginLoc(),
22 Param.getLocation().getLocWithOffset(-1));
32 const auto ConstParamDecl =
33 parmVarDecl(hasType(qualType(isConstQualified()))).bind(
"param");
35 functionDecl(unless(isDefinition()),
36 has(typeLoc(forEach(ConstParamDecl))))
42 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
43 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
45 if (!Param->getType().isLocalConstQualified())
49 (Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) {
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)) {
69 if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) {
75 CharSourceRange FileRange = Lexer::makeFileCharRange(
76 CharSourceRange::getTokenRange(getTypeRange(*Param)),
79 if (!FileRange.isValid())
83 tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
86 Diag << FixItHint::CreateRemoval(
87 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.
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...
llvm::StringMap< ClangTidyValue > OptionMap