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 return llvm::any_of(Param->attrs(), [&](
const Attr *Attr) {
34 return SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
40 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
41 hasAnyParameter(decl()),
42 unless(hasAttr(attr::Kind::Naked)))
48static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
49 const T *PrevNode,
const T *Node,
52 return CharSourceRange::getCharRange(Node->getBeginLoc(),
53 NextNode->getBeginLoc());
56 return CharSourceRange::getTokenRange(
57 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
58 *Result.SourceManager,
59 Result.Context->getLangOpts()),
62 return CharSourceRange::getTokenRange(Node->getSourceRange());
66 const FunctionDecl *Function,
unsigned Index) {
68 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
69 Function->getParamDecl(Index),
70 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
75 const CallExpr *Call,
unsigned Index) {
77 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
79 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
83 :
public RecursiveASTVisitor<IndexerVisitor> {
87 const std::unordered_set<const CallExpr *> &
89 return Index[Fn->getCanonicalDecl()].Calls;
92 const std::unordered_set<const DeclRefExpr *> &
94 return Index[Fn->getCanonicalDecl()].OtherRefs;
100 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
101 Fn = Fn->getCanonicalDecl();
102 Index[Fn].OtherRefs.insert(DeclRef);
109 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
110 Fn = Fn->getCanonicalDecl();
111 if (
const auto *Ref =
112 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
113 Index[Fn].OtherRefs.erase(Ref);
115 Index[Fn].Calls.insert(Call);
122 std::unordered_set<const CallExpr *> Calls;
123 std::unordered_set<const DeclRefExpr *> OtherRefs;
126 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
134 StrictMode(Options.get(
"StrictMode", false)),
135 IgnoreVirtual(Options.get(
"IgnoreVirtual", false)) {}
138 Options.store(Opts,
"StrictMode", StrictMode);
139 Options.store(Opts,
"IgnoreVirtual", IgnoreVirtual);
142void UnusedParametersCheck::warnOnUnusedParameter(
143 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
144 unsigned ParamIndex) {
145 const auto *Param = Function->getParamDecl(ParamIndex);
147 if (Param->isInvalidDecl())
149 auto MyDiag = diag(Param->getLocation(),
"parameter %0 is unused") << Param;
152 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
156 if (Function->isExternallyVisible() ||
157 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
159 isLambdaCallOperator(Function)) {
161 if (!Result.Context->getLangOpts().CPlusPlus)
164 const SourceRange RemovalRange(Param->getLocation());
168 MyDiag << FixItHint::CreateReplacement(
169 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
174 for (
const FunctionDecl *FD : Function->redecls())
175 if (FD->param_size())
179 for (
const CallExpr *Call : Indexer->getFnCalls(Function))
180 if (ParamIndex < Call->getNumArgs())
185 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
186 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
188 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
189 if (IgnoreVirtual && Method->isVirtual())
191 if (Method->isLambdaStaticInvoker())
194 for (
unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
195 const auto *Param = Function->getParamDecl(I);
196 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
197 Param->hasAttr<UnusedAttr>())
207 if (StrictMode || !Function->getBody()->children().empty() ||
208 (isa<CXXConstructorDecl>(Function) &&
209 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
210 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
~UnusedParametersCheck() 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