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>
26 if (
const auto *MD = dyn_cast<CXXMethodDecl>(Function))
27 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
32 const ParmVarDecl *Param) {
33 for (
const auto *Attr : Param->attrs()) {
34 if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
35 Attr->getLocation())) {
43 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
44 hasAnyParameter(decl()),
45 unless(hasAttr(attr::Kind::Naked)))
51static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
52 const T *PrevNode,
const T *Node,
55 return CharSourceRange::getCharRange(Node->getBeginLoc(),
56 NextNode->getBeginLoc());
59 return CharSourceRange::getTokenRange(
60 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
61 *Result.SourceManager,
62 Result.Context->getLangOpts()),
65 return CharSourceRange::getTokenRange(Node->getSourceRange());
69 const FunctionDecl *Function,
unsigned Index) {
71 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
72 Function->getParamDecl(Index),
73 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
78 const CallExpr *Call,
unsigned Index) {
80 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
82 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
86 :
public RecursiveASTVisitor<IndexerVisitor> {
90 const std::unordered_set<const CallExpr *> &
92 return Index[Fn->getCanonicalDecl()].Calls;
95 const std::unordered_set<const DeclRefExpr *> &
97 return Index[Fn->getCanonicalDecl()].OtherRefs;
103 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
104 Fn = Fn->getCanonicalDecl();
105 Index[Fn].OtherRefs.insert(DeclRef);
112 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
113 Fn = Fn->getCanonicalDecl();
114 if (
const auto *Ref =
115 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
116 Index[Fn].OtherRefs.erase(Ref);
118 Index[Fn].Calls.insert(Call);
125 std::unordered_set<const CallExpr *> Calls;
126 std::unordered_set<const DeclRefExpr *> OtherRefs;
129 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
137 StrictMode(Options.get(
"StrictMode", false)),
138 IgnoreVirtual(Options.get(
"IgnoreVirtual", false)) {}
141 Options.store(Opts,
"StrictMode", StrictMode);
142 Options.store(Opts,
"IgnoreVirtual", IgnoreVirtual);
145void UnusedParametersCheck::warnOnUnusedParameter(
146 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
147 unsigned ParamIndex) {
148 const auto *Param = Function->getParamDecl(ParamIndex);
150 if (Param->isInvalidDecl())
152 auto MyDiag = diag(Param->getLocation(),
"parameter %0 is unused") << Param;
155 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
159 if (Function->isExternallyVisible() ||
160 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
162 isLambdaCallOperator(Function)) {
165 if (!Result.Context->getLangOpts().CPlusPlus)
168 SourceRange RemovalRange(Param->getLocation());
172 MyDiag << FixItHint::CreateReplacement(
173 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
178 for (
const FunctionDecl *FD : Function->redecls())
179 if (FD->param_size())
183 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
184 if (ParamIndex < Call->getNumArgs())
189 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
190 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
192 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
193 if (IgnoreVirtual && Method->isVirtual())
195 if (Method->isLambdaStaticInvoker())
198 for (
unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
199 const auto *Param = Function->getParamDecl(I);
200 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
201 Param->hasAttr<UnusedAttr>())
211 if (StrictMode || !Function->getBody()->children().empty() ||
212 (isa<CXXConstructorDecl>(Function) &&
213 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
214 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 bool hasAttrAfterParam(const SourceManager *SourceManager, const ParmVarDecl *Param)
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 bool isOverrideMethod(const FunctionDecl *Function)
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
llvm::StringMap< ClangTidyValue > OptionMap