1//===--- UnusedParametersCheck.cpp - clang-tidy----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "UnusedParametersCheck.h"
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>
18
19using namespace clang::ast_matchers;
20
21namespace clang::tidy::misc {
22
23namespace {
24bool isOverrideMethod(const FunctionDecl *Function) {
25 if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
26 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
27 return false;
28}
29} // namespace
30
31void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
32 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
33 hasAnyParameter(decl()),
34 unless(hasAttr(attr::Kind::Naked)))
35 .bind("function"),
36 this);
37}
38
39template <typename T>
40static CharSourceRange removeNode(const MatchFinder::MatchResult &Result,
41 const T *PrevNode, const T *Node,
42 const T *NextNode) {
43 if (NextNode)
44 return CharSourceRange::getCharRange(Node->getBeginLoc(),
45 NextNode->getBeginLoc());
46
47 if (PrevNode)
48 return CharSourceRange::getTokenRange(
49 Lexer::getLocForEndOfToken(Loc: PrevNode->getEndLoc(), Offset: 0,
50 SM: *Result.SourceManager,
51 LangOpts: Result.Context->getLangOpts()),
52 Node->getEndLoc());
53
54 return CharSourceRange::getTokenRange(Node->getSourceRange());
55}
56
57static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
58 const FunctionDecl *Function, unsigned Index) {
59 return FixItHint::CreateRemoval(RemoveRange: removeNode(
60 Result, PrevNode: Index > 0 ? Function->getParamDecl(i: Index - 1) : nullptr,
61 Node: Function->getParamDecl(i: Index),
62 NextNode: Index + 1 < Function->getNumParams() ? Function->getParamDecl(i: Index + 1)
63 : nullptr));
64}
65
66static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
67 const CallExpr *Call, unsigned Index) {
68 return FixItHint::CreateRemoval(RemoveRange: removeNode(
69 Result, PrevNode: Index > 0 ? Call->getArg(Arg: Index - 1) : nullptr,
70 Node: Call->getArg(Arg: Index),
71 NextNode: Index + 1 < Call->getNumArgs() ? Call->getArg(Arg: Index + 1) : nullptr));
72}
73
74class UnusedParametersCheck::IndexerVisitor
75 : public RecursiveASTVisitor<IndexerVisitor> {
76public:
77 IndexerVisitor(ASTContext &Ctx) { TraverseAST(AST&: Ctx); }
78
79 const std::unordered_set<const CallExpr *> &
80 getFnCalls(const FunctionDecl *Fn) {
81 return Index[Fn->getCanonicalDecl()].Calls;
82 }
83
84 const std::unordered_set<const DeclRefExpr *> &
85 getOtherRefs(const FunctionDecl *Fn) {
86 return Index[Fn->getCanonicalDecl()].OtherRefs;
87 }
88
89 bool shouldTraversePostOrder() const { return true; }
90
91 bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef) {
92 if (const auto *Fn = dyn_cast<FunctionDecl>(Val: DeclRef->getDecl())) {
93 Fn = Fn->getCanonicalDecl();
94 Index[Fn].OtherRefs.insert(x: DeclRef);
95 }
96 return true;
97 }
98
99 bool WalkUpFromCallExpr(CallExpr *Call) {
100 if (const auto *Fn =
101 dyn_cast_or_null<FunctionDecl>(Val: Call->getCalleeDecl())) {
102 Fn = Fn->getCanonicalDecl();
103 if (const auto *Ref =
104 dyn_cast<DeclRefExpr>(Val: Call->getCallee()->IgnoreImplicit())) {
105 Index[Fn].OtherRefs.erase(x: Ref);
106 }
107 Index[Fn].Calls.insert(x: Call);
108 }
109 return true;
110 }
111
112private:
113 struct IndexEntry {
114 std::unordered_set<const CallExpr *> Calls;
115 std::unordered_set<const DeclRefExpr *> OtherRefs;
116 };
117
118 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
119};
120
121UnusedParametersCheck::~UnusedParametersCheck() = default;
122
123UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
124 ClangTidyContext *Context)
125 : ClangTidyCheck(Name, Context),
126 StrictMode(Options.getLocalOrGlobal(LocalName: "StrictMode", Default: false)),
127 IgnoreVirtual(Options.get(LocalName: "IgnoreVirtual", Default: false)) {}
128
129void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
130 Options.store(Options&: Opts, LocalName: "StrictMode", Value: StrictMode);
131 Options.store(Options&: Opts, LocalName: "IgnoreVirtual", Value: IgnoreVirtual);
132}
133
134void UnusedParametersCheck::warnOnUnusedParameter(
135 const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
136 unsigned ParamIndex) {
137 const auto *Param = Function->getParamDecl(i: ParamIndex);
138 // Don't bother to diagnose invalid parameters as being unused.
139 if (Param->isInvalidDecl())
140 return;
141 auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param;
142
143 if (!Indexer) {
144 Indexer = std::make_unique<IndexerVisitor>(args&: *Result.Context);
145 }
146
147 // Cannot remove parameter for non-local functions.
148 if (Function->isExternallyVisible() ||
149 !Result.SourceManager->isInMainFile(Loc: Function->getLocation()) ||
150 !Indexer->getOtherRefs(Fn: Function).empty() || isOverrideMethod(Function) ||
151 isLambdaCallOperator(Function)) {
152
153 // It is illegal to omit parameter name here in C code, so early-out.
154 if (!Result.Context->getLangOpts().CPlusPlus)
155 return;
156
157 SourceRange RemovalRange(Param->getLocation());
158 // Note: We always add a space before the '/*' to not accidentally create
159 // a '*/*' for pointer types, which doesn't start a comment. clang-format
160 // will clean this up afterwards.
161 MyDiag << FixItHint::CreateReplacement(
162 RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
163 return;
164 }
165
166 // Fix all redeclarations.
167 for (const FunctionDecl *FD : Function->redecls())
168 if (FD->param_size())
169 MyDiag << removeParameter(Result, FD, ParamIndex);
170
171 // Fix all call sites.
172 for (const CallExpr *Call : Indexer->getFnCalls(Fn: Function))
173 if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
174 MyDiag << removeArgument(Result, Call, Index: ParamIndex);
175}
176
177void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
178 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(ID: "function");
179 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
180 return;
181 if (const auto *Method = dyn_cast<CXXMethodDecl>(Val: Function)) {
182 if (IgnoreVirtual && Method->isVirtual())
183 return;
184 if (Method->isLambdaStaticInvoker())
185 return;
186 }
187 for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
188 const auto *Param = Function->getParamDecl(i: I);
189 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
190 Param->hasAttr<UnusedAttr>())
191 continue;
192
193 // In non-strict mode ignore function definitions with empty bodies
194 // (constructor initializer counts for non-empty body).
195 if (StrictMode ||
196 (Function->getBody()->child_begin() !=
197 Function->getBody()->child_end()) ||
198 (isa<CXXConstructorDecl>(Val: Function) &&
199 cast<CXXConstructorDecl>(Val: Function)->getNumCtorInitializers() > 0))
200 warnOnUnusedParameter(Result, Function, ParamIndex: I);
201 }
202}
203
204} // namespace clang::tidy::misc
205

source code of clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp