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"
18#include <unordered_map>
19#include <unordered_set>
26bool isOverrideMethod(
const FunctionDecl *Function) {
27 if (
const auto *MD = dyn_cast<CXXMethodDecl>(Function))
28 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
32bool hasAttrAfterParam(
const SourceManager *SourceManager,
33 const ParmVarDecl *Param) {
34 for (
const auto *Attr : Param->attrs()) {
35 if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
36 Attr->getLocation())) {
45 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
46 hasAnyParameter(decl()),
47 unless(hasAttr(attr::Kind::Naked)))
53static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
54 const T *PrevNode,
const T *Node,
57 return CharSourceRange::getCharRange(Node->getBeginLoc(),
58 NextNode->getBeginLoc());
61 return CharSourceRange::getTokenRange(
62 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
63 *Result.SourceManager,
64 Result.Context->getLangOpts()),
67 return CharSourceRange::getTokenRange(Node->getSourceRange());
71 const FunctionDecl *Function,
unsigned Index) {
73 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
74 Function->getParamDecl(Index),
75 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
80 const CallExpr *Call,
unsigned Index) {
82 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
84 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
88 :
public RecursiveASTVisitor<IndexerVisitor> {
92 const std::unordered_set<const CallExpr *> &
94 return Index[Fn->getCanonicalDecl()].Calls;
97 const std::unordered_set<const DeclRefExpr *> &
99 return Index[Fn->getCanonicalDecl()].OtherRefs;
105 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
106 Fn = Fn->getCanonicalDecl();
107 Index[Fn].OtherRefs.insert(DeclRef);
114 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
115 Fn = Fn->getCanonicalDecl();
116 if (
const auto *Ref =
117 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
118 Index[Fn].OtherRefs.erase(Ref);
120 Index[Fn].Calls.insert(Call);
127 std::unordered_set<const CallExpr *> Calls;
128 std::unordered_set<const DeclRefExpr *> OtherRefs;
131 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
139 StrictMode(Options.get(
"StrictMode", false)),
140 IgnoreVirtual(Options.get(
"IgnoreVirtual", false)) {}
143 Options.store(Opts,
"StrictMode", StrictMode);
144 Options.store(Opts,
"IgnoreVirtual", IgnoreVirtual);
147void UnusedParametersCheck::warnOnUnusedParameter(
148 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
149 unsigned ParamIndex) {
150 const auto *Param = Function->getParamDecl(ParamIndex);
152 if (Param->isInvalidDecl())
154 auto MyDiag = diag(Param->getLocation(),
"parameter %0 is unused") << Param;
157 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
161 if (Function->isExternallyVisible() ||
162 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
163 !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function) ||
164 isLambdaCallOperator(Function)) {
167 if (!Result.Context->getLangOpts().CPlusPlus)
170 SourceRange RemovalRange(Param->getLocation());
174 MyDiag << FixItHint::CreateReplacement(
175 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
180 for (
const FunctionDecl *FD : Function->redecls())
181 if (FD->param_size())
185 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
186 if (ParamIndex < Call->getNumArgs())
191 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
192 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
194 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
195 if (IgnoreVirtual && Method->isVirtual())
197 if (Method->isLambdaStaticInvoker())
200 for (
unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
201 const auto *Param = Function->getParamDecl(I);
202 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
203 Param->hasAttr<UnusedAttr>())
205 if (hasAttrAfterParam(Result.SourceManager, Param)) {
213 if (StrictMode || !Function->getBody()->children().empty() ||
214 (isa<CXXConstructorDecl>(Function) &&
215 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
216 warnOnUnusedParameter(Result, Function, I);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
IndexerVisitor(ASTContext &Ctx)
bool WalkUpFromCallExpr(CallExpr *Call)
bool shouldTraversePostOrder() const
bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef)
const std::unordered_set< const DeclRefExpr * > & getOtherRefs(const FunctionDecl *Fn)
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
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 FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
llvm::StringMap< ClangTidyValue > OptionMap