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