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) {
34 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
35 hasAnyParameter(decl()),
36 unless(hasAttr(attr::Kind::Naked)))
42 static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
43 const T *PrevNode,
const T *Node,
46 return CharSourceRange::getCharRange(Node->getBeginLoc(),
47 NextNode->getBeginLoc());
50 return CharSourceRange::getTokenRange(
51 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
52 *Result.SourceManager,
53 Result.Context->getLangOpts()),
56 return CharSourceRange::getTokenRange(Node->getSourceRange());
60 const FunctionDecl *Function,
unsigned Index) {
62 Result,
Index > 0 ? Function->getParamDecl(
Index - 1) :
nullptr,
63 Function->getParamDecl(
Index),
64 Index + 1 < Function->getNumParams() ? Function->getParamDecl(
Index + 1)
69 const CallExpr *Call,
unsigned Index) {
71 Result,
Index > 0 ? Call->getArg(
Index - 1) :
nullptr,
73 Index + 1 < Call->getNumArgs() ? Call->getArg(
Index + 1) :
nullptr));
77 :
public RecursiveASTVisitor<IndexerVisitor> {
81 const std::unordered_set<const CallExpr *> &
83 return Index[Fn->getCanonicalDecl()].Calls;
86 const std::unordered_set<const DeclRefExpr *> &
88 return Index[Fn->getCanonicalDecl()].OtherRefs;
94 if (
const auto *Fn = dyn_cast<FunctionDecl>(
DeclRef->getDecl())) {
95 Fn = Fn->getCanonicalDecl();
103 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
104 Fn = Fn->getCanonicalDecl();
105 if (
const auto *Ref =
106 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
107 Index[Fn].OtherRefs.erase(Ref);
109 Index[Fn].Calls.insert(Call);
116 std::unordered_set<const CallExpr *> Calls;
117 std::unordered_set<const DeclRefExpr *> OtherRefs;
120 std::unordered_map<const FunctionDecl *, IndexEntry>
Index;
123 UnusedParametersCheck::~UnusedParametersCheck() =
default;
125 UnusedParametersCheck::UnusedParametersCheck(StringRef
Name,
128 StrictMode(Options.getLocalOrGlobal(
"StrictMode", false)) {}
134 void UnusedParametersCheck::warnOnUnusedParameter(
135 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
136 unsigned ParamIndex) {
137 const auto *Param = Function->getParamDecl(ParamIndex);
139 if (Param->isInvalidDecl())
141 auto MyDiag =
diag(Param->getLocation(),
"parameter %0 is unused") << Param;
144 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
148 if (Function->isExternallyVisible() ||
149 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
151 isLambdaCallOperator(Function)) {
154 if (!Result.Context->getLangOpts().CPlusPlus)
157 SourceRange RemovalRange(Param->getLocation());
161 MyDiag << FixItHint::CreateReplacement(
162 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
167 for (
const FunctionDecl *FD : Function->redecls())
168 if (FD->param_size())
172 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
173 if (ParamIndex < Call->getNumArgs())
178 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
179 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
181 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function))
182 if (Method->isLambdaStaticInvoker())
184 for (
unsigned I = 0,
E = Function->getNumParams(); I !=
E; ++I) {
185 const auto *Param = Function->getParamDecl(I);
186 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
187 Param->hasAttr<UnusedAttr>())
193 (Function->getBody()->child_begin() !=
194 Function->getBody()->child_end()) ||
195 (isa<CXXConstructorDecl>(Function) &&
196 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
197 warnOnUnusedParameter(Result, Function, I);