10#include "clang/AST/ASTContext.h"
11#include "clang/AST/ASTLambda.h"
12#include "clang/AST/Attr.h"
13#include "clang/AST/Decl.h"
14#include "clang/AST/RecursiveASTVisitor.h"
15#include "clang/ASTMatchers/ASTMatchFinder.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Lex/Lexer.h"
24 if (
const auto *MD = dyn_cast<CXXMethodDecl>(Function))
25 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
30 const ParmVarDecl *Param) {
31 return llvm::any_of(Param->attrs(), [&](
const Attr *Attr) {
32 return SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
38 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
39 hasAnyParameter(decl()),
40 unless(hasAttr(attr::Kind::Naked)))
46static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
47 const T *PrevNode,
const T *Node,
50 return CharSourceRange::getCharRange(Node->getBeginLoc(),
51 NextNode->getBeginLoc());
54 return CharSourceRange::getTokenRange(
55 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
56 *Result.SourceManager,
57 Result.Context->getLangOpts()),
60 return CharSourceRange::getTokenRange(Node->getSourceRange());
64 const FunctionDecl *Function,
unsigned Index) {
66 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
67 Function->getParamDecl(Index),
68 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
73 const CallExpr *Call,
unsigned Index) {
75 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
77 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
81 :
public RecursiveASTVisitor<IndexerVisitor> {
85 const llvm::SmallPtrSetImpl<const CallExpr *> &
87 return Index[Fn->getCanonicalDecl()].Calls;
90 const llvm::SmallPtrSetImpl<const DeclRefExpr *> &
92 return Index[Fn->getCanonicalDecl()].OtherRefs;
98 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
99 Fn = Fn->getCanonicalDecl();
100 Index[Fn].OtherRefs.insert(DeclRef);
107 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
108 Fn = Fn->getCanonicalDecl();
109 if (
const auto *Ref =
110 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
111 Index[Fn].OtherRefs.erase(Ref);
113 Index[Fn].Calls.insert(Call);
120 llvm::SmallPtrSet<const CallExpr *, 2> Calls;
121 llvm::SmallPtrSet<const DeclRefExpr *, 2> OtherRefs;
124 llvm::DenseMap<const FunctionDecl *, IndexEntry> Index;
132 StrictMode(Options.get(
"StrictMode", false)),
133 IgnoreVirtual(Options.get(
"IgnoreVirtual", false)) {}
136 Options.store(Opts,
"StrictMode", StrictMode);
137 Options.store(Opts,
"IgnoreVirtual", IgnoreVirtual);
140void UnusedParametersCheck::warnOnUnusedParameter(
141 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
142 unsigned ParamIndex) {
143 const auto *Param = Function->getParamDecl(ParamIndex);
145 if (Param->isInvalidDecl())
147 auto MyDiag = diag(Param->getLocation(),
"parameter %0 is unused") << Param;
150 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
153 if (Function->isExternallyVisible() ||
154 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
156 isLambdaCallOperator(Function)) {
158 if (!Result.Context->getLangOpts().CPlusPlus)
161 const SourceRange RemovalRange(Param->getLocation());
165 MyDiag << FixItHint::CreateReplacement(
166 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
171 for (
const FunctionDecl *FD : Function->redecls())
172 if (FD->param_size())
176 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
177 if (ParamIndex < Call->getNumArgs())
182 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
183 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
185 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
186 if (IgnoreVirtual && Method->isVirtual())
188 if (Method->isLambdaStaticInvoker())
191 for (
unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
192 const auto *Param = Function->getParamDecl(I);
193 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
194 Param->hasAttr<UnusedAttr>())
204 if (StrictMode || !Function->getBody()->children().empty() ||
205 (isa<CXXConstructorDecl>(Function) &&
206 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
207 warnOnUnusedParameter(Result, Function, I);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
const llvm::SmallPtrSetImpl< const DeclRefExpr * > & getOtherRefs(const FunctionDecl *Fn)
const llvm::SmallPtrSetImpl< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
IndexerVisitor(ASTContext &Ctx)
bool WalkUpFromCallExpr(CallExpr *Call)
bool shouldTraversePostOrder() const
bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
UnusedParametersCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
~UnusedParametersCheck() override
static bool hasAttrAfterParam(const SourceManager *SourceManager, const ParmVarDecl *Param)
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
static bool isOverrideMethod(const FunctionDecl *Function)
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
llvm::StringMap< ClangTidyValue > OptionMap