clang-tools  15.0.0git
UnusedParametersCheck.cpp
Go to the documentation of this file.
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 
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 {
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 
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>
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 
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 
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 
77  : public RecursiveASTVisitor<IndexerVisitor> {
78 public:
79  IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); }
80 
81  const std::unordered_set<const CallExpr *> &
82  getFnCalls(const FunctionDecl *Fn) {
83  return Index[Fn->getCanonicalDecl()].Calls;
84  }
85 
86  const std::unordered_set<const DeclRefExpr *> &
87  getOtherRefs(const FunctionDecl *Fn) {
88  return Index[Fn->getCanonicalDecl()].OtherRefs;
89  }
90 
91  bool shouldTraversePostOrder() const { return true; }
92 
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 
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 
125 UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
126  ClangTidyContext *Context)
127  : ClangTidyCheck(Name, Context),
128  StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
129 
131  Options.store(Opts, "StrictMode", StrictMode);
132 }
133 
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 
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
UnusedParametersCheck.h
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor
Definition: UnusedParametersCheck.cpp:76
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::getOtherRefs
const std::unordered_set< const DeclRefExpr * > & getOtherRefs(const FunctionDecl *Fn)
Definition: UnusedParametersCheck.cpp:87
clang::tidy::misc::UnusedParametersCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UnusedParametersCheck.cpp:130
Ctx
Context Ctx
Definition: TUScheduler.cpp:547
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:53
clang::tidy::misc::removeNode
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
Definition: UnusedParametersCheck.cpp:42
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::WalkUpFromDeclRefExpr
bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef)
Definition: UnusedParametersCheck.cpp:93
clang::tidy::misc::removeParameter
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)
Definition: UnusedParametersCheck.cpp:59
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:415
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::getFnCalls
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
Definition: UnusedParametersCheck.cpp:82
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::WalkUpFromCallExpr
bool WalkUpFromCallExpr(CallExpr *Call)
Definition: UnusedParametersCheck.cpp:101
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
DeclRef
const DeclRefExpr * DeclRef
Definition: UseAfterMoveCheck.cpp:50
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:129
clang::tidy::bugprone::isOverrideMethod
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
Definition: VirtualNearMissCheck.cpp:30
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::IndexerVisitor
IndexerVisitor(ASTContext &Ctx)
Definition: UnusedParametersCheck.cpp:79
clang::tidy::misc::UnusedParametersCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UnusedParametersCheck.cpp:177
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::shouldTraversePostOrder
bool shouldTraversePostOrder() const
Definition: UnusedParametersCheck.cpp:91
clang::tidy::misc::removeArgument
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
Definition: UnusedParametersCheck.cpp:68