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 "llvm/ADT/STLExtras.h"
19#include <unordered_map>
20#include <unordered_set>
27bool isOverrideMethod(
const FunctionDecl *Function) {
28 if (
const auto *MD = dyn_cast<CXXMethodDecl>(Function))
29 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
33bool hasAttrAfterParam(
const SourceManager *SourceManager,
34 const ParmVarDecl *Param) {
35 for (
const auto *Attr : Param->attrs()) {
36 if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
37 Attr->getLocation())) {
46 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
47 hasAnyParameter(decl()),
48 unless(hasAttr(attr::Kind::Naked)))
54static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
55 const T *PrevNode,
const T *Node,
58 return CharSourceRange::getCharRange(
Node->getBeginLoc(),
59 NextNode->getBeginLoc());
62 return CharSourceRange::getTokenRange(
63 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
64 *Result.SourceManager,
65 Result.Context->getLangOpts()),
68 return CharSourceRange::getTokenRange(
Node->getSourceRange());
72 const FunctionDecl *Function,
unsigned Index) {
74 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
75 Function->getParamDecl(Index),
76 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
81 const CallExpr *Call,
unsigned Index) {
83 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
85 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
89 :
public RecursiveASTVisitor<IndexerVisitor> {
93 const std::unordered_set<const CallExpr *> &
95 return Index[Fn->getCanonicalDecl()].Calls;
98 const std::unordered_set<const DeclRefExpr *> &
100 return Index[Fn->getCanonicalDecl()].OtherRefs;
106 if (
const auto *Fn = dyn_cast<FunctionDecl>(
DeclRef->getDecl())) {
107 Fn = Fn->getCanonicalDecl();
108 Index[Fn].OtherRefs.insert(
DeclRef);
115 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
116 Fn = Fn->getCanonicalDecl();
117 if (
const auto *Ref =
118 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
119 Index[Fn].OtherRefs.erase(Ref);
121 Index[Fn].Calls.insert(Call);
128 std::unordered_set<const CallExpr *> Calls;
129 std::unordered_set<const DeclRefExpr *> OtherRefs;
132 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
140 StrictMode(Options.getLocalOrGlobal(
"StrictMode", false)),
141 IgnoreVirtual(Options.get(
"IgnoreVirtual", false)) {}
148void UnusedParametersCheck::warnOnUnusedParameter(
149 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
150 unsigned ParamIndex) {
151 const auto *Param = Function->getParamDecl(ParamIndex);
153 if (Param->isInvalidDecl())
155 auto MyDiag =
diag(Param->getLocation(),
"parameter %0 is unused") << Param;
158 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
162 if (Function->isExternallyVisible() ||
163 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
164 !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function) ||
165 isLambdaCallOperator(Function)) {
168 if (!Result.Context->getLangOpts().CPlusPlus)
171 SourceRange RemovalRange(Param->getLocation());
175 MyDiag << FixItHint::CreateReplacement(
176 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
181 for (
const FunctionDecl *FD : Function->redecls())
182 if (FD->param_size())
186 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
187 if (ParamIndex < Call->getNumArgs())
192 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
193 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
195 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
196 if (IgnoreVirtual && Method->isVirtual())
198 if (Method->isLambdaStaticInvoker())
201 for (
unsigned I = 0,
E = Function->getNumParams(); I !=
E; ++I) {
202 const auto *Param = Function->getParamDecl(I);
203 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
204 Param->hasAttr<UnusedAttr>())
206 if (hasAttrAfterParam(Result.SourceManager, Param)) {
214 if (StrictMode || !Function->getBody()->children().empty() ||
215 (isa<CXXConstructorDecl>(Function) &&
216 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
217 warnOnUnusedParameter(Result, Function, I);
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
const DeclRefExpr * DeclRef
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
UnusedParametersCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
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