1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
//===- ObjCMessage.h - Wrapper for ObjC messages and dot syntax ---*- C++ -*--//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines ObjCMessage which serves as a common wrapper for ObjC
// message expressions or implicit messages for loading/storing ObjC properties.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_STATICANALYZER_PATHSENSITIVE_OBJCMESSAGE
#define LLVM_CLANG_STATICANALYZER_PATHSENSITIVE_OBJCMESSAGE
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/ExprCXX.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Compiler.h"
namespace clang {
namespace ento {
using llvm::StrInStrNoCase;
/// \brief Represents both explicit ObjC message expressions and implicit
/// messages that are sent for handling properties in dot syntax.
class ObjCMessage {
const ObjCMessageExpr *Msg;
const ObjCPropertyRefExpr *PE;
const bool IsPropSetter;
public:
ObjCMessage() : Msg(0), PE(0), IsPropSetter(false) {}
ObjCMessage(const ObjCMessageExpr *E, const ObjCPropertyRefExpr *pe = 0,
bool isSetter = false)
: Msg(E), PE(pe), IsPropSetter(isSetter) {
assert(E && "should not be initialized with null expression");
}
bool isValid() const { return Msg; }
bool isPureMessageExpr() const { return !PE; }
bool isPropertyGetter() const { return PE && !IsPropSetter; }
bool isPropertySetter() const {
return IsPropSetter;
}
const Expr *getMessageExpr() const {
return Msg;
}
QualType getType(ASTContext &ctx) const {
return Msg->getType();
}
QualType getResultType(ASTContext &ctx) const {
if (const ObjCMethodDecl *MD = Msg->getMethodDecl())
return MD->getResultType();
return getType(ctx);
}
ObjCMethodFamily getMethodFamily() const {
return Msg->getMethodFamily();
}
Selector getSelector() const {
return Msg->getSelector();
}
const Expr *getInstanceReceiver() const {
return Msg->getInstanceReceiver();
}
SVal getInstanceReceiverSVal(ProgramStateRef State,
const LocationContext *LC) const {
if (!isInstanceMessage())
return UndefinedVal();
if (const Expr *Ex = getInstanceReceiver())
return State->getSValAsScalarOrLoc(Ex, LC);
// An instance message with no expression means we are sending to super.
// In this case the object reference is the same as 'self'.
const ImplicitParamDecl *SelfDecl = LC->getSelfDecl();
assert(SelfDecl && "No message receiver Expr, but not in an ObjC method");
return State->getSVal(State->getRegion(SelfDecl, LC));
}
bool isInstanceMessage() const {
return Msg->isInstanceMessage();
}
const ObjCMethodDecl *getMethodDecl() const {
return Msg->getMethodDecl();
}
const ObjCInterfaceDecl *getReceiverInterface() const {
return Msg->getReceiverInterface();
}
SourceLocation getSuperLoc() const {
if (PE)
return PE->getReceiverLocation();
return Msg->getSuperLoc();
}
SourceRange getSourceRange() const LLVM_READONLY {
if (PE)
return PE->getSourceRange();
return Msg->getSourceRange();
}
unsigned getNumArgs() const {
return Msg->getNumArgs();
}
SVal getArgSVal(unsigned i,
const LocationContext *LCtx,
ProgramStateRef state) const {
assert(i < getNumArgs() && "Invalid index for argument");
return state->getSVal(Msg->getArg(i), LCtx);
}
QualType getArgType(unsigned i) const {
assert(i < getNumArgs() && "Invalid index for argument");
return Msg->getArg(i)->getType();
}
const Expr *getArgExpr(unsigned i) const {
assert(i < getNumArgs() && "Invalid index for argument");
return Msg->getArg(i);
}
SourceRange getArgSourceRange(unsigned i) const {
const Expr *argE = getArgExpr(i);
return argE->getSourceRange();
}
SourceRange getReceiverSourceRange() const {
if (PE) {
if (PE->isObjectReceiver())
return PE->getBase()->getSourceRange();
}
else {
return Msg->getReceiverRange();
}
// FIXME: This isn't a range.
return PE->getReceiverLocation();
}
};
/// \brief Common wrapper for a call expression, ObjC message, or C++
/// constructor, mainly to provide a common interface for their arguments.
class CallOrObjCMessage {
llvm::PointerUnion<const CallExpr *, const CXXConstructExpr *> CallE;
ObjCMessage Msg;
ProgramStateRef State;
const LocationContext *LCtx;
bool isCallbackArg(unsigned Idx, const Type *T) const;
public:
CallOrObjCMessage(const CallExpr *callE, ProgramStateRef state,
const LocationContext *lctx)
: CallE(callE), State(state), LCtx(lctx) {}
CallOrObjCMessage(const CXXConstructExpr *consE, ProgramStateRef state,
const LocationContext *lctx)
: CallE(consE), State(state), LCtx(lctx) {}
CallOrObjCMessage(const ObjCMessage &msg, ProgramStateRef state,
const LocationContext *lctx)
: CallE((CallExpr *)0), Msg(msg), State(state), LCtx(lctx) {}
QualType getResultType(ASTContext &ctx) const;
bool isFunctionCall() const {
return CallE && CallE.is<const CallExpr *>();
}
bool isCXXConstructExpr() const {
return CallE && CallE.is<const CXXConstructExpr *>();
}
bool isObjCMessage() const {
return !CallE;
}
bool isCXXCall() const {
const CallExpr *ActualCallE = CallE.dyn_cast<const CallExpr *>();
return ActualCallE && isa<CXXMemberCallExpr>(ActualCallE);
}
/// Check if the callee is declared in the system header.
bool isInSystemHeader() const {
if (const Decl *FD = getDecl()) {
const SourceManager &SM =
State->getStateManager().getContext().getSourceManager();
return SM.isInSystemHeader(FD->getLocation());
}
return false;
}
const Expr *getOriginExpr() const {
if (!CallE)
return Msg.getMessageExpr();
if (const CXXConstructExpr *Ctor =
CallE.dyn_cast<const CXXConstructExpr *>())
return Ctor;
return CallE.get<const CallExpr *>();
}
SVal getFunctionCallee() const;
SVal getCXXCallee() const;
SVal getInstanceMessageReceiver(const LocationContext *LC) const;
/// Get the declaration of the function or method.
const Decl *getDecl() const;
unsigned getNumArgs() const {
if (!CallE)
return Msg.getNumArgs();
if (const CXXConstructExpr *Ctor =
CallE.dyn_cast<const CXXConstructExpr *>())
return Ctor->getNumArgs();
return CallE.get<const CallExpr *>()->getNumArgs();
}
SVal getArgSVal(unsigned i) const {
assert(i < getNumArgs());
if (!CallE)
return Msg.getArgSVal(i, LCtx, State);
return State->getSVal(getArg(i), LCtx);
}
const Expr *getArg(unsigned i) const {
assert(i < getNumArgs());
if (!CallE)
return Msg.getArgExpr(i);
if (const CXXConstructExpr *Ctor =
CallE.dyn_cast<const CXXConstructExpr *>())
return Ctor->getArg(i);
return CallE.get<const CallExpr *>()->getArg(i);
}
SourceRange getArgSourceRange(unsigned i) const {
assert(i < getNumArgs());
if (CallE)
return getArg(i)->getSourceRange();
return Msg.getArgSourceRange(i);
}
SourceRange getReceiverSourceRange()
|