clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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