clang-tools 8.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->getBeginLoc(),
45 NextNode->getBeginLoc());
46
47 if (PrevNode)
48 return CharSourceRange::getTokenRange(
49 Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0,
50 *Result.SourceManager,
51 Result.Context->getLangOpts()),
52 Node->getEndLoc());
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
74 class UnusedParametersCheck::IndexerVisitor
75 : public RecursiveASTVisitor<IndexerVisitor> {
76 public:
77 IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); }
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
128 void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
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>(*Result.Context);
140 }
141
142 // Comment out parameter name for non-local functions.
143 if (Function->isExternallyVisible() ||
144 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
145 !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function)) {
146 SourceRange RemovalRange(Param->getLocation());
147 // Note: We always add a space before the '/*' to not accidentally create a
148 // '*/*' for pointer types, which doesn't start a comment. clang-format will
149 // clean this up afterwards.
150 MyDiag << FixItHint::CreateReplacement(
151 RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
152 return;
153 }
154
155 // Fix all redeclarations.
156 for (const FunctionDecl *FD : Function->redecls())
157 if (FD->param_size())
158 MyDiag << removeParameter(Result, FD, ParamIndex);
159
160 // Fix all call sites.
161 for (const CallExpr *Call : Indexer->getFnCalls(Function))
162 if (ParamIndex < Call->getNumArgs()) // See PR38055 for example.
163 MyDiag << removeArgument(Result, Call, ParamIndex);
164 }
165
166 void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
167 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
168 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
169 return;
170 if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
171 if (Method->isLambdaStaticInvoker())
172 return;
173 for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
174 const auto *Param = Function->getParamDecl(i);
175 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
176 Param->hasAttr<UnusedAttr>())
177 continue;
178
179 // In non-strict mode ignore function definitions with empty bodies
180 // (constructor initializer counts for non-empty body).
181 if (StrictMode ||
182 (Function->getBody()->child_begin() !=
183 Function->getBody()->child_end()) ||
184 (isa<CXXConstructorDecl>(Function) &&
185 cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0))
186 warnOnUnusedParameter(Result, Function, i);
187 }
188 }
189
190 } // namespace misc
191 } // namespace tidy
192 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::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: ClangTidy.cpp:473
clang::tidy::bugprone::isOverrideMethod
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
Definition: VirtualNearMissCheck.cpp:31
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::IndexerVisitor
IndexerVisitor(ASTContext &Ctx)
Definition: UnusedParametersCheck.cpp:77
clang::tidy::misc::removeArgument
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
Definition: UnusedParametersCheck.cpp:66
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:128
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::shouldTraversePostOrder
bool shouldTraversePostOrder() const
Definition: UnusedParametersCheck.cpp:89
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor
Definition: UnusedParametersCheck.cpp:74
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::getFnCalls
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
Definition: UnusedParametersCheck.cpp:80
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
Ctx
Context Ctx
Definition: TUScheduler.cpp:224
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::WalkUpFromDeclRefExpr
bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef)
Definition: UnusedParametersCheck.cpp:91
UnusedParametersCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
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:85
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:166
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:100
clang::tidy::misc::removeNode
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
Definition: UnusedParametersCheck.cpp:40
clang::tidy::misc::UnusedParametersCheck::IndexerVisitor::WalkUpFromCallExpr
bool WalkUpFromCallExpr(CallExpr *Call)
Definition: UnusedParametersCheck.cpp:99
DeclRef
const DeclRefExpr * DeclRef
Definition: UseAfterMoveCheck.cpp:30
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438
clang::tidy::misc::removeParameter
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)
Definition: UnusedParametersCheck.cpp:57
Index
const SymbolIndex * Index
Definition: Dexp.cpp:85