clang: lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
24#include "llvm/ADT/STLExtras.h"
25
26using namespace clang;
27using namespace ento;
28
30typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
31
33 if (!S)
34 return;
35
36 if (const ObjCIvarRefExpr *Ex = dyn_cast(S)) {
38 IvarUsageMap::iterator I = M.find(D);
39 if (I != M.end())
40 I->second = Used;
41 return;
42 }
43
44
45 if (const BlockExpr *BE = dyn_cast(S)) {
46 Scan(M, BE->getBody());
47 return;
48 }
49
50 if (const PseudoObjectExpr *POE = dyn_cast(S))
51 for (const Expr *sub : POE->semantics()) {
52 if (const OpaqueValueExpr *OVE = dyn_cast(sub))
53 sub = OVE->getSourceExpr();
55 }
56
58 Scan(M, SubStmt);
59}
60
62 if (!D)
63 return;
64
66
67 if (!ID)
68 return;
69
70 IvarUsageMap::iterator I = M.find(ID);
71 if (I != M.end())
72 I->second = Used;
73}
74
76
78 Scan(M, I->getBody());
79
81
82
83 for (const auto *I : ID->property_impls())
85
86
87 for (const auto *Cat : ID->getClassInterface()->visible_categories()) {
90 }
91 }
92}
93
96 for (const auto *I : C->decls())
97 if (const auto *FD = dyn_cast(I)) {
99 if (SM.getFileID(L) == FID)
100 Scan(M, FD->getBody());
101 }
102}
103
107
110
111
112 for (const auto *Ivar : ID->ivars()) {
113
114
115
116
117
119 Ivar->hasAttr() || Ivar->hasAttr() ||
120 Ivar->hasAttr() || Ivar->isUnnamedBitField())
121 continue;
122
124 }
125
126 if (M.empty())
127 return;
128
129
131
132
133 bool hasUnused = false;
134 for (IVarState State : llvm::make_second_range(M))
135 if (State == Unused) {
136 hasUnused = true;
137 break;
138 }
139
140 if (!hasUnused)
141 return;
142
143
144
145
146
147
150
151
152 for (auto [Ivar, State] : M)
153 if (State == Unused) {
154 std::string sbuf;
155 llvm::raw_string_ostream os(sbuf);
156 os << "Instance variable '" << *Ivar << "' in class '" << *ID
157 << "' is never used by the methods in its @implementation "
158 "(although it may be used by category methods).";
159
163 "Optimization", os.str(), L);
164 }
165}
166
167
168
169
170
171namespace {
172class ObjCUnusedIvarsChecker : public Checker<
173 check::ASTDecl > {
174public:
175 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
176 BugReporter &BR) const {
178 }
179};
180}
181
182void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) {
184}
185
186bool ento::shouldRegisterObjCUnusedIvarsChecker(const CheckerManager &mgr) {
187 return true;
188}
static void checkObjCUnusedIvar(const ObjCImplementationDecl *D, BugReporter &BR, const CheckerBase *Checker)
Definition ObjCUnusedIVarsChecker.cpp:104
llvm::DenseMap< const ObjCIvarDecl *, IVarState > IvarUsageMap
Definition ObjCUnusedIVarsChecker.cpp:30
IVarState
Definition ObjCUnusedIVarsChecker.cpp:29
@ Used
Definition ObjCUnusedIVarsChecker.cpp:29
@ Unused
Definition ObjCUnusedIVarsChecker.cpp:29
static void Scan(IvarUsageMap &M, const Stmt *S)
Definition ObjCUnusedIVarsChecker.cpp:32
Defines the SourceManager interface.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
SourceLocation getLocation() const
DeclContext * getDeclContext()
This represents one expression.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
ObjCCategoryImplDecl - An object of this class encapsulates a category @implementation declaration.
ObjCContainerDecl - Represents a container for method declarations.
instmeth_range instance_methods() const
const ObjCInterfaceDecl * getClassInterface() const
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Represents an ObjC class declaration.
ObjCIvarDecl - Represents an ObjC instance variable.
ObjCIvarRefExpr - A reference to an ObjC instance variable.
ObjCPropertyImplDecl - Represents implementation declaration of a property in a class or category imp...
ObjCIvarDecl * getPropertyIvarDecl() const
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Stmt - This represents one statement.
BugReporter is a utility class for generating PathDiagnostics for analysis.
const SourceManager & getSourceManager()
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerFrontend *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges={}, ArrayRef< FixItHint > Fixits={})
The non-templated common ancestor of all the simple Checker<...> classes.
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
static PathDiagnosticLocation create(const Decl *D, const SourceManager &SM)
Create a location corresponding to the given declaration.
The JSON file list parser is used to communicate input to InstallAPI.