blob: 06ae32c3f1cbfa2f7bc93073fcb574533932f576 [file] [log] [blame]
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001//===- ThreadSafety.cpp ----------------------------------------*- C++ --*-===//
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// A intra-procedural analysis for thread safety (e.g. deadlocks and race
11// conditions), based off of an annotation system.
12//
Caitlin Sadowski19903462011-09-14 20:05:09 +000013// See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more
14// information.
Caitlin Sadowski402aa062011-09-09 16:11:56 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "clang/Analysis/Analyses/ThreadSafety.h"
Ted Kremenek439ed162011-10-22 02:14:27 +000019#include "clang/Analysis/Analyses/PostOrderCFGView.h"
Caitlin Sadowskid5b16052011-09-09 23:00:59 +000020#include "clang/Analysis/AnalysisContext.h"
21#include "clang/Analysis/CFG.h"
22#include "clang/Analysis/CFGStmtMap.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000023#include "clang/AST/DeclCXX.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/StmtCXX.h"
26#include "clang/AST/StmtVisitor.h"
Caitlin Sadowskid5b16052011-09-09 23:00:59 +000027#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/SourceLocation.h"
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +000029#include "clang/Basic/OperatorKinds.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000030#include "llvm/ADT/BitVector.h"
31#include "llvm/ADT/FoldingSet.h"
32#include "llvm/ADT/ImmutableMap.h"
33#include "llvm/ADT/PostOrderIterator.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/StringRef.h"
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +000036#include "llvm/Support/raw_ostream.h"
Caitlin Sadowski402aa062011-09-09 16:11:56 +000037#include <algorithm>
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +000038#include <utility>
Caitlin Sadowski402aa062011-09-09 16:11:56 +000039#include <vector>
40
41using namespace clang;
42using namespace thread_safety;
43
Caitlin Sadowski19903462011-09-14 20:05:09 +000044// Key method definition
45ThreadSafetyHandler::~ThreadSafetyHandler() {}
46
Caitlin Sadowski402aa062011-09-09 16:11:56 +000047namespace {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +000048
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000049/// SExpr implements a simple expression language that is used to store,
50/// compare, and pretty-print C++ expressions. Unlike a clang Expr, a SExpr
51/// does not capture surface syntax, and it does not distinguish between
52/// C++ concepts, like pointers and references, that have no real semantic
53/// differences. This simplicity allows SExprs to be meaningfully compared,
54/// e.g.
55/// (x) = x
56/// (*this).foo = this->foo
57/// *&a = a
Caitlin Sadowski402aa062011-09-09 16:11:56 +000058///
59/// Thread-safety analysis works by comparing lock expressions. Within the
60/// body of a function, an expression such as "x->foo->bar.mu" will resolve to
61/// a particular mutex object at run-time. Subsequent occurrences of the same
62/// expression (where "same" means syntactic equality) will refer to the same
63/// run-time object if three conditions hold:
64/// (1) Local variables in the expression, such as "x" have not changed.
65/// (2) Values on the heap that affect the expression have not changed.
66/// (3) The expression involves only pure function calls.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +000067///
Caitlin Sadowski402aa062011-09-09 16:11:56 +000068/// The current implementation assumes, but does not verify, that multiple uses
69/// of the same lock expression satisfies these criteria.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000070class SExpr {
71private:
72 enum ExprOp {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +000073 EOP_Nop, ///< No-op
74 EOP_Wildcard, ///< Matches anything.
75 EOP_Universal, ///< Universal lock.
76 EOP_This, ///< This keyword.
77 EOP_NVar, ///< Named variable.
78 EOP_LVar, ///< Local variable.
79 EOP_Dot, ///< Field access
80 EOP_Call, ///< Function call
81 EOP_MCall, ///< Method call
82 EOP_Index, ///< Array index
83 EOP_Unary, ///< Unary operation
84 EOP_Binary, ///< Binary operation
85 EOP_Unknown ///< Catchall for everything else
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000086 };
87
88
89 class SExprNode {
90 private:
Ted Kremenekad0fe032012-08-22 23:50:41 +000091 unsigned char Op; ///< Opcode of the root node
92 unsigned char Flags; ///< Additional opcode-specific data
93 unsigned short Sz; ///< Number of child nodes
94 const void* Data; ///< Additional opcode-specific data
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +000095
96 public:
97 SExprNode(ExprOp O, unsigned F, const void* D)
98 : Op(static_cast<unsigned char>(O)),
99 Flags(static_cast<unsigned char>(F)), Sz(1), Data(D)
100 { }
101
102 unsigned size() const { return Sz; }
103 void setSize(unsigned S) { Sz = S; }
104
105 ExprOp kind() const { return static_cast<ExprOp>(Op); }
106
107 const NamedDecl* getNamedDecl() const {
108 assert(Op == EOP_NVar || Op == EOP_LVar || Op == EOP_Dot);
109 return reinterpret_cast<const NamedDecl*>(Data);
110 }
111
112 const NamedDecl* getFunctionDecl() const {
113 assert(Op == EOP_Call || Op == EOP_MCall);
114 return reinterpret_cast<const NamedDecl*>(Data);
115 }
116
117 bool isArrow() const { return Op == EOP_Dot && Flags == 1; }
118 void setArrow(bool A) { Flags = A ? 1 : 0; }
119
120 unsigned arity() const {
121 switch (Op) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000122 case EOP_Nop: return 0;
123 case EOP_Wildcard: return 0;
124 case EOP_Universal: return 0;
125 case EOP_NVar: return 0;
126 case EOP_LVar: return 0;
127 case EOP_This: return 0;
128 case EOP_Dot: return 1;
129 case EOP_Call: return Flags+1; // First arg is function.
130 case EOP_MCall: return Flags+1; // First arg is implicit obj.
131 case EOP_Index: return 2;
132 case EOP_Unary: return 1;
133 case EOP_Binary: return 2;
134 case EOP_Unknown: return Flags;
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000135 }
136 return 0;
137 }
138
139 bool operator==(const SExprNode& Other) const {
140 // Ignore flags and size -- they don't matter.
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000141 return (Op == Other.Op &&
142 Data == Other.Data);
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000143 }
144
145 bool operator!=(const SExprNode& Other) const {
146 return !(*this == Other);
147 }
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000148
149 bool matches(const SExprNode& Other) const {
150 return (*this == Other) ||
151 (Op == EOP_Wildcard) ||
152 (Other.Op == EOP_Wildcard);
153 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000154 };
155
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000156
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000157 /// \brief Encapsulates the lexical context of a function call. The lexical
158 /// context includes the arguments to the call, including the implicit object
159 /// argument. When an attribute containing a mutex expression is attached to
160 /// a method, the expression may refer to formal parameters of the method.
161 /// Actual arguments must be substituted for formal parameters to derive
162 /// the appropriate mutex expression in the lexical context where the function
163 /// is called. PrevCtx holds the context in which the arguments themselves
164 /// should be evaluated; multiple calling contexts can be chained together
165 /// by the lock_returned attribute.
166 struct CallingContext {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000167 const NamedDecl* AttrDecl; // The decl to which the attribute is attached.
168 Expr* SelfArg; // Implicit object argument -- e.g. 'this'
169 bool SelfArrow; // is Self referred to with -> or .?
170 unsigned NumArgs; // Number of funArgs
171 Expr** FunArgs; // Function arguments
172 CallingContext* PrevCtx; // The previous context; or 0 if none.
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000173
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000174 CallingContext(const NamedDecl *D = 0, Expr *S = 0,
175 unsigned N = 0, Expr **A = 0, CallingContext *P = 0)
176 : AttrDecl(D), SelfArg(S), SelfArrow(false),
177 NumArgs(N), FunArgs(A), PrevCtx(P)
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000178 { }
179 };
180
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000181 typedef SmallVector<SExprNode, 4> NodeVector;
182
183private:
184 // A SExpr is a list of SExprNodes in prefix order. The Size field allows
185 // the list to be traversed as a tree.
186 NodeVector NodeVec;
187
188private:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000189 unsigned makeNop() {
190 NodeVec.push_back(SExprNode(EOP_Nop, 0, 0));
191 return NodeVec.size()-1;
192 }
193
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000194 unsigned makeWildcard() {
195 NodeVec.push_back(SExprNode(EOP_Wildcard, 0, 0));
196 return NodeVec.size()-1;
197 }
198
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000199 unsigned makeUniversal() {
200 NodeVec.push_back(SExprNode(EOP_Universal, 0, 0));
201 return NodeVec.size()-1;
202 }
203
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000204 unsigned makeNamedVar(const NamedDecl *D) {
205 NodeVec.push_back(SExprNode(EOP_NVar, 0, D));
206 return NodeVec.size()-1;
207 }
208
209 unsigned makeLocalVar(const NamedDecl *D) {
210 NodeVec.push_back(SExprNode(EOP_LVar, 0, D));
211 return NodeVec.size()-1;
212 }
213
214 unsigned makeThis() {
215 NodeVec.push_back(SExprNode(EOP_This, 0, 0));
216 return NodeVec.size()-1;
217 }
218
219 unsigned makeDot(const NamedDecl *D, bool Arrow) {
220 NodeVec.push_back(SExprNode(EOP_Dot, Arrow ? 1 : 0, D));
221 return NodeVec.size()-1;
222 }
223
224 unsigned makeCall(unsigned NumArgs, const NamedDecl *D) {
225 NodeVec.push_back(SExprNode(EOP_Call, NumArgs, D));
226 return NodeVec.size()-1;
227 }
228
229 unsigned makeMCall(unsigned NumArgs, const NamedDecl *D) {
230 NodeVec.push_back(SExprNode(EOP_MCall, NumArgs, D));
231 return NodeVec.size()-1;
232 }
233
234 unsigned makeIndex() {
235 NodeVec.push_back(SExprNode(EOP_Index, 0, 0));
236 return NodeVec.size()-1;
237 }
238
239 unsigned makeUnary() {
240 NodeVec.push_back(SExprNode(EOP_Unary, 0, 0));
241 return NodeVec.size()-1;
242 }
243
244 unsigned makeBinary() {
245 NodeVec.push_back(SExprNode(EOP_Binary, 0, 0));
246 return NodeVec.size()-1;
247 }
248
249 unsigned makeUnknown(unsigned Arity) {
250 NodeVec.push_back(SExprNode(EOP_Unknown, Arity, 0));
251 return NodeVec.size()-1;
252 }
253
254 /// Build an SExpr from the given C++ expression.
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000255 /// Recursive function that terminates on DeclRefExpr.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000256 /// Note: this function merely creates a SExpr; it does not check to
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000257 /// ensure that the original expression is a valid mutex expression.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000258 ///
259 /// NDeref returns the number of Derefence and AddressOf operations
260 /// preceeding the Expr; this is used to decide whether to pretty-print
261 /// SExprs with . or ->.
262 unsigned buildSExpr(Expr *Exp, CallingContext* CallCtx, int* NDeref = 0) {
263 if (!Exp)
264 return 0;
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000265
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000266 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) {
267 NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl());
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000268 ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND);
269 if (PV) {
270 FunctionDecl *FD =
271 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
272 unsigned i = PV->getFunctionScopeIndex();
273
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000274 if (CallCtx && CallCtx->FunArgs &&
275 FD == CallCtx->AttrDecl->getCanonicalDecl()) {
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000276 // Substitute call arguments for references to function parameters
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000277 assert(i < CallCtx->NumArgs);
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000278 return buildSExpr(CallCtx->FunArgs[i], CallCtx->PrevCtx, NDeref);
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000279 }
280 // Map the param back to the param of the original function declaration.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000281 makeNamedVar(FD->getParamDecl(i));
282 return 1;
DeLesley Hutchinse03b2b32012-01-20 23:24:41 +0000283 }
284 // Not a function parameter -- just store the reference.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000285 makeNamedVar(ND);
286 return 1;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000287 } else if (isa<CXXThisExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000288 // Substitute parent for 'this'
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000289 if (CallCtx && CallCtx->SelfArg) {
290 if (!CallCtx->SelfArrow && NDeref)
291 // 'this' is a pointer, but self is not, so need to take address.
292 --(*NDeref);
293 return buildSExpr(CallCtx->SelfArg, CallCtx->PrevCtx, NDeref);
294 }
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000295 else {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000296 makeThis();
297 return 1;
DeLesley Hutchins4bda3ec2012-02-16 17:03:24 +0000298 }
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000299 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
300 NamedDecl *ND = ME->getMemberDecl();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000301 int ImplicitDeref = ME->isArrow() ? 1 : 0;
302 unsigned Root = makeDot(ND, false);
303 unsigned Sz = buildSExpr(ME->getBase(), CallCtx, &ImplicitDeref);
304 NodeVec[Root].setArrow(ImplicitDeref > 0);
305 NodeVec[Root].setSize(Sz + 1);
306 return Sz + 1;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000307 } else if (CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000308 // When calling a function with a lock_returned attribute, replace
309 // the function call with the expression in lock_returned.
DeLesley Hutchins54081532012-08-31 22:09:53 +0000310 CXXMethodDecl* MD =
311 cast<CXXMethodDecl>(CMCE->getMethodDecl()->getMostRecentDecl());
312 if (LockReturnedAttr* At = MD->getAttr<LockReturnedAttr>()) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000313 CallingContext LRCallCtx(CMCE->getMethodDecl());
314 LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000315 LRCallCtx.SelfArrow =
316 dyn_cast<MemberExpr>(CMCE->getCallee())->isArrow();
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000317 LRCallCtx.NumArgs = CMCE->getNumArgs();
318 LRCallCtx.FunArgs = CMCE->getArgs();
319 LRCallCtx.PrevCtx = CallCtx;
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000320 return buildSExpr(At->getArg(), &LRCallCtx);
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000321 }
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000322 // Hack to treat smart pointers and iterators as pointers;
323 // ignore any method named get().
324 if (CMCE->getMethodDecl()->getNameAsString() == "get" &&
325 CMCE->getNumArgs() == 0) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000326 if (NDeref && dyn_cast<MemberExpr>(CMCE->getCallee())->isArrow())
327 ++(*NDeref);
328 return buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx, NDeref);
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000329 }
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000330 unsigned NumCallArgs = CMCE->getNumArgs();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000331 unsigned Root =
332 makeMCall(NumCallArgs, CMCE->getMethodDecl()->getCanonicalDecl());
333 unsigned Sz = buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000334 Expr** CallArgs = CMCE->getArgs();
335 for (unsigned i = 0; i < NumCallArgs; ++i) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000336 Sz += buildSExpr(CallArgs[i], CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000337 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000338 NodeVec[Root].setSize(Sz + 1);
339 return Sz + 1;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000340 } else if (CallExpr *CE = dyn_cast<CallExpr>(Exp)) {
DeLesley Hutchins54081532012-08-31 22:09:53 +0000341 FunctionDecl* FD =
342 cast<FunctionDecl>(CE->getDirectCallee()->getMostRecentDecl());
343 if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000344 CallingContext LRCallCtx(CE->getDirectCallee());
345 LRCallCtx.NumArgs = CE->getNumArgs();
346 LRCallCtx.FunArgs = CE->getArgs();
347 LRCallCtx.PrevCtx = CallCtx;
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000348 return buildSExpr(At->getArg(), &LRCallCtx);
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000349 }
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000350 // Treat smart pointers and iterators as pointers;
351 // ignore the * and -> operators.
352 if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(CE)) {
353 OverloadedOperatorKind k = OE->getOperator();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000354 if (k == OO_Star) {
355 if (NDeref) ++(*NDeref);
356 return buildSExpr(OE->getArg(0), CallCtx, NDeref);
357 }
358 else if (k == OO_Arrow) {
359 return buildSExpr(OE->getArg(0), CallCtx, NDeref);
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000360 }
361 }
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000362 unsigned NumCallArgs = CE->getNumArgs();
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000363 unsigned Root = makeCall(NumCallArgs, 0);
364 unsigned Sz = buildSExpr(CE->getCallee(), CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000365 Expr** CallArgs = CE->getArgs();
366 for (unsigned i = 0; i < NumCallArgs; ++i) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000367 Sz += buildSExpr(CallArgs[i], CallCtx);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000368 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000369 NodeVec[Root].setSize(Sz+1);
370 return Sz+1;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000371 } else if (BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000372 unsigned Root = makeBinary();
373 unsigned Sz = buildSExpr(BOE->getLHS(), CallCtx);
374 Sz += buildSExpr(BOE->getRHS(), CallCtx);
375 NodeVec[Root].setSize(Sz);
376 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000377 } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000378 // Ignore & and * operators -- they're no-ops.
379 // However, we try to figure out whether the expression is a pointer,
380 // so we can use . and -> appropriately in error messages.
381 if (UOE->getOpcode() == UO_Deref) {
382 if (NDeref) ++(*NDeref);
383 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
384 }
385 if (UOE->getOpcode() == UO_AddrOf) {
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000386 if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UOE->getSubExpr())) {
387 if (DRE->getDecl()->isCXXInstanceMember()) {
388 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
389 // We interpret this syntax specially, as a wildcard.
390 unsigned Root = makeDot(DRE->getDecl(), false);
391 makeWildcard();
392 NodeVec[Root].setSize(2);
393 return 2;
394 }
395 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000396 if (NDeref) --(*NDeref);
397 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
398 }
399 unsigned Root = makeUnary();
400 unsigned Sz = buildSExpr(UOE->getSubExpr(), CallCtx);
401 NodeVec[Root].setSize(Sz);
402 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000403 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000404 unsigned Root = makeIndex();
405 unsigned Sz = buildSExpr(ASE->getBase(), CallCtx);
406 Sz += buildSExpr(ASE->getIdx(), CallCtx);
407 NodeVec[Root].setSize(Sz);
408 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000409 } else if (AbstractConditionalOperator *CE =
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000410 dyn_cast<AbstractConditionalOperator>(Exp)) {
411 unsigned Root = makeUnknown(3);
412 unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
413 Sz += buildSExpr(CE->getTrueExpr(), CallCtx);
414 Sz += buildSExpr(CE->getFalseExpr(), CallCtx);
415 NodeVec[Root].setSize(Sz);
416 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000417 } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000418 unsigned Root = makeUnknown(3);
419 unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
420 Sz += buildSExpr(CE->getLHS(), CallCtx);
421 Sz += buildSExpr(CE->getRHS(), CallCtx);
422 NodeVec[Root].setSize(Sz);
423 return Sz;
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000424 } else if (CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000425 return buildSExpr(CE->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000426 } else if (ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000427 return buildSExpr(PE->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins9d6e7f32012-07-03 18:25:56 +0000428 } else if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000429 return buildSExpr(EWC->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins96fac6a2012-07-03 19:47:18 +0000430 } else if (CXXBindTemporaryExpr *E = dyn_cast<CXXBindTemporaryExpr>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000431 return buildSExpr(E->getSubExpr(), CallCtx, NDeref);
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000432 } else if (isa<CharacterLiteral>(Exp) ||
DeLesley Hutchins9d6e7f32012-07-03 18:25:56 +0000433 isa<CXXNullPtrLiteralExpr>(Exp) ||
434 isa<GNUNullExpr>(Exp) ||
435 isa<CXXBoolLiteralExpr>(Exp) ||
436 isa<FloatingLiteral>(Exp) ||
437 isa<ImaginaryLiteral>(Exp) ||
438 isa<IntegerLiteral>(Exp) ||
439 isa<StringLiteral>(Exp) ||
440 isa<ObjCStringLiteral>(Exp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000441 makeNop();
442 return 1; // FIXME: Ignore literals for now
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000443 } else {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000444 makeNop();
445 return 1; // Ignore. FIXME: mark as invalid expression?
DeLesley Hutchins0d95dfc2012-03-02 23:36:05 +0000446 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000447 }
448
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000449 /// \brief Construct a SExpr from an expression.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000450 /// \param MutexExp The original mutex expression within an attribute
451 /// \param DeclExp An expression involving the Decl on which the attribute
452 /// occurs.
453 /// \param D The declaration to which the lock/unlock attribute is attached.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000454 void buildSExprFromExpr(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000455 CallingContext CallCtx(D);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000456
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000457
458 if (MutexExp) {
459 if (StringLiteral* SLit = dyn_cast<StringLiteral>(MutexExp)) {
460 if (SLit->getString() == StringRef("*"))
461 // The "*" expr is a universal lock, which essentially turns off
462 // checks until it is removed from the lockset.
463 makeUniversal();
464 else
465 // Ignore other string literals for now.
466 makeNop();
467 return;
468 }
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000469 }
470
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000471 // If we are processing a raw attribute expression, with no substitutions.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000472 if (DeclExp == 0) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000473 buildSExpr(MutexExp, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000474 return;
475 }
476
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000477 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000478 // for formal parameters when we call buildMutexID later.
DeLesley Hutchins81216392011-10-17 21:38:02 +0000479 if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000480 CallCtx.SelfArg = ME->getBase();
481 CallCtx.SelfArrow = ME->isArrow();
DeLesley Hutchins81216392011-10-17 21:38:02 +0000482 } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000483 CallCtx.SelfArg = CE->getImplicitObjectArgument();
484 CallCtx.SelfArrow = dyn_cast<MemberExpr>(CE->getCallee())->isArrow();
485 CallCtx.NumArgs = CE->getNumArgs();
486 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchinsdf497822011-12-29 00:56:48 +0000487 } else if (CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000488 CallCtx.NumArgs = CE->getNumArgs();
489 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +0000490 } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000491 CallCtx.SelfArg = 0; // FIXME -- get the parent from DeclStmt
492 CallCtx.NumArgs = CE->getNumArgs();
493 CallCtx.FunArgs = CE->getArgs();
DeLesley Hutchins6db51f72011-10-21 20:51:27 +0000494 } else if (D && isa<CXXDestructorDecl>(D)) {
495 // There's no such thing as a "destructor call" in the AST.
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000496 CallCtx.SelfArg = DeclExp;
DeLesley Hutchins81216392011-10-17 21:38:02 +0000497 }
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000498
499 // If the attribute has no arguments, then assume the argument is "this".
500 if (MutexExp == 0) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000501 buildSExpr(CallCtx.SelfArg, 0);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000502 return;
503 }
DeLesley Hutchins81216392011-10-17 21:38:02 +0000504
DeLesley Hutchinsf63797c2012-06-25 18:33:18 +0000505 // For most attributes.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000506 buildSExpr(MutexExp, &CallCtx);
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000507 }
508
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000509 /// \brief Get index of next sibling of node i.
510 unsigned getNextSibling(unsigned i) const {
511 return i + NodeVec[i].size();
512 }
513
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000514public:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000515 explicit SExpr(clang::Decl::EmptyShell e) { NodeVec.clear(); }
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000516
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000517 /// \param MutexExp The original mutex expression within an attribute
518 /// \param DeclExp An expression involving the Decl on which the attribute
519 /// occurs.
520 /// \param D The declaration to which the lock/unlock attribute is attached.
521 /// Caller must check isValid() after construction.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000522 SExpr(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D) {
523 buildSExprFromExpr(MutexExp, DeclExp, D);
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000524 }
525
DeLesley Hutchins9f80a972011-10-17 21:33:35 +0000526 /// Return true if this is a valid decl sequence.
527 /// Caller must call this by hand after construction to handle errors.
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000528 bool isValid() const {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000529 return !NodeVec.empty();
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000530 }
531
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000532 bool shouldIgnore() const {
533 // Nop is a mutex that we have decided to deliberately ignore.
534 assert(NodeVec.size() > 0 && "Invalid Mutex");
535 return NodeVec[0].kind() == EOP_Nop;
536 }
537
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000538 bool isUniversal() const {
539 assert(NodeVec.size() > 0 && "Invalid Mutex");
540 return NodeVec[0].kind() == EOP_Universal;
541 }
542
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +0000543 /// Issue a warning about an invalid lock expression
544 static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp,
545 Expr *DeclExp, const NamedDecl* D) {
546 SourceLocation Loc;
547 if (DeclExp)
548 Loc = DeclExp->getExprLoc();
549
550 // FIXME: add a note about the attribute location in MutexExp or D
551 if (Loc.isValid())
552 Handler.handleInvalidLockExp(Loc);
553 }
554
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000555 bool operator==(const SExpr &other) const {
556 return NodeVec == other.NodeVec;
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000557 }
558
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000559 bool operator!=(const SExpr &other) const {
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000560 return !(*this == other);
561 }
562
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000563 bool matches(const SExpr &Other, unsigned i = 0, unsigned j = 0) const {
564 if (NodeVec[i].matches(Other.NodeVec[j])) {
565 unsigned n = NodeVec[i].arity();
566 bool Result = true;
567 unsigned ci = i+1; // first child of i
568 unsigned cj = j+1; // first child of j
569 for (unsigned k = 0; k < n;
570 ++k, ci=getNextSibling(ci), cj = Other.getNextSibling(cj)) {
571 Result = Result && matches(Other, ci, cj);
572 }
573 return Result;
574 }
575 return false;
576 }
577
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000578 /// \brief Pretty print a lock expression for use in error messages.
579 std::string toString(unsigned i = 0) const {
Caitlin Sadowski194418f2011-09-14 20:00:24 +0000580 assert(isValid());
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000581 if (i >= NodeVec.size())
582 return "";
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000583
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000584 const SExprNode* N = &NodeVec[i];
585 switch (N->kind()) {
586 case EOP_Nop:
587 return "_";
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000588 case EOP_Wildcard:
589 return "(?)";
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000590 case EOP_Universal:
591 return "*";
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000592 case EOP_This:
593 return "this";
594 case EOP_NVar:
595 case EOP_LVar: {
596 return N->getNamedDecl()->getNameAsString();
597 }
598 case EOP_Dot: {
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000599 if (NodeVec[i+1].kind() == EOP_Wildcard) {
600 std::string S = "&";
601 S += N->getNamedDecl()->getQualifiedNameAsString();
602 return S;
603 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000604 std::string FieldName = N->getNamedDecl()->getNameAsString();
605 if (NodeVec[i+1].kind() == EOP_This)
606 return FieldName;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000607
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000608 std::string S = toString(i+1);
609 if (N->isArrow())
610 return S + "->" + FieldName;
611 else
612 return S + "." + FieldName;
613 }
614 case EOP_Call: {
615 std::string S = toString(i+1) + "(";
616 unsigned NumArgs = N->arity()-1;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000617 unsigned ci = getNextSibling(i+1);
618 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000619 S += toString(ci);
620 if (k+1 < NumArgs) S += ",";
621 }
622 S += ")";
623 return S;
624 }
625 case EOP_MCall: {
626 std::string S = "";
627 if (NodeVec[i+1].kind() != EOP_This)
628 S = toString(i+1) + ".";
629 if (const NamedDecl *D = N->getFunctionDecl())
630 S += D->getNameAsString() + "(";
631 else
632 S += "#(";
633 unsigned NumArgs = N->arity()-1;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000634 unsigned ci = getNextSibling(i+1);
635 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000636 S += toString(ci);
637 if (k+1 < NumArgs) S += ",";
638 }
639 S += ")";
640 return S;
641 }
642 case EOP_Index: {
643 std::string S1 = toString(i+1);
644 std::string S2 = toString(i+1 + NodeVec[i+1].size());
645 return S1 + "[" + S2 + "]";
646 }
647 case EOP_Unary: {
648 std::string S = toString(i+1);
649 return "#" + S;
650 }
651 case EOP_Binary: {
652 std::string S1 = toString(i+1);
653 std::string S2 = toString(i+1 + NodeVec[i+1].size());
654 return "(" + S1 + "#" + S2 + ")";
655 }
656 case EOP_Unknown: {
657 unsigned NumChildren = N->arity();
658 if (NumChildren == 0)
659 return "(...)";
660 std::string S = "(";
661 unsigned ci = i+1;
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000662 for (unsigned j = 0; j < NumChildren; ++j, ci = getNextSibling(ci)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000663 S += toString(ci);
664 if (j+1 < NumChildren) S += "#";
665 }
666 S += ")";
667 return S;
668 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000669 }
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000670 return "";
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000671 }
672};
673
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000674
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000675
676/// \brief A short list of SExprs
677class MutexIDList : public SmallVector<SExpr, 3> {
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000678public:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000679 /// \brief Return true if the list contains the specified SExpr
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000680 /// Performs a linear search, because these lists are almost always very small.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000681 bool contains(const SExpr& M) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000682 for (iterator I=begin(),E=end(); I != E; ++I)
683 if ((*I) == M) return true;
684 return false;
685 }
686
687 /// \brief Push M onto list, bud discard duplicates
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000688 void push_back_nodup(const SExpr& M) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +0000689 if (!contains(M)) push_back(M);
690 }
691};
692
693
694
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000695/// \brief This is a helper class that stores info about the most recent
696/// accquire of a Lock.
697///
698/// The main body of the analysis maps MutexIDs to LockDatas.
699struct LockData {
700 SourceLocation AcquireLoc;
701
702 /// \brief LKind stores whether a lock is held shared or exclusively.
703 /// Note that this analysis does not currently support either re-entrant
704 /// locking or lock "upgrading" and "downgrading" between exclusive and
705 /// shared.
706 ///
707 /// FIXME: add support for re-entrant locking and lock up/downgrading
708 LockKind LKind;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000709 bool Managed; // for ScopedLockable objects
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000710 SExpr UnderlyingMutex; // for ScopedLockable objects
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000711
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000712 LockData(SourceLocation AcquireLoc, LockKind LKind, bool M = false)
713 : AcquireLoc(AcquireLoc), LKind(LKind), Managed(M),
714 UnderlyingMutex(Decl::EmptyShell())
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +0000715 {}
716
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000717 LockData(SourceLocation AcquireLoc, LockKind LKind, const SExpr &Mu)
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +0000718 : AcquireLoc(AcquireLoc), LKind(LKind), Managed(false),
719 UnderlyingMutex(Mu)
720 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000721
722 bool operator==(const LockData &other) const {
723 return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
724 }
725
726 bool operator!=(const LockData &other) const {
727 return !(*this == other);
728 }
729
730 void Profile(llvm::FoldingSetNodeID &ID) const {
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000731 ID.AddInteger(AcquireLoc.getRawEncoding());
732 ID.AddInteger(LKind);
733 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000734
735 bool isAtLeast(LockKind LK) {
736 return (LK == LK_Shared) || (LKind == LK_Exclusive);
737 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000738};
739
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +0000740
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000741/// \brief A FactEntry stores a single fact that is known at a particular point
742/// in the program execution. Currently, this is information regarding a lock
743/// that is held at that point.
744struct FactEntry {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000745 SExpr MutID;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000746 LockData LDat;
747
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000748 FactEntry(const SExpr& M, const LockData& L)
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000749 : MutID(M), LDat(L)
750 { }
751};
752
753
754typedef unsigned short FactID;
755
756/// \brief FactManager manages the memory for all facts that are created during
757/// the analysis of a single routine.
758class FactManager {
759private:
760 std::vector<FactEntry> Facts;
761
762public:
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000763 FactID newLock(const SExpr& M, const LockData& L) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000764 Facts.push_back(FactEntry(M,L));
765 return static_cast<unsigned short>(Facts.size() - 1);
766 }
767
768 const FactEntry& operator[](FactID F) const { return Facts[F]; }
769 FactEntry& operator[](FactID F) { return Facts[F]; }
770};
771
772
773/// \brief A FactSet is the set of facts that are known to be true at a
774/// particular program point. FactSets must be small, because they are
775/// frequently copied, and are thus implemented as a set of indices into a
776/// table maintained by a FactManager. A typical FactSet only holds 1 or 2
777/// locks, so we can get away with doing a linear search for lookup. Note
778/// that a hashtable or map is inappropriate in this case, because lookups
779/// may involve partial pattern matches, rather than exact matches.
780class FactSet {
781private:
782 typedef SmallVector<FactID, 4> FactVec;
783
784 FactVec FactIDs;
785
786public:
787 typedef FactVec::iterator iterator;
788 typedef FactVec::const_iterator const_iterator;
789
790 iterator begin() { return FactIDs.begin(); }
791 const_iterator begin() const { return FactIDs.begin(); }
792
793 iterator end() { return FactIDs.end(); }
794 const_iterator end() const { return FactIDs.end(); }
795
796 bool isEmpty() const { return FactIDs.size() == 0; }
797
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000798 FactID addLock(FactManager& FM, const SExpr& M, const LockData& L) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000799 FactID F = FM.newLock(M, L);
800 FactIDs.push_back(F);
801 return F;
802 }
803
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000804 bool removeLock(FactManager& FM, const SExpr& M) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000805 unsigned n = FactIDs.size();
806 if (n == 0)
807 return false;
808
809 for (unsigned i = 0; i < n-1; ++i) {
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000810 if (FM[FactIDs[i]].MutID.matches(M)) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000811 FactIDs[i] = FactIDs[n-1];
812 FactIDs.pop_back();
813 return true;
814 }
815 }
DeLesley Hutchinsee2f0322012-08-10 20:29:46 +0000816 if (FM[FactIDs[n-1]].MutID.matches(M)) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000817 FactIDs.pop_back();
818 return true;
819 }
820 return false;
821 }
822
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000823 LockData* findLock(FactManager& FM, const SExpr& M) const {
Chad Rosier2de47702012-09-07 18:44:15 +0000824 for (const_iterator I = begin(), E = end(); I != E; ++I) {
825 const SExpr& Exp = FM[*I].MutID;
826 if (Exp.matches(M))
827 return &FM[*I].LDat;
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000828 }
829 return 0;
830 }
831
832 LockData* findLockUniv(FactManager& FM, const SExpr& M) const {
Chad Rosier2de47702012-09-07 18:44:15 +0000833 for (const_iterator I = begin(), E = end(); I != E; ++I) {
834 const SExpr& Exp = FM[*I].MutID;
835 if (Exp.matches(M) || Exp.isUniversal())
836 return &FM[*I].LDat;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000837 }
838 return 0;
839 }
840};
841
842
843
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000844/// A Lockset maps each SExpr (defined above) to information about how it has
Caitlin Sadowski402aa062011-09-09 16:11:56 +0000845/// been locked.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +0000846typedef llvm::ImmutableMap<SExpr, LockData> Lockset;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000847typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000848
849class LocalVariableMap;
850
Richard Smith2e515622012-02-03 04:45:26 +0000851/// A side (entry or exit) of a CFG node.
852enum CFGBlockSide { CBS_Entry, CBS_Exit };
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000853
854/// CFGBlockInfo is a struct which contains all the information that is
855/// maintained for each block in the CFG. See LocalVariableMap for more
856/// information about the contexts.
857struct CFGBlockInfo {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000858 FactSet EntrySet; // Lockset held at entry to block
859 FactSet ExitSet; // Lockset held at exit from block
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000860 LocalVarContext EntryContext; // Context held at entry to block
861 LocalVarContext ExitContext; // Context held at exit from block
Richard Smith2e515622012-02-03 04:45:26 +0000862 SourceLocation EntryLoc; // Location of first statement in block
863 SourceLocation ExitLoc; // Location of last statement in block.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000864 unsigned EntryIndex; // Used to replay contexts later
865
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000866 const FactSet &getSet(CFGBlockSide Side) const {
Richard Smith2e515622012-02-03 04:45:26 +0000867 return Side == CBS_Entry ? EntrySet : ExitSet;
868 }
869 SourceLocation getLocation(CFGBlockSide Side) const {
870 return Side == CBS_Entry ? EntryLoc : ExitLoc;
871 }
872
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000873private:
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000874 CFGBlockInfo(LocalVarContext EmptyCtx)
875 : EntryContext(EmptyCtx), ExitContext(EmptyCtx)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000876 { }
877
878public:
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +0000879 static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000880};
881
882
883
884// A LocalVariableMap maintains a map from local variables to their currently
885// valid definitions. It provides SSA-like functionality when traversing the
886// CFG. Like SSA, each definition or assignment to a variable is assigned a
887// unique name (an integer), which acts as the SSA name for that definition.
888// The total set of names is shared among all CFG basic blocks.
889// Unlike SSA, we do not rewrite expressions to replace local variables declrefs
890// with their SSA-names. Instead, we compute a Context for each point in the
891// code, which maps local variables to the appropriate SSA-name. This map
892// changes with each assignment.
893//
894// The map is computed in a single pass over the CFG. Subsequent analyses can
895// then query the map to find the appropriate Context for a statement, and use
896// that Context to look up the definitions of variables.
897class LocalVariableMap {
898public:
899 typedef LocalVarContext Context;
900
901 /// A VarDefinition consists of an expression, representing the value of the
902 /// variable, along with the context in which that expression should be
903 /// interpreted. A reference VarDefinition does not itself contain this
904 /// information, but instead contains a pointer to a previous VarDefinition.
905 struct VarDefinition {
906 public:
907 friend class LocalVariableMap;
908
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000909 const NamedDecl *Dec; // The original declaration for this variable.
910 const Expr *Exp; // The expression for this variable, OR
911 unsigned Ref; // Reference to another VarDefinition
912 Context Ctx; // The map with which Exp should be interpreted.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000913
914 bool isReference() { return !Exp; }
915
916 private:
917 // Create ordinary variable definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000918 VarDefinition(const NamedDecl *D, const Expr *E, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000919 : Dec(D), Exp(E), Ref(0), Ctx(C)
920 { }
921
922 // Create reference to previous definition
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000923 VarDefinition(const NamedDecl *D, unsigned R, Context C)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000924 : Dec(D), Exp(0), Ref(R), Ctx(C)
925 { }
926 };
927
928private:
929 Context::Factory ContextFactory;
930 std::vector<VarDefinition> VarDefinitions;
931 std::vector<unsigned> CtxIndices;
932 std::vector<std::pair<Stmt*, Context> > SavedContexts;
933
934public:
935 LocalVariableMap() {
936 // index 0 is a placeholder for undefined variables (aka phi-nodes).
937 VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext()));
938 }
939
940 /// Look up a definition, within the given context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000941 const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000942 const unsigned *i = Ctx.lookup(D);
943 if (!i)
944 return 0;
945 assert(*i < VarDefinitions.size());
946 return &VarDefinitions[*i];
947 }
948
949 /// Look up the definition for D within the given context. Returns
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000950 /// NULL if the expression is not statically known. If successful, also
951 /// modifies Ctx to hold the context of the return Expr.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000952 const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000953 const unsigned *P = Ctx.lookup(D);
954 if (!P)
955 return 0;
956
957 unsigned i = *P;
958 while (i > 0) {
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000959 if (VarDefinitions[i].Exp) {
960 Ctx = VarDefinitions[i].Ctx;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000961 return VarDefinitions[i].Exp;
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +0000962 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000963 i = VarDefinitions[i].Ref;
964 }
965 return 0;
966 }
967
968 Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
969
970 /// Return the next context after processing S. This function is used by
971 /// clients of the class to get the appropriate context when traversing the
972 /// CFG. It must be called for every assignment or DeclStmt.
973 Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
974 if (SavedContexts[CtxIndex+1].first == S) {
975 CtxIndex++;
976 Context Result = SavedContexts[CtxIndex].second;
977 return Result;
978 }
979 return C;
980 }
981
982 void dumpVarDefinitionName(unsigned i) {
983 if (i == 0) {
984 llvm::errs() << "Undefined";
985 return;
986 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000987 const NamedDecl *Dec = VarDefinitions[i].Dec;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000988 if (!Dec) {
989 llvm::errs() << "<<NULL>>";
990 return;
991 }
992 Dec->printName(llvm::errs());
Roman Divacky31ba6132012-09-06 15:59:27 +0000993 llvm::errs() << "." << i << " " << ((const void*) Dec);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +0000994 }
995
996 /// Dumps an ASCII representation of the variable map to llvm::errs()
997 void dump() {
998 for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +0000999 const Expr *Exp = VarDefinitions[i].Exp;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001000 unsigned Ref = VarDefinitions[i].Ref;
1001
1002 dumpVarDefinitionName(i);
1003 llvm::errs() << " = ";
1004 if (Exp) Exp->dump();
1005 else {
1006 dumpVarDefinitionName(Ref);
1007 llvm::errs() << "\n";
1008 }
1009 }
1010 }
1011
1012 /// Dumps an ASCII representation of a Context to llvm::errs()
1013 void dumpContext(Context C) {
1014 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001015 const NamedDecl *D = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001016 D->printName(llvm::errs());
1017 const unsigned *i = C.lookup(D);
1018 llvm::errs() << " -> ";
1019 dumpVarDefinitionName(*i);
1020 llvm::errs() << "\n";
1021 }
1022 }
1023
1024 /// Builds the variable map.
1025 void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph,
1026 std::vector<CFGBlockInfo> &BlockInfo);
1027
1028protected:
1029 // Get the current context index
1030 unsigned getContextIndex() { return SavedContexts.size()-1; }
1031
1032 // Save the current context for later replay
1033 void saveContext(Stmt *S, Context C) {
1034 SavedContexts.push_back(std::make_pair(S,C));
1035 }
1036
1037 // Adds a new definition to the given context, and returns a new context.
1038 // This method should be called when declaring a new variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001039 Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001040 assert(!Ctx.contains(D));
1041 unsigned newID = VarDefinitions.size();
1042 Context NewCtx = ContextFactory.add(Ctx, D, newID);
1043 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1044 return NewCtx;
1045 }
1046
1047 // Add a new reference to an existing definition.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001048 Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001049 unsigned newID = VarDefinitions.size();
1050 Context NewCtx = ContextFactory.add(Ctx, D, newID);
1051 VarDefinitions.push_back(VarDefinition(D, i, Ctx));
1052 return NewCtx;
1053 }
1054
1055 // Updates a definition only if that definition is already in the map.
1056 // This method should be called when assigning to an existing variable.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001057 Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001058 if (Ctx.contains(D)) {
1059 unsigned newID = VarDefinitions.size();
1060 Context NewCtx = ContextFactory.remove(Ctx, D);
1061 NewCtx = ContextFactory.add(NewCtx, D, newID);
1062 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1063 return NewCtx;
1064 }
1065 return Ctx;
1066 }
1067
1068 // Removes a definition from the context, but keeps the variable name
1069 // as a valid variable. The index 0 is a placeholder for cleared definitions.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001070 Context clearDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001071 Context NewCtx = Ctx;
1072 if (NewCtx.contains(D)) {
1073 NewCtx = ContextFactory.remove(NewCtx, D);
1074 NewCtx = ContextFactory.add(NewCtx, D, 0);
1075 }
1076 return NewCtx;
1077 }
1078
1079 // Remove a definition entirely frmo the context.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001080 Context removeDefinition(const NamedDecl *D, Context Ctx) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001081 Context NewCtx = Ctx;
1082 if (NewCtx.contains(D)) {
1083 NewCtx = ContextFactory.remove(NewCtx, D);
1084 }
1085 return NewCtx;
1086 }
1087
1088 Context intersectContexts(Context C1, Context C2);
1089 Context createReferenceContext(Context C);
1090 void intersectBackEdge(Context C1, Context C2);
1091
1092 friend class VarMapBuilder;
1093};
1094
1095
1096// This has to be defined after LocalVariableMap.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001097CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) {
1098 return CFGBlockInfo(M.getEmptyContext());
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001099}
1100
1101
1102/// Visitor which builds a LocalVariableMap
1103class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
1104public:
1105 LocalVariableMap* VMap;
1106 LocalVariableMap::Context Ctx;
1107
1108 VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
1109 : VMap(VM), Ctx(C) {}
1110
1111 void VisitDeclStmt(DeclStmt *S);
1112 void VisitBinaryOperator(BinaryOperator *BO);
1113};
1114
1115
1116// Add new local variables to the variable map
1117void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
1118 bool modifiedCtx = false;
1119 DeclGroupRef DGrp = S->getDeclGroup();
1120 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1121 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
1122 Expr *E = VD->getInit();
1123
1124 // Add local variables with trivial type to the variable map
1125 QualType T = VD->getType();
1126 if (T.isTrivialType(VD->getASTContext())) {
1127 Ctx = VMap->addDefinition(VD, E, Ctx);
1128 modifiedCtx = true;
1129 }
1130 }
1131 }
1132 if (modifiedCtx)
1133 VMap->saveContext(S, Ctx);
1134}
1135
1136// Update local variable definitions in variable map
1137void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
1138 if (!BO->isAssignmentOp())
1139 return;
1140
1141 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1142
1143 // Update the variable map and current context.
1144 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
1145 ValueDecl *VDec = DRE->getDecl();
1146 if (Ctx.lookup(VDec)) {
1147 if (BO->getOpcode() == BO_Assign)
1148 Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
1149 else
1150 // FIXME -- handle compound assignment operators
1151 Ctx = VMap->clearDefinition(VDec, Ctx);
1152 VMap->saveContext(BO, Ctx);
1153 }
1154 }
1155}
1156
1157
1158// Computes the intersection of two contexts. The intersection is the
1159// set of variables which have the same definition in both contexts;
1160// variables with different definitions are discarded.
1161LocalVariableMap::Context
1162LocalVariableMap::intersectContexts(Context C1, Context C2) {
1163 Context Result = C1;
1164 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001165 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001166 unsigned i1 = I.getData();
1167 const unsigned *i2 = C2.lookup(Dec);
1168 if (!i2) // variable doesn't exist on second path
1169 Result = removeDefinition(Dec, Result);
1170 else if (*i2 != i1) // variable exists, but has different definition
1171 Result = clearDefinition(Dec, Result);
1172 }
1173 return Result;
1174}
1175
1176// For every variable in C, create a new variable that refers to the
1177// definition in C. Return a new context that contains these new variables.
1178// (We use this for a naive implementation of SSA on loop back-edges.)
1179LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
1180 Context Result = getEmptyContext();
1181 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001182 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001183 unsigned i = I.getData();
1184 Result = addReference(Dec, i, Result);
1185 }
1186 return Result;
1187}
1188
1189// This routine also takes the intersection of C1 and C2, but it does so by
1190// altering the VarDefinitions. C1 must be the result of an earlier call to
1191// createReferenceContext.
1192void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
1193 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001194 const NamedDecl *Dec = I.getKey();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001195 unsigned i1 = I.getData();
1196 VarDefinition *VDef = &VarDefinitions[i1];
1197 assert(VDef->isReference());
1198
1199 const unsigned *i2 = C2.lookup(Dec);
1200 if (!i2 || (*i2 != i1))
1201 VDef->Ref = 0; // Mark this variable as undefined
1202 }
1203}
1204
1205
1206// Traverse the CFG in topological order, so all predecessors of a block
1207// (excluding back-edges) are visited before the block itself. At
1208// each point in the code, we calculate a Context, which holds the set of
1209// variable definitions which are visible at that point in execution.
1210// Visible variables are mapped to their definitions using an array that
1211// contains all definitions.
1212//
1213// At join points in the CFG, the set is computed as the intersection of
1214// the incoming sets along each edge, E.g.
1215//
1216// { Context | VarDefinitions }
1217// int x = 0; { x -> x1 | x1 = 0 }
1218// int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
1219// if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... }
1220// else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... }
1221// ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... }
1222//
1223// This is essentially a simpler and more naive version of the standard SSA
1224// algorithm. Those definitions that remain in the intersection are from blocks
1225// that strictly dominate the current block. We do not bother to insert proper
1226// phi nodes, because they are not used in our analysis; instead, wherever
1227// a phi node would be required, we simply remove that definition from the
1228// context (E.g. x above).
1229//
1230// The initial traversal does not capture back-edges, so those need to be
1231// handled on a separate pass. Whenever the first pass encounters an
1232// incoming back edge, it duplicates the context, creating new definitions
1233// that refer back to the originals. (These correspond to places where SSA
1234// might have to insert a phi node.) On the second pass, these definitions are
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00001235// set to NULL if the variable has changed on the back-edge (i.e. a phi
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001236// node was actually required.) E.g.
1237//
1238// { Context | VarDefinitions }
1239// int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
1240// while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; }
1241// x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... }
1242// ... { y -> y1 | x3 = 2, x2 = 1, ... }
1243//
1244void LocalVariableMap::traverseCFG(CFG *CFGraph,
1245 PostOrderCFGView *SortedGraph,
1246 std::vector<CFGBlockInfo> &BlockInfo) {
1247 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
1248
1249 CtxIndices.resize(CFGraph->getNumBlockIDs());
1250
1251 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1252 E = SortedGraph->end(); I!= E; ++I) {
1253 const CFGBlock *CurrBlock = *I;
1254 int CurrBlockID = CurrBlock->getBlockID();
1255 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
1256
1257 VisitedBlocks.insert(CurrBlock);
1258
1259 // Calculate the entry context for the current block
1260 bool HasBackEdges = false;
1261 bool CtxInit = true;
1262 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
1263 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
1264 // if *PI -> CurrBlock is a back edge, so skip it
1265 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) {
1266 HasBackEdges = true;
1267 continue;
1268 }
1269
1270 int PrevBlockID = (*PI)->getBlockID();
1271 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
1272
1273 if (CtxInit) {
1274 CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
1275 CtxInit = false;
1276 }
1277 else {
1278 CurrBlockInfo->EntryContext =
1279 intersectContexts(CurrBlockInfo->EntryContext,
1280 PrevBlockInfo->ExitContext);
1281 }
1282 }
1283
1284 // Duplicate the context if we have back-edges, so we can call
1285 // intersectBackEdges later.
1286 if (HasBackEdges)
1287 CurrBlockInfo->EntryContext =
1288 createReferenceContext(CurrBlockInfo->EntryContext);
1289
1290 // Create a starting context index for the current block
1291 saveContext(0, CurrBlockInfo->EntryContext);
1292 CurrBlockInfo->EntryIndex = getContextIndex();
1293
1294 // Visit all the statements in the basic block.
1295 VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
1296 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1297 BE = CurrBlock->end(); BI != BE; ++BI) {
1298 switch (BI->getKind()) {
1299 case CFGElement::Statement: {
1300 const CFGStmt *CS = cast<CFGStmt>(&*BI);
1301 VMapBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
1302 break;
1303 }
1304 default:
1305 break;
1306 }
1307 }
1308 CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
1309
1310 // Mark variables on back edges as "unknown" if they've been changed.
1311 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1312 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1313 // if CurrBlock -> *SI is *not* a back edge
1314 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
1315 continue;
1316
1317 CFGBlock *FirstLoopBlock = *SI;
1318 Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
1319 Context LoopEnd = CurrBlockInfo->ExitContext;
1320 intersectBackEdge(LoopBegin, LoopEnd);
1321 }
1322 }
1323
1324 // Put an extra entry at the end of the indexed context array
1325 unsigned exitID = CFGraph->getExit().getBlockID();
1326 saveContext(0, BlockInfo[exitID].ExitContext);
1327}
1328
Richard Smith2e515622012-02-03 04:45:26 +00001329/// Find the appropriate source locations to use when producing diagnostics for
1330/// each block in the CFG.
1331static void findBlockLocations(CFG *CFGraph,
1332 PostOrderCFGView *SortedGraph,
1333 std::vector<CFGBlockInfo> &BlockInfo) {
1334 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1335 E = SortedGraph->end(); I!= E; ++I) {
1336 const CFGBlock *CurrBlock = *I;
1337 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
1338
1339 // Find the source location of the last statement in the block, if the
1340 // block is not empty.
1341 if (const Stmt *S = CurrBlock->getTerminator()) {
1342 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
1343 } else {
1344 for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
1345 BE = CurrBlock->rend(); BI != BE; ++BI) {
1346 // FIXME: Handle other CFGElement kinds.
1347 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
1348 CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
1349 break;
1350 }
1351 }
1352 }
1353
1354 if (!CurrBlockInfo->ExitLoc.isInvalid()) {
1355 // This block contains at least one statement. Find the source location
1356 // of the first statement in the block.
1357 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1358 BE = CurrBlock->end(); BI != BE; ++BI) {
1359 // FIXME: Handle other CFGElement kinds.
1360 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
1361 CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
1362 break;
1363 }
1364 }
1365 } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
1366 CurrBlock != &CFGraph->getExit()) {
1367 // The block is empty, and has a single predecessor. Use its exit
1368 // location.
1369 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
1370 BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
1371 }
1372 }
1373}
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001374
1375/// \brief Class which implements the core thread safety analysis routines.
1376class ThreadSafetyAnalyzer {
1377 friend class BuildLockset;
1378
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001379 ThreadSafetyHandler &Handler;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001380 LocalVariableMap LocalVarMap;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001381 FactManager FactMan;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001382 std::vector<CFGBlockInfo> BlockInfo;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001383
1384public:
1385 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
1386
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001387 void addLock(FactSet &FSet, const SExpr &Mutex, const LockData &LDat);
1388 void removeLock(FactSet &FSet, const SExpr &Mutex,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001389 SourceLocation UnlockLoc, bool FullyRemove=false);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001390
1391 template <typename AttrType>
1392 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1393 const NamedDecl *D);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001394
1395 template <class AttrType>
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001396 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1397 const NamedDecl *D,
1398 const CFGBlock *PredBlock, const CFGBlock *CurrBlock,
1399 Expr *BrE, bool Neg);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001400
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001401 const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
1402 bool &Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001403
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001404 void getEdgeLockset(FactSet &Result, const FactSet &ExitSet,
1405 const CFGBlock* PredBlock,
1406 const CFGBlock *CurrBlock);
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001407
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001408 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1409 SourceLocation JoinLoc,
1410 LockErrorKind LEK1, LockErrorKind LEK2,
1411 bool Modify=true);
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001412
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001413 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1414 SourceLocation JoinLoc, LockErrorKind LEK1,
1415 bool Modify=true) {
1416 intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify);
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001417 }
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001418
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001419 void runAnalysis(AnalysisDeclContext &AC);
1420};
1421
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001422
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001423/// \brief Add a new lock to the lockset, warning if the lock is already there.
1424/// \param Mutex -- the Mutex expression for the lock
1425/// \param LDat -- the LockData for the lock
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001426void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const SExpr &Mutex,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001427 const LockData &LDat) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001428 // FIXME: deal with acquired before/after annotations.
1429 // FIXME: Don't always warn when we have support for reentrant locks.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +00001430 if (Mutex.shouldIgnore())
1431 return;
1432
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001433 if (FSet.findLock(FactMan, Mutex)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001434 Handler.handleDoubleLock(Mutex.toString(), LDat.AcquireLoc);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001435 } else {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001436 FSet.addLock(FactMan, Mutex, LDat);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001437 }
1438}
1439
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001440
1441/// \brief Remove a lock from the lockset, warning if the lock is not there.
Ted Kremenekad0fe032012-08-22 23:50:41 +00001442/// \param Mutex The lock expression corresponding to the lock to be removed
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001443/// \param UnlockLoc The source location of the unlock (only used in error msg)
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001444void ThreadSafetyAnalyzer::removeLock(FactSet &FSet,
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001445 const SExpr &Mutex,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001446 SourceLocation UnlockLoc,
1447 bool FullyRemove) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +00001448 if (Mutex.shouldIgnore())
1449 return;
1450
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001451 const LockData *LDat = FSet.findLock(FactMan, Mutex);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001452 if (!LDat) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001453 Handler.handleUnmatchedUnlock(Mutex.toString(), UnlockLoc);
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001454 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001455 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001456
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001457 if (LDat->UnderlyingMutex.isValid()) {
1458 // This is scoped lockable object, which manages the real mutex.
1459 if (FullyRemove) {
1460 // We're destroying the managing object.
1461 // Remove the underlying mutex if it exists; but don't warn.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001462 if (FSet.findLock(FactMan, LDat->UnderlyingMutex))
1463 FSet.removeLock(FactMan, LDat->UnderlyingMutex);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001464 } else {
1465 // We're releasing the underlying mutex, but not destroying the
1466 // managing object. Warn on dual release.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001467 if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001468 Handler.handleUnmatchedUnlock(LDat->UnderlyingMutex.toString(),
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001469 UnlockLoc);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001470 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001471 FSet.removeLock(FactMan, LDat->UnderlyingMutex);
1472 return;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001473 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001474 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001475 FSet.removeLock(FactMan, Mutex);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001476}
1477
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00001478
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001479/// \brief Extract the list of mutexIDs from the attribute on an expression,
1480/// and push them onto Mtxs, discarding any duplicates.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001481template <typename AttrType>
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001482void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1483 Expr *Exp, const NamedDecl *D) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001484 typedef typename AttrType::args_iterator iterator_type;
1485
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001486 if (Attr->args_size() == 0) {
1487 // The mutex held is the "this" object.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001488 SExpr Mu(0, Exp, D);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001489 if (!Mu.isValid())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001490 SExpr::warnInvalidLock(Handler, 0, Exp, D);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001491 else
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001492 Mtxs.push_back_nodup(Mu);
1493 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001494 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001495
1496 for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001497 SExpr Mu(*I, Exp, D);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001498 if (!Mu.isValid())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001499 SExpr::warnInvalidLock(Handler, *I, Exp, D);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001500 else
1501 Mtxs.push_back_nodup(Mu);
1502 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001503}
1504
1505
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001506/// \brief Extract the list of mutexIDs from a trylock attribute. If the
1507/// trylock applies to the given edge, then push them onto Mtxs, discarding
1508/// any duplicates.
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001509template <class AttrType>
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001510void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1511 Expr *Exp, const NamedDecl *D,
1512 const CFGBlock *PredBlock,
1513 const CFGBlock *CurrBlock,
1514 Expr *BrE, bool Neg) {
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001515 // Find out which branch has the lock
1516 bool branch = 0;
1517 if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) {
1518 branch = BLE->getValue();
1519 }
1520 else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) {
1521 branch = ILE->getValue().getBoolValue();
1522 }
1523 int branchnum = branch ? 0 : 1;
1524 if (Neg) branchnum = !branchnum;
1525
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001526 // If we've taken the trylock branch, then add the lock
1527 int i = 0;
1528 for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
1529 SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
1530 if (*SI == CurrBlock && i == branchnum) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001531 getMutexIDs(Mtxs, Attr, Exp, D);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001532 }
1533 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001534}
1535
1536
DeLesley Hutchins13106112012-07-10 21:47:55 +00001537bool getStaticBooleanValue(Expr* E, bool& TCond) {
1538 if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) {
1539 TCond = false;
1540 return true;
1541 } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) {
1542 TCond = BLE->getValue();
1543 return true;
1544 } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) {
1545 TCond = ILE->getValue().getBoolValue();
1546 return true;
1547 } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
1548 return getStaticBooleanValue(CE->getSubExpr(), TCond);
1549 }
1550 return false;
1551}
1552
1553
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001554// If Cond can be traced back to a function call, return the call expression.
1555// The negate variable should be called with false, and will be set to true
1556// if the function call is negated, e.g. if (!mu.tryLock(...))
1557const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
1558 LocalVarContext C,
1559 bool &Negate) {
1560 if (!Cond)
1561 return 0;
1562
1563 if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
1564 return CallExp;
1565 }
DeLesley Hutchins13106112012-07-10 21:47:55 +00001566 else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) {
1567 return getTrylockCallExpr(PE->getSubExpr(), C, Negate);
1568 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001569 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
1570 return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
1571 }
DeLesley Hutchinsfd0f11c2012-09-05 20:01:16 +00001572 else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) {
1573 return getTrylockCallExpr(EWC->getSubExpr(), C, Negate);
1574 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001575 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
1576 const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
1577 return getTrylockCallExpr(E, C, Negate);
1578 }
1579 else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
1580 if (UOP->getOpcode() == UO_LNot) {
1581 Negate = !Negate;
1582 return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
1583 }
DeLesley Hutchins13106112012-07-10 21:47:55 +00001584 return 0;
1585 }
1586 else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) {
1587 if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) {
1588 if (BOP->getOpcode() == BO_NE)
1589 Negate = !Negate;
1590
1591 bool TCond = false;
1592 if (getStaticBooleanValue(BOP->getRHS(), TCond)) {
1593 if (!TCond) Negate = !Negate;
1594 return getTrylockCallExpr(BOP->getLHS(), C, Negate);
1595 }
1596 else if (getStaticBooleanValue(BOP->getLHS(), TCond)) {
1597 if (!TCond) Negate = !Negate;
1598 return getTrylockCallExpr(BOP->getRHS(), C, Negate);
1599 }
1600 return 0;
1601 }
1602 return 0;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001603 }
1604 // FIXME -- handle && and || as well.
DeLesley Hutchins13106112012-07-10 21:47:55 +00001605 return 0;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001606}
1607
1608
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001609/// \brief Find the lockset that holds on the edge between PredBlock
1610/// and CurrBlock. The edge set is the exit set of PredBlock (passed
1611/// as the ExitSet parameter) plus any trylocks, which are conditionally held.
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001612void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result,
1613 const FactSet &ExitSet,
1614 const CFGBlock *PredBlock,
1615 const CFGBlock *CurrBlock) {
1616 Result = ExitSet;
1617
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001618 if (!PredBlock->getTerminatorCondition())
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001619 return;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00001620
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001621 bool Negate = false;
1622 const Stmt *Cond = PredBlock->getTerminatorCondition();
1623 const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
1624 const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
1625
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001626 CallExpr *Exp =
1627 const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate));
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001628 if (!Exp)
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001629 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001630
1631 NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1632 if(!FunDecl || !FunDecl->hasAttrs())
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001633 return;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001634
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001635
1636 MutexIDList ExclusiveLocksToAdd;
1637 MutexIDList SharedLocksToAdd;
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001638
1639 // If the condition is a call to a Trylock function, then grab the attributes
1640 AttrVec &ArgAttrs = FunDecl->getAttrs();
1641 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
1642 Attr *Attr = ArgAttrs[i];
1643 switch (Attr->getKind()) {
1644 case attr::ExclusiveTrylockFunction: {
1645 ExclusiveTrylockFunctionAttr *A =
1646 cast<ExclusiveTrylockFunctionAttr>(Attr);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001647 getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl,
1648 PredBlock, CurrBlock, A->getSuccessValue(), Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001649 break;
1650 }
1651 case attr::SharedTrylockFunction: {
1652 SharedTrylockFunctionAttr *A =
1653 cast<SharedTrylockFunctionAttr>(Attr);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001654 getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl,
1655 PredBlock, CurrBlock, A->getSuccessValue(), Negate);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001656 break;
1657 }
1658 default:
1659 break;
1660 }
1661 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001662
1663 // Add and remove locks.
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001664 SourceLocation Loc = Exp->getExprLoc();
1665 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001666 addLock(Result, ExclusiveLocksToAdd[i],
1667 LockData(Loc, LK_Exclusive));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001668 }
1669 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001670 addLock(Result, SharedLocksToAdd[i],
1671 LockData(Loc, LK_Shared));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001672 }
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001673}
1674
1675
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001676/// \brief We use this class to visit different types of expressions in
1677/// CFGBlocks, and build up the lockset.
1678/// An expression may cause us to add or remove locks from the lockset, or else
1679/// output error messages related to missing locks.
1680/// FIXME: In future, we may be able to not inherit from a visitor.
1681class BuildLockset : public StmtVisitor<BuildLockset> {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001682 friend class ThreadSafetyAnalyzer;
1683
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001684 ThreadSafetyAnalyzer *Analyzer;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001685 FactSet FSet;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001686 LocalVariableMap::Context LVarCtx;
1687 unsigned CtxIndex;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001688
1689 // Helper functions
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001690 const ValueDecl *getValueDecl(Expr *Exp);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001691
1692 void warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, AccessKind AK,
1693 Expr *MutexExp, ProtectedOperationKind POK);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001694 void warnIfMutexHeld(const NamedDecl *D, Expr *Exp, Expr *MutexExp);
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001695
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001696 void checkAccess(Expr *Exp, AccessKind AK);
1697 void checkDereference(Expr *Exp, AccessKind AK);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001698 void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = 0);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001699
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001700public:
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001701 BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001702 : StmtVisitor<BuildLockset>(),
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001703 Analyzer(Anlzr),
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001704 FSet(Info.EntrySet),
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001705 LVarCtx(Info.EntryContext),
1706 CtxIndex(Info.EntryIndex)
1707 {}
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001708
1709 void VisitUnaryOperator(UnaryOperator *UO);
1710 void VisitBinaryOperator(BinaryOperator *BO);
1711 void VisitCastExpr(CastExpr *CE);
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001712 void VisitCallExpr(CallExpr *Exp);
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001713 void VisitCXXConstructExpr(CXXConstructExpr *Exp);
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001714 void VisitDeclStmt(DeclStmt *S);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001715};
1716
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001717
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001718/// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
1719const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) {
1720 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
1721 return DR->getDecl();
1722
1723 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
1724 return ME->getMemberDecl();
1725
1726 return 0;
1727}
1728
1729/// \brief Warn if the LSet does not contain a lock sufficient to protect access
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001730/// of at least the passed in AccessKind.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001731void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp,
1732 AccessKind AK, Expr *MutexExp,
1733 ProtectedOperationKind POK) {
1734 LockKind LK = getLockKindFromAccessKind(AK);
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001735
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001736 SExpr Mutex(MutexExp, Exp, D);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001737 if (!Mutex.isValid()) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001738 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001739 return;
1740 } else if (Mutex.shouldIgnore()) {
1741 return;
1742 }
1743
1744 LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex);
1745 if (!LDat || !LDat->isAtLeast(LK))
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001746 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001747 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001748}
1749
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001750/// \brief Warn if the LSet contains the given lock.
1751void BuildLockset::warnIfMutexHeld(const NamedDecl *D, Expr* Exp,
1752 Expr *MutexExp) {
1753 SExpr Mutex(MutexExp, Exp, D);
1754 if (!Mutex.isValid()) {
1755 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
1756 return;
1757 }
1758
1759 LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex);
1760 if (LDat)
1761 Analyzer->Handler.handleFunExcludesLock(D->getName(), Mutex.toString(),
1762 Exp->getExprLoc());
1763}
1764
1765
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001766/// \brief This method identifies variable dereferences and checks pt_guarded_by
1767/// and pt_guarded_var annotations. Note that we only check these annotations
1768/// at the time a pointer is dereferenced.
1769/// FIXME: We need to check for other types of pointer dereferences
1770/// (e.g. [], ->) and deal with them here.
1771/// \param Exp An expression that has been read or written.
1772void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) {
1773 UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp);
1774 if (!UO || UO->getOpcode() != clang::UO_Deref)
1775 return;
1776 Exp = UO->getSubExpr()->IgnoreParenCasts();
1777
1778 const ValueDecl *D = getValueDecl(Exp);
1779 if(!D || !D->hasAttrs())
1780 return;
1781
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001782 if (D->getAttr<PtGuardedVarAttr>() && FSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001783 Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK,
1784 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001785
1786 const AttrVec &ArgAttrs = D->getAttrs();
1787 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1788 if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
1789 warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference);
1790}
1791
1792/// \brief Checks guarded_by and guarded_var attributes.
1793/// Whenever we identify an access (read or write) of a DeclRefExpr or
1794/// MemberExpr, we need to check whether there are any guarded_by or
1795/// guarded_var attributes, and make sure we hold the appropriate mutexes.
1796void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) {
1797 const ValueDecl *D = getValueDecl(Exp);
1798 if(!D || !D->hasAttrs())
1799 return;
1800
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001801 if (D->getAttr<GuardedVarAttr>() && FSet.isEmpty())
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001802 Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK,
1803 Exp->getExprLoc());
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001804
1805 const AttrVec &ArgAttrs = D->getAttrs();
1806 for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1807 if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
1808 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
1809}
1810
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001811/// \brief Process a function call, method call, constructor call,
1812/// or destructor call. This involves looking at the attributes on the
1813/// corresponding function/method/constructor/destructor, issuing warnings,
1814/// and updating the locksets accordingly.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001815///
1816/// FIXME: For classes annotated with one of the guarded annotations, we need
1817/// to treat const method calls as reads and non-const method calls as writes,
1818/// and check that the appropriate locks are held. Non-const method calls with
1819/// the same signature as const method calls can be also treated as reads.
1820///
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001821void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) {
1822 const AttrVec &ArgAttrs = D->getAttrs();
1823 MutexIDList ExclusiveLocksToAdd;
1824 MutexIDList SharedLocksToAdd;
1825 MutexIDList LocksToRemove;
1826
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001827 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001828 Attr *At = const_cast<Attr*>(ArgAttrs[i]);
1829 switch (At->getKind()) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001830 // When we encounter an exclusive lock function, we need to add the lock
1831 // to our lockset with kind exclusive.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001832 case attr::ExclusiveLockFunction: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001833 ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(At);
1834 Analyzer->getMutexIDs(ExclusiveLocksToAdd, A, Exp, D);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001835 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001836 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001837
1838 // When we encounter a shared lock function, we need to add the lock
1839 // to our lockset with kind shared.
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001840 case attr::SharedLockFunction: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001841 SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(At);
1842 Analyzer->getMutexIDs(SharedLocksToAdd, A, Exp, D);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001843 break;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00001844 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001845
1846 // When we encounter an unlock function, we need to remove unlocked
1847 // mutexes from the lockset, and flag a warning if they are not there.
1848 case attr::UnlockFunction: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001849 UnlockFunctionAttr *A = cast<UnlockFunctionAttr>(At);
1850 Analyzer->getMutexIDs(LocksToRemove, A, Exp, D);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001851 break;
1852 }
1853
1854 case attr::ExclusiveLocksRequired: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001855 ExclusiveLocksRequiredAttr *A = cast<ExclusiveLocksRequiredAttr>(At);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001856
1857 for (ExclusiveLocksRequiredAttr::args_iterator
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001858 I = A->args_begin(), E = A->args_end(); I != E; ++I)
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001859 warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
1860 break;
1861 }
1862
1863 case attr::SharedLocksRequired: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001864 SharedLocksRequiredAttr *A = cast<SharedLocksRequiredAttr>(At);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001865
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001866 for (SharedLocksRequiredAttr::args_iterator I = A->args_begin(),
1867 E = A->args_end(); I != E; ++I)
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001868 warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
1869 break;
1870 }
1871
1872 case attr::LocksExcluded: {
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001873 LocksExcludedAttr *A = cast<LocksExcludedAttr>(At);
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001874
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001875 for (LocksExcludedAttr::args_iterator I = A->args_begin(),
1876 E = A->args_end(); I != E; ++I) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00001877 warnIfMutexHeld(D, Exp, *I);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001878 }
1879 break;
1880 }
1881
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001882 // Ignore other (non thread-safety) attributes
1883 default:
1884 break;
1885 }
1886 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001887
1888 // Figure out if we're calling the constructor of scoped lockable class
1889 bool isScopedVar = false;
1890 if (VD) {
1891 if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) {
1892 const CXXRecordDecl* PD = CD->getParent();
1893 if (PD && PD->getAttr<ScopedLockableAttr>())
1894 isScopedVar = true;
1895 }
1896 }
1897
1898 // Add locks.
1899 SourceLocation Loc = Exp->getExprLoc();
1900 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001901 Analyzer->addLock(FSet, ExclusiveLocksToAdd[i],
1902 LockData(Loc, LK_Exclusive, isScopedVar));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001903 }
1904 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001905 Analyzer->addLock(FSet, SharedLocksToAdd[i],
1906 LockData(Loc, LK_Shared, isScopedVar));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001907 }
1908
1909 // Add the managing object as a dummy mutex, mapped to the underlying mutex.
1910 // FIXME -- this doesn't work if we acquire multiple locks.
1911 if (isScopedVar) {
1912 SourceLocation MLoc = VD->getLocation();
1913 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00001914 SExpr SMutex(&DRE, 0, 0);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001915
1916 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001917 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive,
1918 ExclusiveLocksToAdd[i]));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001919 }
1920 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001921 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared,
1922 SharedLocksToAdd[i]));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001923 }
1924 }
1925
1926 // Remove locks.
1927 // FIXME -- should only fully remove if the attribute refers to 'this'.
1928 bool Dtor = isa<CXXDestructorDecl>(D);
1929 for (unsigned i=0,n=LocksToRemove.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00001930 Analyzer->removeLock(FSet, LocksToRemove[i], Loc, Dtor);
DeLesley Hutchins5381c052012-07-05 21:16:29 +00001931 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00001932}
1933
DeLesley Hutchinsb4fa4182012-01-06 19:16:50 +00001934
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001935/// \brief For unary operations which read and write a variable, we need to
1936/// check whether we hold any required mutexes. Reads are checked in
1937/// VisitCastExpr.
1938void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
1939 switch (UO->getOpcode()) {
1940 case clang::UO_PostDec:
1941 case clang::UO_PostInc:
1942 case clang::UO_PreDec:
1943 case clang::UO_PreInc: {
1944 Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts();
1945 checkAccess(SubExp, AK_Written);
1946 checkDereference(SubExp, AK_Written);
1947 break;
1948 }
1949 default:
1950 break;
1951 }
1952}
1953
1954/// For binary operations which assign to a variable (writes), we need to check
1955/// whether we hold any required mutexes.
1956/// FIXME: Deal with non-primitive types.
1957void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
1958 if (!BO->isAssignmentOp())
1959 return;
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001960
1961 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001962 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001963
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001964 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1965 checkAccess(LHSExp, AK_Written);
1966 checkDereference(LHSExp, AK_Written);
1967}
1968
1969/// Whenever we do an LValue to Rvalue cast, we are reading a variable and
1970/// need to ensure we hold any required mutexes.
1971/// FIXME: Deal with non-primitive types.
1972void BuildLockset::VisitCastExpr(CastExpr *CE) {
1973 if (CE->getCastKind() != CK_LValueToRValue)
1974 return;
1975 Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts();
1976 checkAccess(SubExp, AK_Read);
1977 checkDereference(SubExp, AK_Read);
1978}
1979
1980
DeLesley Hutchinsdf497822011-12-29 00:56:48 +00001981void BuildLockset::VisitCallExpr(CallExpr *Exp) {
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00001982 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1983 if(!D || !D->hasAttrs())
1984 return;
1985 handleCall(Exp, D);
1986}
1987
1988void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001989 // FIXME -- only handles constructors in DeclStmt below.
1990}
1991
1992void BuildLockset::VisitDeclStmt(DeclStmt *S) {
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001993 // adjust the context
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00001994 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00001995
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001996 DeclGroupRef DGrp = S->getDeclGroup();
1997 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1998 Decl *D = *I;
1999 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
2000 Expr *E = VD->getInit();
DeLesley Hutchins9d6e7f32012-07-03 18:25:56 +00002001 // handle constructors that involve temporaries
2002 if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E))
2003 E = EWC->getSubExpr();
2004
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00002005 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
2006 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
2007 if (!CtorD || !CtorD->hasAttrs())
2008 return;
2009 handleCall(CE, CtorD, VD);
2010 }
2011 }
2012 }
DeLesley Hutchinse0eaa852011-10-21 18:06:53 +00002013}
2014
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002015
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002016
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00002017/// \brief Compute the intersection of two locksets and issue warnings for any
2018/// locks in the symmetric difference.
2019///
2020/// This function is used at a merge point in the CFG when comparing the lockset
2021/// of each branch being merged. For example, given the following sequence:
2022/// A; if () then B; else C; D; we need to check that the lockset after B and C
2023/// are the same. In the event of a difference, we use the intersection of these
2024/// two locksets at the start of D.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002025///
Ted Kremenekad0fe032012-08-22 23:50:41 +00002026/// \param FSet1 The first lockset.
2027/// \param FSet2 The second lockset.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002028/// \param JoinLoc The location of the join point for error reporting
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002029/// \param LEK1 The error message to report if a mutex is missing from LSet1
2030/// \param LEK2 The error message to report if a mutex is missing from Lset2
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002031void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1,
2032 const FactSet &FSet2,
2033 SourceLocation JoinLoc,
2034 LockErrorKind LEK1,
2035 LockErrorKind LEK2,
2036 bool Modify) {
2037 FactSet FSet1Orig = FSet1;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002038
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002039 for (FactSet::const_iterator I = FSet2.begin(), E = FSet2.end();
2040 I != E; ++I) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002041 const SExpr &FSet2Mutex = FactMan[*I].MutID;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002042 const LockData &LDat2 = FactMan[*I].LDat;
2043
2044 if (const LockData *LDat1 = FSet1.findLock(FactMan, FSet2Mutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002045 if (LDat1->LKind != LDat2.LKind) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002046 Handler.handleExclusiveAndShared(FSet2Mutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002047 LDat2.AcquireLoc,
2048 LDat1->AcquireLoc);
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002049 if (Modify && LDat1->LKind != LK_Exclusive) {
2050 FSet1.removeLock(FactMan, FSet2Mutex);
2051 FSet1.addLock(FactMan, FSet2Mutex, LDat2);
2052 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002053 }
2054 } else {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002055 if (LDat2.UnderlyingMutex.isValid()) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002056 if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002057 // If this is a scoped lock that manages another mutex, and if the
2058 // underlying mutex is still held, then warn about the underlying
2059 // mutex.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002060 Handler.handleMutexHeldEndOfScope(LDat2.UnderlyingMutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002061 LDat2.AcquireLoc,
2062 JoinLoc, LEK1);
2063 }
2064 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00002065 else if (!LDat2.Managed && !FSet2Mutex.isUniversal())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002066 Handler.handleMutexHeldEndOfScope(FSet2Mutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002067 LDat2.AcquireLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002068 JoinLoc, LEK1);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002069 }
2070 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002071
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002072 for (FactSet::const_iterator I = FSet1.begin(), E = FSet1.end();
2073 I != E; ++I) {
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002074 const SExpr &FSet1Mutex = FactMan[*I].MutID;
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002075 const LockData &LDat1 = FactMan[*I].LDat;
DeLesley Hutchinsc99a5d82012-06-28 22:42:48 +00002076
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002077 if (!FSet2.findLock(FactMan, FSet1Mutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002078 if (LDat1.UnderlyingMutex.isValid()) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002079 if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) {
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002080 // If this is a scoped lock that manages another mutex, and if the
2081 // underlying mutex is still held, then warn about the underlying
2082 // mutex.
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002083 Handler.handleMutexHeldEndOfScope(LDat1.UnderlyingMutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002084 LDat1.AcquireLoc,
2085 JoinLoc, LEK1);
2086 }
2087 }
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +00002088 else if (!LDat1.Managed && !FSet1Mutex.isUniversal())
DeLesley Hutchinsa74b7152012-08-10 20:19:55 +00002089 Handler.handleMutexHeldEndOfScope(FSet1Mutex.toString(),
DeLesley Hutchinsbbe33412012-07-02 22:26:29 +00002090 LDat1.AcquireLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002091 JoinLoc, LEK2);
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002092 if (Modify)
2093 FSet1.removeLock(FactMan, FSet1Mutex);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002094 }
2095 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002096}
2097
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002098
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002099
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002100/// \brief Check a function's CFG for thread-safety violations.
2101///
2102/// We traverse the blocks in the CFG, compute the set of mutexes that are held
2103/// at the end of each block, and issue warnings for thread safety violations.
2104/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00002105void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002106 CFG *CFGraph = AC.getCFG();
2107 if (!CFGraph) return;
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00002108 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
2109
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002110 // AC.dumpCFG(true);
2111
DeLesley Hutchins9f80a972011-10-17 21:33:35 +00002112 if (!D)
2113 return; // Ignore anonymous functions for now.
2114 if (D->getAttr<NoThreadSafetyAnalysisAttr>())
2115 return;
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002116 // FIXME: Do something a bit more intelligent inside constructor and
2117 // destructor code. Constructors and destructors must assume unique access
2118 // to 'this', so checks on member variable access is disabled, but we should
2119 // still enable checks on other objects.
2120 if (isa<CXXConstructorDecl>(D))
2121 return; // Don't check inside constructors.
2122 if (isa<CXXDestructorDecl>(D))
2123 return; // Don't check inside destructors.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002124
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00002125 BlockInfo.resize(CFGraph->getNumBlockIDs(),
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002126 CFGBlockInfo::getEmptyBlockInfo(LocalVarMap));
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002127
2128 // We need to explore the CFG via a "topological" ordering.
2129 // That way, we will be guaranteed to have information about required
2130 // predecessor locksets when exploring a new block.
Ted Kremenek439ed162011-10-22 02:14:27 +00002131 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
2132 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002133
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002134 // Compute SSA names for local variables
2135 LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
2136
Richard Smith2e515622012-02-03 04:45:26 +00002137 // Fill in source locations for all CFGBlocks.
2138 findBlockLocations(CFGraph, SortedGraph, BlockInfo);
2139
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002140 // Add locks from exclusive_locks_required and shared_locks_required
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002141 // to initial lockset. Also turn off checking for lock and unlock functions.
2142 // FIXME: is there a more intelligent way to check lock/unlock functions?
Ted Kremenek439ed162011-10-22 02:14:27 +00002143 if (!SortedGraph->empty() && D->hasAttrs()) {
2144 const CFGBlock *FirstBlock = *SortedGraph->begin();
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002145 FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002146 const AttrVec &ArgAttrs = D->getAttrs();
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002147
2148 MutexIDList ExclusiveLocksToAdd;
2149 MutexIDList SharedLocksToAdd;
2150
2151 SourceLocation Loc = D->getLocation();
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002152 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002153 Attr *Attr = ArgAttrs[i];
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002154 Loc = Attr->getLocation();
2155 if (ExclusiveLocksRequiredAttr *A
2156 = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
2157 getMutexIDs(ExclusiveLocksToAdd, A, (Expr*) 0, D);
2158 } else if (SharedLocksRequiredAttr *A
2159 = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
2160 getMutexIDs(SharedLocksToAdd, A, (Expr*) 0, D);
DeLesley Hutchins2f13bec2012-02-16 17:13:43 +00002161 } else if (isa<UnlockFunctionAttr>(Attr)) {
2162 // Don't try to check unlock functions for now
2163 return;
2164 } else if (isa<ExclusiveLockFunctionAttr>(Attr)) {
2165 // Don't try to check lock functions for now
2166 return;
2167 } else if (isa<SharedLockFunctionAttr>(Attr)) {
2168 // Don't try to check lock functions for now
2169 return;
DeLesley Hutchins76f0a6e2012-07-02 21:59:24 +00002170 } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) {
2171 // Don't try to check trylock functions for now
2172 return;
2173 } else if (isa<SharedTrylockFunctionAttr>(Attr)) {
2174 // Don't try to check trylock functions for now
2175 return;
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002176 }
2177 }
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002178
2179 // FIXME -- Loc can be wrong here.
2180 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002181 addLock(InitialLockset, ExclusiveLocksToAdd[i],
2182 LockData(Loc, LK_Exclusive));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002183 }
2184 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002185 addLock(InitialLockset, SharedLocksToAdd[i],
2186 LockData(Loc, LK_Shared));
DeLesley Hutchins5381c052012-07-05 21:16:29 +00002187 }
Caitlin Sadowskicb967512011-09-15 17:43:08 +00002188 }
2189
Ted Kremenek439ed162011-10-22 02:14:27 +00002190 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
2191 E = SortedGraph->end(); I!= E; ++I) {
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002192 const CFGBlock *CurrBlock = *I;
2193 int CurrBlockID = CurrBlock->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002194 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002195
2196 // Use the default initial lockset in case there are no predecessors.
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002197 VisitedBlocks.insert(CurrBlock);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002198
2199 // Iterate through the predecessor blocks and warn if the lockset for all
2200 // predecessors is not the same. We take the entry lockset of the current
2201 // block to be the intersection of all previous locksets.
2202 // FIXME: By keeping the intersection, we may output more errors in future
2203 // for a lock which is not in the intersection, but was in the union. We
2204 // may want to also keep the union in future. As an example, let's say
2205 // the intersection contains Mutex L, and the union contains L and M.
2206 // Later we unlock M. At this point, we would output an error because we
2207 // never locked M; although the real error is probably that we forgot to
2208 // lock M on all code paths. Conversely, let's say that later we lock M.
2209 // In this case, we should compare against the intersection instead of the
2210 // union because the real error is probably that we forgot to unlock M on
2211 // all code paths.
2212 bool LocksetInitialized = false;
Richard Smithaacde712012-02-03 03:30:07 +00002213 llvm::SmallVector<CFGBlock*, 8> SpecialBlocks;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002214 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
2215 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
2216
2217 // if *PI -> CurrBlock is a back edge
2218 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
2219 continue;
2220
DeLesley Hutchins2a35be82012-03-02 22:02:58 +00002221 // Ignore edges from blocks that can't return.
2222 if ((*PI)->hasNoReturnElement())
2223 continue;
2224
Richard Smithaacde712012-02-03 03:30:07 +00002225 // If the previous block ended in a 'continue' or 'break' statement, then
2226 // a difference in locksets is probably due to a bug in that block, rather
2227 // than in some other predecessor. In that case, keep the other
2228 // predecessor's lockset.
2229 if (const Stmt *Terminator = (*PI)->getTerminator()) {
2230 if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
2231 SpecialBlocks.push_back(*PI);
2232 continue;
2233 }
2234 }
2235
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002236 int PrevBlockID = (*PI)->getBlockID();
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002237 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002238 FactSet PrevLockset;
2239 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock);
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002240
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002241 if (!LocksetInitialized) {
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002242 CurrBlockInfo->EntrySet = PrevLockset;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002243 LocksetInitialized = true;
2244 } else {
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002245 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2246 CurrBlockInfo->EntryLoc,
2247 LEK_LockedSomePredecessors);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002248 }
2249 }
2250
Richard Smithaacde712012-02-03 03:30:07 +00002251 // Process continue and break blocks. Assume that the lockset for the
2252 // resulting block is unaffected by any discrepancies in them.
2253 for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size();
2254 SpecialI < SpecialN; ++SpecialI) {
2255 CFGBlock *PrevBlock = SpecialBlocks[SpecialI];
2256 int PrevBlockID = PrevBlock->getBlockID();
2257 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2258
2259 if (!LocksetInitialized) {
2260 CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
2261 LocksetInitialized = true;
2262 } else {
2263 // Determine whether this edge is a loop terminator for diagnostic
2264 // purposes. FIXME: A 'break' statement might be a loop terminator, but
2265 // it might also be part of a switch. Also, a subsequent destructor
2266 // might add to the lockset, in which case the real issue might be a
2267 // double lock on the other path.
2268 const Stmt *Terminator = PrevBlock->getTerminator();
2269 bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
2270
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002271 FactSet PrevLockset;
2272 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet,
2273 PrevBlock, CurrBlock);
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002274
Richard Smithaacde712012-02-03 03:30:07 +00002275 // Do not update EntrySet.
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002276 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2277 PrevBlockInfo->ExitLoc,
Richard Smithaacde712012-02-03 03:30:07 +00002278 IsLoop ? LEK_LockedSomeLoopIterations
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002279 : LEK_LockedSomePredecessors,
2280 false);
Richard Smithaacde712012-02-03 03:30:07 +00002281 }
2282 }
2283
DeLesley Hutchins54c350a2012-04-19 16:48:43 +00002284 BuildLockset LocksetBuilder(this, *CurrBlockInfo);
2285
DeLesley Hutchinsb37d2b52012-01-06 18:36:09 +00002286 // Visit all the statements in the basic block.
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002287 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
2288 BE = CurrBlock->end(); BI != BE; ++BI) {
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00002289 switch (BI->getKind()) {
2290 case CFGElement::Statement: {
2291 const CFGStmt *CS = cast<CFGStmt>(&*BI);
2292 LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
2293 break;
2294 }
2295 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
2296 case CFGElement::AutomaticObjectDtor: {
2297 const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI);
2298 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>(
2299 AD->getDestructorDecl(AC.getASTContext()));
2300 if (!DD->hasAttrs())
2301 break;
2302
2303 // Create a dummy expression,
2304 VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl());
John McCallf4b88a42012-03-10 09:33:50 +00002305 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
DeLesley Hutchins6db51f72011-10-21 20:51:27 +00002306 AD->getTriggerStmt()->getLocEnd());
2307 LocksetBuilder.handleCall(&DRE, DD);
2308 break;
2309 }
2310 default:
2311 break;
2312 }
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002313 }
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002314 CurrBlockInfo->ExitSet = LocksetBuilder.FSet;
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002315
2316 // For every back edge from CurrBlock (the end of the loop) to another block
2317 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
2318 // the one held at the beginning of FirstLoopBlock. We can look up the
2319 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
2320 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
2321 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
2322
2323 // if CurrBlock -> *SI is *not* a back edge
2324 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
2325 continue;
2326
2327 CFGBlock *FirstLoopBlock = *SI;
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002328 CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
2329 CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
2330 intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
2331 PreLoop->EntryLoc,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002332 LEK_LockedSomeLoopIterations,
2333 false);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002334 }
2335 }
2336
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002337 CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
2338 CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()];
Caitlin Sadowski1748b122011-09-16 00:35:54 +00002339
2340 // FIXME: Should we call this function for all blocks which exit the function?
DeLesley Hutchins0da44142012-06-22 17:07:28 +00002341 intersectAndWarn(Initial->EntrySet, Final->ExitSet,
2342 Final->ExitLoc,
DeLesley Hutchins879a4332012-07-02 22:16:54 +00002343 LEK_LockedAtEndOfFunction,
DeLesley Hutchinsa1fa4712012-08-10 18:39:05 +00002344 LEK_NotLockedAtEndOfFunction,
2345 false);
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002346}
2347
2348} // end anonymous namespace
2349
2350
2351namespace clang {
2352namespace thread_safety {
2353
2354/// \brief Check a function's CFG for thread-safety violations.
2355///
2356/// We traverse the blocks in the CFG, compute the set of mutexes that are held
2357/// at the end of each block, and issue warnings for thread safety violations.
2358/// Each block in the CFG is traversed exactly once.
Ted Kremenek1d26f482011-10-24 01:32:45 +00002359void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002360 ThreadSafetyHandler &Handler) {
2361 ThreadSafetyAnalyzer Analyzer(Handler);
2362 Analyzer.runAnalysis(AC);
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002363}
2364
2365/// \brief Helper function that returns a LockKind required for the given level
2366/// of access.
2367LockKind getLockKindFromAccessKind(AccessKind AK) {
2368 switch (AK) {
2369 case AK_Read :
2370 return LK_Shared;
2371 case AK_Written :
2372 return LK_Exclusive;
2373 }
Benjamin Kramerafc5b152011-09-10 21:52:04 +00002374 llvm_unreachable("Unknown AccessKind");
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002375}
DeLesley Hutchinsa60448d2011-10-21 16:14:33 +00002376
Caitlin Sadowski402aa062011-09-09 16:11:56 +00002377}} // end namespace clang::thread_safety