clang-tools 22.0.0git
UnusedParametersCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
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>
20
21using namespace clang::ast_matchers;
22
23namespace clang::tidy::misc {
24
25namespace {
26bool 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
32bool hasAttrAfterParam(const SourceManager *SourceManager,
33 const ParmVarDecl *Param) {
34 for (const auto *Attr : Param->attrs()) {
35 if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
36 Attr->getLocation())) {
37 return true;
38 }
39 }
40 return false;
41}
42} // namespace
43
44void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
45 Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()),
46 hasAnyParameter(decl()),
47 unless(hasAttr(attr::Kind::Naked)))
48 .bind("function"),
49 this);
50}
51
52template <typename T>
53static CharSourceRange removeNode(const MatchFinder::MatchResult &Result,
54 const T *PrevNode, const T *Node,
55 const T *NextNode) {
56 if (NextNode)
57 return CharSourceRange::getCharRange(Node->getBeginLoc(),
58 NextNode->getBeginLoc());
59
60 if (PrevNode)
61 return CharSourceRange::getTokenRange(
62 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
63 *Result.SourceManager,
64 Result.Context->getLangOpts()),
65 Node->getEndLoc());
66
67 return CharSourceRange::getTokenRange(Node->getSourceRange());
68}
69
70static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
71 const FunctionDecl *Function, unsigned Index) {
72 return FixItHint::CreateRemoval(removeNode(
73 Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr,
74 Function->getParamDecl(Index),
75 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
76 : nullptr));
77}
78
79static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
80 const CallExpr *Call, unsigned Index) {
81 return FixItHint::CreateRemoval(removeNode(
82 Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
83 Call->getArg(Index),
84 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr));
85}
86
88 : public RecursiveASTVisitor<IndexerVisitor> {
89public:
90 IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); }
91
92 const std::unordered_set<const CallExpr *> &
93 getFnCalls(const FunctionDecl *Fn) {
94 return Index[Fn->getCanonicalDecl()].Calls;
95 }
96
97 const std::unordered_set<const DeclRefExpr *> &
98 getOtherRefs(const FunctionDecl *Fn) {
99 return Index[Fn->getCanonicalDecl()].OtherRefs;
100 }
101
102 bool shouldTraversePostOrder() const { return true; }
103
104 bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef) {
105 if (const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
106 Fn = Fn->getCanonicalDecl();
107 Index[Fn].OtherRefs.insert(DeclRef);
108 }
109 return true;
110 }
111
112 bool WalkUpFromCallExpr(CallExpr *Call) {
113 if (const auto *Fn =
114 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
115 Fn = Fn->getCanonicalDecl();
116 if (const auto *Ref =
117 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
118 Index[Fn].OtherRefs.erase(Ref);
119 }
120 Index[Fn].Calls.insert(Call);
121 }
122 return true;
123 }
124
125private:
126 struct IndexEntry {
127 std::unordered_set<const CallExpr *> Calls;
128 std::unordered_set<const DeclRefExpr *> OtherRefs;
129 };
130
131 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
132};
133
135
137 ClangTidyContext *Context)
138 : ClangTidyCheck(Name, Context),
139 StrictMode(Options.get("StrictMode", false)),
140 IgnoreVirtual(Options.get("IgnoreVirtual", false)) {}
141
143 Options.store(Opts, "StrictMode", StrictMode);
144 Options.store(Opts, "IgnoreVirtual", IgnoreVirtual);
145}
146
147void UnusedParametersCheck::warnOnUnusedParameter(
148 const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
149 unsigned ParamIndex) {
150 const auto *Param = Function->getParamDecl(ParamIndex);
151 // Don't bother to diagnose invalid parameters as being unused.
152 if (Param->isInvalidDecl())
153 return;
154 auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param;
155
156 if (!Indexer) {
157 Indexer = std::make_unique<IndexerVisitor>(*Result.Context);
158 }
159
160 // Cannot remove parameter for non-local functions.
161 if (Function->isExternallyVisible() ||
162 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
163 !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function) ||
164 isLambdaCallOperator(Function)) {
165
166 // It is illegal to omit parameter name here in C code, so early-out.
167 if (!Result.Context->getLangOpts().CPlusPlus)
168 return;
169
170 SourceRange RemovalRange(Param->getLocation());
171 // Note: We always add a space before the '/*' to not accidentally create
172 // a '*/*' for pointer types, which doesn't start a comment. clang-format
173 // will clean this up afterwards.
174 MyDiag << FixItHint::CreateReplacement(
175 RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
176 return;
177 }
178
179 // Fix all redeclarations.
180 for (const FunctionDecl *FD : Function->redecls())
181 if (FD->param_size())
182 MyDiag << removeParameter(Result, FD, ParamIndex);
183
184 // Fix all call sites.
185 for (const CallExpr *Call : Indexer->getFnCalls(Function))
186 if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
187 MyDiag << removeArgument(Result, Call, ParamIndex);
188}
189
190void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
191 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
192 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
193 return;
194 if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
195 if (IgnoreVirtual && Method->isVirtual())
196 return;
197 if (Method->isLambdaStaticInvoker())
198 return;
199 }
200 for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
201 const auto *Param = Function->getParamDecl(I);
202 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
203 Param->hasAttr<UnusedAttr>())
204 continue;
205 if (hasAttrAfterParam(Result.SourceManager, Param)) {
206 // Due to how grammar works, attributes would be wrongly applied to the
207 // type if we remove the preceding parameter name.
208 continue;
209 }
210
211 // In non-strict mode ignore function definitions with empty bodies
212 // (constructor initializer counts for non-empty body).
213 if (StrictMode || !Function->getBody()->children().empty() ||
214 (isa<CXXConstructorDecl>(Function) &&
215 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
216 warnOnUnusedParameter(Result, Function, I);
217 }
218}
219
220} // namespace clang::tidy::misc
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
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 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