clang-tools  7.0.0
UnusedParametersCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedParametersCheck.cpp - clang-tidy----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UnusedParametersCheck.h"
11 #include "clang/AST/ASTContext.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_set>
17 
18 using namespace clang::ast_matchers;
19 
20 namespace clang {
21 namespace tidy {
22 namespace misc {
23 
24 namespace {
25 bool isOverrideMethod(const FunctionDecl *Function) {
26  if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
27  return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
28  return false;
29 }
30 } // namespace
31 
32 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
33  Finder->addMatcher(
34  functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
35  .bind("function"),
36  this);
37 }
38 
39 template <typename T>
40 static 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->getLocStart(),
45  NextNode->getLocStart());
46 
47  if (PrevNode)
48  return CharSourceRange::getTokenRange(
49  Lexer::getLocForEndOfToken(PrevNode->getLocEnd(), 0,
50  *Result.SourceManager,
51  Result.Context->getLangOpts()),
52  Node->getLocEnd());
53 
54  return CharSourceRange::getTokenRange(Node->getSourceRange());
55 }
56 
57 static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
58  const FunctionDecl *Function, unsigned Index) {
59  return FixItHint::CreateRemoval(removeNode(
60  Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr,
61  Function->getParamDecl(Index),
62  Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
63  : nullptr));
64 }
65 
66 static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
67  const CallExpr *Call, unsigned Index) {
68  return FixItHint::CreateRemoval(removeNode(
69  Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
70  Call->getArg(Index),
71  Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr));
72 }
73 
75  : public RecursiveASTVisitor<IndexerVisitor> {
76 public:
77  IndexerVisitor(TranslationUnitDecl *Top) { TraverseDecl(Top); }
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>(DeclRef->getDecl())) {
93  Fn = Fn->getCanonicalDecl();
94  Index[Fn].OtherRefs.insert(DeclRef);
95  }
96  return true;
97  }
98 
99  bool WalkUpFromCallExpr(CallExpr *Call) {
100  if (const auto *Fn =
101  dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
102  Fn = Fn->getCanonicalDecl();
103  if (const auto *Ref =
104  dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
105  Index[Fn].OtherRefs.erase(Ref);
106  }
107  Index[Fn].Calls.insert(Call);
108  }
109  return true;
110  }
111 
112 private:
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 
121 UnusedParametersCheck::~UnusedParametersCheck() = default;
122 
123 UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
124  ClangTidyContext *Context)
125  : ClangTidyCheck(Name, Context),
126  StrictMode(Options.getLocalOrGlobal("StrictMode", 0) != 0) {}
127 
129  Options.store(Opts, "StrictMode", StrictMode);
130 }
131 
132 void UnusedParametersCheck::warnOnUnusedParameter(
133  const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
134  unsigned ParamIndex) {
135  const auto *Param = Function->getParamDecl(ParamIndex);
136  auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param;
137 
138  if (!Indexer) {
139  Indexer = llvm::make_unique<IndexerVisitor>(
140  Result.Context->getTranslationUnitDecl());
141  }
142 
143  // Comment out parameter name for non-local functions.
144  if (Function->isExternallyVisible() ||
145  !Result.SourceManager->isInMainFile(Function->getLocation()) ||
146  !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function)) {
147  SourceRange RemovalRange(Param->getLocation());
148  // Note: We always add a space before the '/*' to not accidentally create a
149  // '*/*' for pointer types, which doesn't start a comment. clang-format will
150  // clean this up afterwards.
151  MyDiag << FixItHint::CreateReplacement(
152  RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
153  return;
154  }
155 
156  // Fix all redeclarations.
157  for (const FunctionDecl *FD : Function->redecls())
158  if (FD->param_size())
159  MyDiag << removeParameter(Result, FD, ParamIndex);
160 
161  // Fix all call sites.
162  for (const CallExpr *Call : Indexer->getFnCalls(Function))
163  if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
164  MyDiag << removeArgument(Result, Call, ParamIndex);
165 }
166 
167 void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
168  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
169  if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
170  return;
171  if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
172  if (Method->isLambdaStaticInvoker())
173  return;
174  for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
175  const auto *Param = Function->getParamDecl(i);
176  if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
177  Param->hasAttr<UnusedAttr>())
178  continue;
179 
180  // In non-strict mode ignore function definitions with empty bodies
181  // (constructor initializer counts for non-empty body).
182  if (StrictMode ||
183  (Function->getBody()->child_begin() !=
184  Function->getBody()->child_end()) ||
185  (isa<CXXConstructorDecl>(Function) &&
186  cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
187  warnOnUnusedParameter(Result, Function, i);
188  }
189 }
190 
191 } // namespace misc
192 } // namespace tidy
193 } // namespace clang
llvm::StringRef Name
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: ClangTidy.cpp:460
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const std::unordered_set< const DeclRefExpr * > & getOtherRefs(const FunctionDecl *Fn)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:427
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)