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>
24bool isOverrideMethod(
const FunctionDecl *Function) {
25 if (
const auto *MD = dyn_cast<CXXMethodDecl>(Function))
26 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
32 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
33 hasAnyParameter(decl()),
34 unless(hasAttr(attr::Kind::Naked)))
40static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
41 const T *PrevNode,
const T *Node,
44 return CharSourceRange::getCharRange(
Node->getBeginLoc(),
45 NextNode->getBeginLoc());
48 return CharSourceRange::getTokenRange(
49 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
50 *Result.SourceManager,
51 Result.Context->getLangOpts()),
54 return CharSourceRange::getTokenRange(
Node->getSourceRange());
58 const FunctionDecl *Function,
unsigned Index) {
60 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
61 Function->getParamDecl(Index),
62 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
67 const CallExpr *Call,
unsigned Index) {
69 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
71 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
75 :
public RecursiveASTVisitor<IndexerVisitor> {
79 const std::unordered_set<const CallExpr *> &
81 return Index[Fn->getCanonicalDecl()].Calls;
84 const std::unordered_set<const DeclRefExpr *> &
86 return Index[Fn->getCanonicalDecl()].OtherRefs;
92 if (
const auto *Fn = dyn_cast<FunctionDecl>(
DeclRef->getDecl())) {
93 Fn = Fn->getCanonicalDecl();
94 Index[Fn].OtherRefs.insert(
DeclRef);
101 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
102 Fn = Fn->getCanonicalDecl();
103 if (
const auto *Ref =
104 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
105 Index[Fn].OtherRefs.erase(Ref);
107 Index[Fn].Calls.insert(Call);
114 std::unordered_set<const CallExpr *> Calls;
115 std::unordered_set<const DeclRefExpr *> OtherRefs;
118 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
126 StrictMode(Options.getLocalOrGlobal(
"StrictMode", false)),
127 IgnoreVirtual(Options.get(
"IgnoreVirtual", false)) {}
134void 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()) ||
150 !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function) ||
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 (IgnoreVirtual && Method->isVirtual())
184 if (Method->isLambdaStaticInvoker())
187 for (
unsigned I = 0,
E = Function->getNumParams(); I !=
E; ++I) {
188 const auto *Param = Function->getParamDecl(I);
189 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
190 Param->hasAttr<UnusedAttr>())
195 if (StrictMode || !Function->getBody()->children().empty() ||
196 (isa<CXXConstructorDecl>(Function) &&
197 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
198 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