clang-tools  3.9.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/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace misc {
20 
21 namespace {
22 bool isOverrideMethod(const FunctionDecl *Function) {
23  if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
24  return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
25  return false;
26 }
27 } // namespace
28 
29 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
30  Finder->addMatcher(functionDecl().bind("function"), this);
31 }
32 
33 template <typename T>
34 static CharSourceRange removeNode(const MatchFinder::MatchResult &Result,
35  const T *PrevNode, const T *Node,
36  const T *NextNode) {
37  if (NextNode)
38  return CharSourceRange::getCharRange(Node->getLocStart(),
39  NextNode->getLocStart());
40 
41  if (PrevNode)
42  return CharSourceRange::getTokenRange(
43  Lexer::getLocForEndOfToken(PrevNode->getLocEnd(), 0,
44  *Result.SourceManager,
45  Result.Context->getLangOpts()),
46  Node->getLocEnd());
47 
48  return CharSourceRange::getTokenRange(Node->getSourceRange());
49 }
50 
51 static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
52  const FunctionDecl *Function, unsigned Index) {
53  return FixItHint::CreateRemoval(removeNode(
54  Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr,
55  Function->getParamDecl(Index),
56  Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
57  : nullptr));
58 }
59 
60 static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
61  const CallExpr *Call, unsigned Index) {
62  return FixItHint::CreateRemoval(removeNode(
63  Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
64  Call->getArg(Index),
65  Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr));
66 }
67 
68 void UnusedParametersCheck::warnOnUnusedParameter(
69  const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
70  unsigned ParamIndex) {
71  const auto *Param = Function->getParamDecl(ParamIndex);
72  auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param;
73 
74  auto DeclRefExpr =
75  declRefExpr(to(equalsNode(Function)),
76  unless(hasAncestor(callExpr(callee(equalsNode(Function))))));
77 
78  // Comment out parameter name for non-local functions.
79  if (Function->isExternallyVisible() ||
80  !Result.SourceManager->isInMainFile(Function->getLocation()) ||
81  !ast_matchers::match(DeclRefExpr, *Result.Context).empty() ||
82  isOverrideMethod(Function)) {
83  SourceRange RemovalRange(Param->getLocation(), Param->getLocEnd());
84  // Note: We always add a space before the '/*' to not accidentally create a
85  // '*/*' for pointer types, which doesn't start a comment. clang-format will
86  // clean this up afterwards.
87  MyDiag << FixItHint::CreateReplacement(
88  RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
89  return;
90  }
91 
92  // Fix all redeclarations.
93  for (const FunctionDecl *FD : Function->redecls())
94  if (FD->param_size())
95  MyDiag << removeParameter(Result, FD, ParamIndex);
96 
97  // Fix all call sites.
98  auto CallMatches = ast_matchers::match(
99  decl(forEachDescendant(
100  callExpr(callee(functionDecl(equalsNode(Function)))).bind("x"))),
101  *Result.Context->getTranslationUnitDecl(), *Result.Context);
102  for (const auto &Match : CallMatches)
103  MyDiag << removeArgument(Result, Match.getNodeAs<CallExpr>("x"),
104  ParamIndex);
105 }
106 
107 void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
108  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
109  if (!Function->doesThisDeclarationHaveABody() ||
110  !Function->hasWrittenPrototype() ||
111  Function->isTemplateInstantiation())
112  return;
113  if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
114  if (Method->isLambdaStaticInvoker())
115  return;
116  for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
117  const auto *Param = Function->getParamDecl(i);
118  if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
119  Param->hasAttr<UnusedAttr>())
120  continue;
121  warnOnUnusedParameter(Result, Function, i);
122  }
123 }
124 
125 } // namespace misc
126 } // namespace tidy
127 } // namespace clang
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)
const NamedDecl * Result
Definition: USRFinder.cpp:137