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
19 using namespace clang::ast_matchers;
20
21 namespace clang {
22 namespace tidy {
23 namespace misc {
24
25 namespace {
isOverrideMethod(const FunctionDecl * Function)26 bool isOverrideMethod(const FunctionDecl *Function) {
27 if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
28 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
29 return false;
30 }
31 } // namespace
32
registerMatchers(MatchFinder * Finder)33 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
34 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
35 hasAnyParameter(decl()),
36 unless(hasAttr(attr::Kind::Naked)))
37 .bind("function"),
38 this);
39 }
40
41 template <typename T>
removeNode(const MatchFinder::MatchResult & Result,const T * PrevNode,const T * Node,const T * NextNode)42 static CharSourceRange removeNode(const MatchFinder::MatchResult &Result,
43 const T *PrevNode, const T *Node,
44 const T *NextNode) {
45 if (NextNode)
46 return CharSourceRange::getCharRange(Node->getBeginLoc(),
47 NextNode->getBeginLoc());
48
49 if (PrevNode)
50 return CharSourceRange::getTokenRange(
51 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
52 *Result.SourceManager,
53 Result.Context->getLangOpts()),
54 Node->getEndLoc());
55
56 return CharSourceRange::getTokenRange(Node->getSourceRange());
57 }
58
removeParameter(const MatchFinder::MatchResult & Result,const FunctionDecl * Function,unsigned Index)59 static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
60 const FunctionDecl *Function, unsigned Index) {
61 return FixItHint::CreateRemoval(removeNode(
62 Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr,
63 Function->getParamDecl(Index),
64 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
65 : nullptr));
66 }
67
removeArgument(const MatchFinder::MatchResult & Result,const CallExpr * Call,unsigned Index)68 static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
69 const CallExpr *Call, unsigned Index) {
70 return FixItHint::CreateRemoval(removeNode(
71 Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
72 Call->getArg(Index),
73 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr));
74 }
75
76 class UnusedParametersCheck::IndexerVisitor
77 : public RecursiveASTVisitor<IndexerVisitor> {
78 public:
IndexerVisitor(ASTContext & Ctx)79 IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); }
80
81 const std::unordered_set<const CallExpr *> &
getFnCalls(const FunctionDecl * Fn)82 getFnCalls(const FunctionDecl *Fn) {
83 return Index[Fn->getCanonicalDecl()].Calls;
84 }
85
86 const std::unordered_set<const DeclRefExpr *> &
getOtherRefs(const FunctionDecl * Fn)87 getOtherRefs(const FunctionDecl *Fn) {
88 return Index[Fn->getCanonicalDecl()].OtherRefs;
89 }
90
shouldTraversePostOrder() const91 bool shouldTraversePostOrder() const { return true; }
92
WalkUpFromDeclRefExpr(DeclRefExpr * DeclRef)93 bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef) {
94 if (const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
95 Fn = Fn->getCanonicalDecl();
96 Index[Fn].OtherRefs.insert(DeclRef);
97 }
98 return true;
99 }
100
WalkUpFromCallExpr(CallExpr * Call)101 bool WalkUpFromCallExpr(CallExpr *Call) {
102 if (const auto *Fn =
103 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
104 Fn = Fn->getCanonicalDecl();
105 if (const auto *Ref =
106 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
107 Index[Fn].OtherRefs.erase(Ref);
108 }
109 Index[Fn].Calls.insert(Call);
110 }
111 return true;
112 }
113
114 private:
115 struct IndexEntry {
116 std::unordered_set<const CallExpr *> Calls;
117 std::unordered_set<const DeclRefExpr *> OtherRefs;
118 };
119
120 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
121 };
122
123 UnusedParametersCheck::~UnusedParametersCheck() = default;
124
UnusedParametersCheck(StringRef Name,ClangTidyContext * Context)125 UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
126 ClangTidyContext *Context)
127 : ClangTidyCheck(Name, Context),
128 StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
129
storeOptions(ClangTidyOptions::OptionMap & Opts)130 void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
131 Options.store(Opts, "StrictMode", StrictMode);
132 }
133
warnOnUnusedParameter(const MatchFinder::MatchResult & Result,const FunctionDecl * Function,unsigned ParamIndex)134 void UnusedParametersCheck::warnOnUnusedParameter(
135 const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
136 unsigned ParamIndex) {
137 const auto *Param = Function->getParamDecl(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>(*Result.Context);
145 }
146
147 // Cannot remove parameter for non-local functions.
148 if (Function->isExternallyVisible() ||
149 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
150 !Indexer->getOtherRefs(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(Function))
173 if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
174 MyDiag << removeArgument(Result, Call, ParamIndex);
175 }
176
check(const MatchFinder::MatchResult & Result)177 void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
178 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
179 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
180 return;
181 if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
182 if (Method->isLambdaStaticInvoker())
183 return;
184 for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
185 const auto *Param = Function->getParamDecl(I);
186 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
187 Param->hasAttr<UnusedAttr>())
188 continue;
189
190 // In non-strict mode ignore function definitions with empty bodies
191 // (constructor initializer counts for non-empty body).
192 if (StrictMode ||
193 (Function->getBody()->child_begin() !=
194 Function->getBody()->child_end()) ||
195 (isa<CXXConstructorDecl>(Function) &&
196 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
197 warnOnUnusedParameter(Result, Function, I);
198 }
199 }
200
201 } // namespace misc
202 } // namespace tidy
203 } // namespace clang
204