10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/ASTLambda.h"
12 #include "clang/AST/RecursiveASTVisitor.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Lex/Lexer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include <unordered_map>
17 #include <unordered_set>
27 if (
const auto *
MD = dyn_cast<CXXMethodDecl>(Function))
28 return MD->size_overridden_methods() > 0 ||
MD->hasAttr<OverrideAttr>();
33 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
35 functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
41 static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
42 const T *PrevNode,
const T *Node,
45 return CharSourceRange::getCharRange(Node->getBeginLoc(),
46 NextNode->getBeginLoc());
49 return CharSourceRange::getTokenRange(
50 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
51 *Result.SourceManager,
52 Result.Context->getLangOpts()),
55 return CharSourceRange::getTokenRange(Node->getSourceRange());
59 const FunctionDecl *Function,
unsigned Index) {
61 Result,
Index > 0 ? Function->getParamDecl(
Index - 1) :
nullptr,
62 Function->getParamDecl(
Index),
63 Index + 1 < Function->getNumParams() ? Function->getParamDecl(
Index + 1)
68 const CallExpr *Call,
unsigned Index) {
70 Result,
Index > 0 ? Call->getArg(
Index - 1) :
nullptr,
72 Index + 1 < Call->getNumArgs() ? Call->getArg(
Index + 1) :
nullptr));
76 :
public RecursiveASTVisitor<IndexerVisitor> {
80 const std::unordered_set<const CallExpr *> &
82 return Index[Fn->getCanonicalDecl()].Calls;
85 const std::unordered_set<const DeclRefExpr *> &
87 return Index[Fn->getCanonicalDecl()].OtherRefs;
93 if (
const auto *Fn = dyn_cast<FunctionDecl>(
DeclRef->getDecl())) {
94 Fn = Fn->getCanonicalDecl();
102 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
103 Fn = Fn->getCanonicalDecl();
104 if (
const auto *Ref =
105 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
106 Index[Fn].OtherRefs.erase(Ref);
108 Index[Fn].Calls.insert(Call);
115 std::unordered_set<const CallExpr *> Calls;
116 std::unordered_set<const DeclRefExpr *> OtherRefs;
119 std::unordered_map<const FunctionDecl *, IndexEntry>
Index;
122 UnusedParametersCheck::~UnusedParametersCheck() =
default;
124 UnusedParametersCheck::UnusedParametersCheck(StringRef
Name,
127 StrictMode(Options.getLocalOrGlobal(
"StrictMode", false)) {}
133 void UnusedParametersCheck::warnOnUnusedParameter(
134 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
135 unsigned ParamIndex) {
136 const auto *Param = Function->getParamDecl(ParamIndex);
137 auto MyDiag =
diag(Param->getLocation(),
"parameter %0 is unused") << Param;
140 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
144 if (Function->isExternallyVisible() ||
145 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
147 isLambdaCallOperator(Function)) {
150 if (!Result.Context->getLangOpts().CPlusPlus)
153 SourceRange RemovalRange(Param->getLocation());
157 MyDiag << FixItHint::CreateReplacement(
158 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
163 for (
const FunctionDecl *FD : Function->redecls())
164 if (FD->param_size())
168 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
169 if (ParamIndex < Call->getNumArgs())
174 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
175 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
177 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function))
178 if (Method->isLambdaStaticInvoker())
180 for (
unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
181 const auto *Param = Function->getParamDecl(i);
182 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
183 Param->hasAttr<UnusedAttr>())
189 (Function->getBody()->child_begin() !=
190 Function->getBody()->child_end()) ||
191 (isa<CXXConstructorDecl>(Function) &&
192 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
193 warnOnUnusedParameter(Result, Function, i);