clang-tools  5.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 <unordered_set>
16 
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace misc {
22 
23 namespace {
24 bool isOverrideMethod(const FunctionDecl *Function) {
25  if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
26  return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
27  return false;
28 }
29 } // namespace
30 
31 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
32  Finder->addMatcher(
33  functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
34  .bind("function"),
35  this);
36 }
37 
38 template <typename T>
39 static CharSourceRange removeNode(const MatchFinder::MatchResult &Result,
40  const T *PrevNode, const T *Node,
41  const T *NextNode) {
42  if (NextNode)
43  return CharSourceRange::getCharRange(Node->getLocStart(),
44  NextNode->getLocStart());
45 
46  if (PrevNode)
47  return CharSourceRange::getTokenRange(
48  Lexer::getLocForEndOfToken(PrevNode->getLocEnd(), 0,
49  *Result.SourceManager,
50  Result.Context->getLangOpts()),
51  Node->getLocEnd());
52 
53  return CharSourceRange::getTokenRange(Node->getSourceRange());
54 }
55 
56 static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
57  const FunctionDecl *Function, unsigned Index) {
58  return FixItHint::CreateRemoval(removeNode(
59  Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr,
60  Function->getParamDecl(Index),
61  Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
62  : nullptr));
63 }
64 
65 static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
66  const CallExpr *Call, unsigned Index) {
67  return FixItHint::CreateRemoval(removeNode(
68  Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
69  Call->getArg(Index),
70  Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr));
71 }
72 
74  : public RecursiveASTVisitor<IndexerVisitor> {
75 public:
76  IndexerVisitor(TranslationUnitDecl *Top) { TraverseDecl(Top); }
77 
78  const std::unordered_set<const CallExpr *> &
79  getFnCalls(const FunctionDecl *Fn) {
80  return Index[Fn->getCanonicalDecl()].Calls;
81  }
82 
83  const std::unordered_set<const DeclRefExpr *> &
84  getOtherRefs(const FunctionDecl *Fn) {
85  return Index[Fn->getCanonicalDecl()].OtherRefs;
86  }
87 
88  bool shouldTraversePostOrder() const { return true; }
89 
90  bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef) {
91  if (const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
92  Fn = Fn->getCanonicalDecl();
93  Index[Fn].OtherRefs.insert(DeclRef);
94  }
95  return true;
96  }
97 
98  bool WalkUpFromCallExpr(CallExpr *Call) {
99  if (const auto *Fn =
100  dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
101  Fn = Fn->getCanonicalDecl();
102  if (const auto *Ref =
103  dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
104  Index[Fn].OtherRefs.erase(Ref);
105  }
106  Index[Fn].Calls.insert(Call);
107  }
108  return true;
109  }
110 
111 private:
112  struct IndexEntry {
113  std::unordered_set<const CallExpr *> Calls;
114  std::unordered_set<const DeclRefExpr *> OtherRefs;
115  };
116 
117  std::unordered_map<const FunctionDecl *, IndexEntry> Index;
118 };
119 
120 UnusedParametersCheck::~UnusedParametersCheck() = default;
121 
122 UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
124  : ClangTidyCheck(Name, Context) {}
125 
126 void UnusedParametersCheck::warnOnUnusedParameter(
127  const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
128  unsigned ParamIndex) {
129  const auto *Param = Function->getParamDecl(ParamIndex);
130  auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param;
131 
132  if (!Indexer) {
133  Indexer = llvm::make_unique<IndexerVisitor>(
134  Result.Context->getTranslationUnitDecl());
135  }
136 
137  // Comment out parameter name for non-local functions.
138  if (Function->isExternallyVisible() ||
139  !Result.SourceManager->isInMainFile(Function->getLocation()) ||
140  !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function)) {
141  SourceRange RemovalRange(Param->getLocation(), Param->getLocEnd());
142  // Note: We always add a space before the '/*' to not accidentally create a
143  // '*/*' for pointer types, which doesn't start a comment. clang-format will
144  // clean this up afterwards.
145  MyDiag << FixItHint::CreateReplacement(
146  RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
147  return;
148  }
149 
150  // Fix all redeclarations.
151  for (const FunctionDecl *FD : Function->redecls())
152  if (FD->param_size())
153  MyDiag << removeParameter(Result, FD, ParamIndex);
154 
155  // Fix all call sites.
156  for (const auto *Call : Indexer->getFnCalls(Function))
157  MyDiag << removeArgument(Result, Call, ParamIndex);
158 }
159 
160 void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
161  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
162  if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
163  return;
164  if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
165  if (Method->isLambdaStaticInvoker())
166  return;
167  for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
168  const auto *Param = Function->getParamDecl(i);
169  if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
170  Param->hasAttr<UnusedAttr>())
171  continue;
172  warnOnUnusedParameter(Result, Function, i);
173  }
174 }
175 
176 } // namespace misc
177 } // namespace tidy
178 } // namespace clang
StringHandle Name
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:275
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
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.
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
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's name.
Definition: ClangTidy.cpp:416
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)