1 //==- ObjCUnusedIVarsChecker.cpp - Check for unused ivars --------*- C++ -*-==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a CheckObjCUnusedIvars, a checker that
10 // analyzes an Objective-C class's interface/implementation to determine if it
11 // has any ivars that are never accessed.
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/ExprObjC.h"
19 #include "clang/Analysis/PathDiagnostic.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
23 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
24 #include "clang/StaticAnalyzer/Core/Checker.h"
25 #include "llvm/ADT/STLExtras.h"
27 using namespace clang
;
30 enum IVarState
{ Unused
, Used
};
31 typedef llvm::DenseMap
<const ObjCIvarDecl
*,IVarState
> IvarUsageMap
;
33 static void Scan(IvarUsageMap
& M
, const Stmt
*S
) {
37 if (const ObjCIvarRefExpr
*Ex
= dyn_cast
<ObjCIvarRefExpr
>(S
)) {
38 const ObjCIvarDecl
*D
= Ex
->getDecl();
39 IvarUsageMap::iterator I
= M
.find(D
);
45 // Blocks can reference an instance variable of a class.
46 if (const BlockExpr
*BE
= dyn_cast
<BlockExpr
>(S
)) {
47 Scan(M
, BE
->getBody());
51 if (const PseudoObjectExpr
*POE
= dyn_cast
<PseudoObjectExpr
>(S
))
52 for (const Expr
*sub
: POE
->semantics()) {
53 if (const OpaqueValueExpr
*OVE
= dyn_cast
<OpaqueValueExpr
>(sub
))
54 sub
= OVE
->getSourceExpr();
58 for (const Stmt
*SubStmt
: S
->children())
62 static void Scan(IvarUsageMap
& M
, const ObjCPropertyImplDecl
*D
) {
66 const ObjCIvarDecl
*ID
= D
->getPropertyIvarDecl();
71 IvarUsageMap::iterator I
= M
.find(ID
);
76 static void Scan(IvarUsageMap
& M
, const ObjCContainerDecl
*D
) {
77 // Scan the methods for accesses.
78 for (const auto *I
: D
->instance_methods())
79 Scan(M
, I
->getBody());
81 if (const ObjCImplementationDecl
*ID
= dyn_cast
<ObjCImplementationDecl
>(D
)) {
82 // Scan for @synthesized property methods that act as setters/getters
84 for (const auto *I
: ID
->property_impls())
87 // Scan the associated categories as well.
88 for (const auto *Cat
: ID
->getClassInterface()->visible_categories()) {
89 if (const ObjCCategoryImplDecl
*CID
= Cat
->getImplementation())
95 static void Scan(IvarUsageMap
&M
, const DeclContext
*C
, const FileID FID
,
96 const SourceManager
&SM
) {
97 for (const auto *I
: C
->decls())
98 if (const auto *FD
= dyn_cast
<FunctionDecl
>(I
)) {
99 SourceLocation L
= FD
->getBeginLoc();
100 if (SM
.getFileID(L
) == FID
)
101 Scan(M
, FD
->getBody());
105 static void checkObjCUnusedIvar(const ObjCImplementationDecl
*D
,
107 const CheckerBase
*Checker
) {
109 const ObjCInterfaceDecl
*ID
= D
->getClassInterface();
112 // Iterate over the ivars.
113 for (const auto *Ivar
: ID
->ivars()) {
114 // Ignore ivars that...
115 // (a) aren't private
116 // (b) explicitly marked unused
118 // (d) are unnamed bitfields
119 if (Ivar
->getAccessControl() != ObjCIvarDecl::Private
||
120 Ivar
->hasAttr
<UnusedAttr
>() || Ivar
->hasAttr
<IBOutletAttr
>() ||
121 Ivar
->hasAttr
<IBOutletCollectionAttr
>() ||
122 Ivar
->isUnnamedBitfield())
131 // Now scan the implementation declaration.
134 // Any potentially unused ivars?
135 bool hasUnused
= false;
136 for (IVarState State
: llvm::make_second_range(M
))
137 if (State
== Unused
) {
145 // We found some potentially unused ivars. Scan the entire translation unit
146 // for functions inside the @implementation that reference these ivars.
147 // FIXME: In the future hopefully we can just use the lexical DeclContext
148 // to go from the ObjCImplementationDecl to the lexically "nested"
150 const SourceManager
&SM
= BR
.getSourceManager();
151 Scan(M
, D
->getDeclContext(), SM
.getFileID(D
->getLocation()), SM
);
153 // Find ivars that are unused.
154 for (auto [Ivar
, State
] : M
)
155 if (State
== Unused
) {
157 llvm::raw_string_ostream
os(sbuf
);
158 os
<< "Instance variable '" << *Ivar
<< "' in class '" << *ID
159 << "' is never used by the methods in its @implementation "
160 "(although it may be used by category methods).";
162 PathDiagnosticLocation L
=
163 PathDiagnosticLocation::create(Ivar
, BR
.getSourceManager());
164 BR
.EmitBasicReport(D
, Checker
, "Unused instance variable", "Optimization",
169 //===----------------------------------------------------------------------===//
170 // ObjCUnusedIvarsChecker
171 //===----------------------------------------------------------------------===//
174 class ObjCUnusedIvarsChecker
: public Checker
<
175 check::ASTDecl
<ObjCImplementationDecl
> > {
177 void checkASTDecl(const ObjCImplementationDecl
*D
, AnalysisManager
& mgr
,
178 BugReporter
&BR
) const {
179 checkObjCUnusedIvar(D
, BR
, this);
184 void ento::registerObjCUnusedIvarsChecker(CheckerManager
&mgr
) {
185 mgr
.registerChecker
<ObjCUnusedIvarsChecker
>();
188 bool ento::shouldRegisterObjCUnusedIvarsChecker(const CheckerManager
&mgr
) {