]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Analysis/ThreadSafety.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Analysis / ThreadSafety.cpp
1 //===- 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 //
13 // See http://clang.llvm.org/docs/LanguageExtensions.html#threadsafety for more
14 // information.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "clang/Analysis/Analyses/ThreadSafety.h"
19 #include "clang/Analysis/Analyses/PostOrderCFGView.h"
20 #include "clang/Analysis/AnalysisContext.h"
21 #include "clang/Analysis/CFG.h"
22 #include "clang/Analysis/CFGStmtMap.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/StmtCXX.h"
26 #include "clang/AST/StmtVisitor.h"
27 #include "clang/Basic/SourceManager.h"
28 #include "clang/Basic/SourceLocation.h"
29 #include "clang/Basic/OperatorKinds.h"
30 #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"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm>
38 #include <utility>
39 #include <vector>
40
41 using namespace clang;
42 using namespace thread_safety;
43
44 // Key method definition
45 ThreadSafetyHandler::~ThreadSafetyHandler() {}
46
47 namespace {
48
49 /// 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
58 ///
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.
67 ///
68 /// The current implementation assumes, but does not verify, that multiple uses
69 /// of the same lock expression satisfies these criteria.
70 class SExpr {
71 private:
72   enum ExprOp {
73     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
86   };
87
88
89   class SExprNode {
90    private:
91     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
95
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) {
122         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;
135       }
136       return 0;
137     }
138
139     bool operator==(const SExprNode& Other) const {
140       // Ignore flags and size -- they don't matter.
141       return (Op == Other.Op &&
142               Data == Other.Data);
143     }
144
145     bool operator!=(const SExprNode& Other) const {
146       return !(*this == Other);
147     }
148
149     bool matches(const SExprNode& Other) const {
150       return (*this == Other) ||
151              (Op == EOP_Wildcard) ||
152              (Other.Op == EOP_Wildcard);
153     }
154   };
155
156
157   /// \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 {
167     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.
173
174     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)
178     { }
179   };
180
181   typedef SmallVector<SExprNode, 4> NodeVector;
182
183 private:
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
188 private:
189   unsigned makeNop() {
190     NodeVec.push_back(SExprNode(EOP_Nop, 0, 0));
191     return NodeVec.size()-1;
192   }
193
194   unsigned makeWildcard() {
195     NodeVec.push_back(SExprNode(EOP_Wildcard, 0, 0));
196     return NodeVec.size()-1;
197   }
198
199   unsigned makeUniversal() {
200     NodeVec.push_back(SExprNode(EOP_Universal, 0, 0));
201     return NodeVec.size()-1;
202   }
203
204   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   // Grab the very first declaration of virtual method D
230   const CXXMethodDecl* getFirstVirtualDecl(const CXXMethodDecl *D) {
231     while (true) {
232       D = D->getCanonicalDecl();
233       CXXMethodDecl::method_iterator I = D->begin_overridden_methods(),
234                                      E = D->end_overridden_methods();
235       if (I == E)
236         return D;  // Method does not override anything
237       D = *I;      // FIXME: this does not work with multiple inheritance.
238     }
239     return 0;
240   }
241
242   unsigned makeMCall(unsigned NumArgs, const CXXMethodDecl *D) {
243     NodeVec.push_back(SExprNode(EOP_MCall, NumArgs, getFirstVirtualDecl(D)));
244     return NodeVec.size()-1;
245   }
246
247   unsigned makeIndex() {
248     NodeVec.push_back(SExprNode(EOP_Index, 0, 0));
249     return NodeVec.size()-1;
250   }
251
252   unsigned makeUnary() {
253     NodeVec.push_back(SExprNode(EOP_Unary, 0, 0));
254     return NodeVec.size()-1;
255   }
256
257   unsigned makeBinary() {
258     NodeVec.push_back(SExprNode(EOP_Binary, 0, 0));
259     return NodeVec.size()-1;
260   }
261
262   unsigned makeUnknown(unsigned Arity) {
263     NodeVec.push_back(SExprNode(EOP_Unknown, Arity, 0));
264     return NodeVec.size()-1;
265   }
266
267   /// Build an SExpr from the given C++ expression.
268   /// Recursive function that terminates on DeclRefExpr.
269   /// Note: this function merely creates a SExpr; it does not check to
270   /// ensure that the original expression is a valid mutex expression.
271   ///
272   /// NDeref returns the number of Derefence and AddressOf operations
273   /// preceeding the Expr; this is used to decide whether to pretty-print
274   /// SExprs with . or ->.
275   unsigned buildSExpr(Expr *Exp, CallingContext* CallCtx, int* NDeref = 0) {
276     if (!Exp)
277       return 0;
278
279     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) {
280       NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl());
281       ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND);
282       if (PV) {
283         FunctionDecl *FD =
284           cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
285         unsigned i = PV->getFunctionScopeIndex();
286
287         if (CallCtx && CallCtx->FunArgs &&
288             FD == CallCtx->AttrDecl->getCanonicalDecl()) {
289           // Substitute call arguments for references to function parameters
290           assert(i < CallCtx->NumArgs);
291           return buildSExpr(CallCtx->FunArgs[i], CallCtx->PrevCtx, NDeref);
292         }
293         // Map the param back to the param of the original function declaration.
294         makeNamedVar(FD->getParamDecl(i));
295         return 1;
296       }
297       // Not a function parameter -- just store the reference.
298       makeNamedVar(ND);
299       return 1;
300     } else if (isa<CXXThisExpr>(Exp)) {
301       // Substitute parent for 'this'
302       if (CallCtx && CallCtx->SelfArg) {
303         if (!CallCtx->SelfArrow && NDeref)
304           // 'this' is a pointer, but self is not, so need to take address.
305           --(*NDeref);
306         return buildSExpr(CallCtx->SelfArg, CallCtx->PrevCtx, NDeref);
307       }
308       else {
309         makeThis();
310         return 1;
311       }
312     } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
313       NamedDecl *ND = ME->getMemberDecl();
314       int ImplicitDeref = ME->isArrow() ? 1 : 0;
315       unsigned Root = makeDot(ND, false);
316       unsigned Sz = buildSExpr(ME->getBase(), CallCtx, &ImplicitDeref);
317       NodeVec[Root].setArrow(ImplicitDeref > 0);
318       NodeVec[Root].setSize(Sz + 1);
319       return Sz + 1;
320     } else if (CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) {
321       // When calling a function with a lock_returned attribute, replace
322       // the function call with the expression in lock_returned.
323       CXXMethodDecl* MD =
324         cast<CXXMethodDecl>(CMCE->getMethodDecl()->getMostRecentDecl());
325       if (LockReturnedAttr* At = MD->getAttr<LockReturnedAttr>()) {
326         CallingContext LRCallCtx(CMCE->getMethodDecl());
327         LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument();
328         LRCallCtx.SelfArrow =
329           dyn_cast<MemberExpr>(CMCE->getCallee())->isArrow();
330         LRCallCtx.NumArgs = CMCE->getNumArgs();
331         LRCallCtx.FunArgs = CMCE->getArgs();
332         LRCallCtx.PrevCtx = CallCtx;
333         return buildSExpr(At->getArg(), &LRCallCtx);
334       }
335       // Hack to treat smart pointers and iterators as pointers;
336       // ignore any method named get().
337       if (CMCE->getMethodDecl()->getNameAsString() == "get" &&
338           CMCE->getNumArgs() == 0) {
339         if (NDeref && dyn_cast<MemberExpr>(CMCE->getCallee())->isArrow())
340           ++(*NDeref);
341         return buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx, NDeref);
342       }
343       unsigned NumCallArgs = CMCE->getNumArgs();
344       unsigned Root = makeMCall(NumCallArgs, CMCE->getMethodDecl());
345       unsigned Sz = buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx);
346       Expr** CallArgs = CMCE->getArgs();
347       for (unsigned i = 0; i < NumCallArgs; ++i) {
348         Sz += buildSExpr(CallArgs[i], CallCtx);
349       }
350       NodeVec[Root].setSize(Sz + 1);
351       return Sz + 1;
352     } else if (CallExpr *CE = dyn_cast<CallExpr>(Exp)) {
353       FunctionDecl* FD =
354         cast<FunctionDecl>(CE->getDirectCallee()->getMostRecentDecl());
355       if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) {
356         CallingContext LRCallCtx(CE->getDirectCallee());
357         LRCallCtx.NumArgs = CE->getNumArgs();
358         LRCallCtx.FunArgs = CE->getArgs();
359         LRCallCtx.PrevCtx = CallCtx;
360         return buildSExpr(At->getArg(), &LRCallCtx);
361       }
362       // Treat smart pointers and iterators as pointers;
363       // ignore the * and -> operators.
364       if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(CE)) {
365         OverloadedOperatorKind k = OE->getOperator();
366         if (k == OO_Star) {
367           if (NDeref) ++(*NDeref);
368           return buildSExpr(OE->getArg(0), CallCtx, NDeref);
369         }
370         else if (k == OO_Arrow) {
371           return buildSExpr(OE->getArg(0), CallCtx, NDeref);
372         }
373       }
374       unsigned NumCallArgs = CE->getNumArgs();
375       unsigned Root = makeCall(NumCallArgs, 0);
376       unsigned Sz = buildSExpr(CE->getCallee(), CallCtx);
377       Expr** CallArgs = CE->getArgs();
378       for (unsigned i = 0; i < NumCallArgs; ++i) {
379         Sz += buildSExpr(CallArgs[i], CallCtx);
380       }
381       NodeVec[Root].setSize(Sz+1);
382       return Sz+1;
383     } else if (BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) {
384       unsigned Root = makeBinary();
385       unsigned Sz = buildSExpr(BOE->getLHS(), CallCtx);
386       Sz += buildSExpr(BOE->getRHS(), CallCtx);
387       NodeVec[Root].setSize(Sz);
388       return Sz;
389     } else if (UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) {
390       // Ignore & and * operators -- they're no-ops.
391       // However, we try to figure out whether the expression is a pointer,
392       // so we can use . and -> appropriately in error messages.
393       if (UOE->getOpcode() == UO_Deref) {
394         if (NDeref) ++(*NDeref);
395         return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
396       }
397       if (UOE->getOpcode() == UO_AddrOf) {
398         if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UOE->getSubExpr())) {
399           if (DRE->getDecl()->isCXXInstanceMember()) {
400             // This is a pointer-to-member expression, e.g. &MyClass::mu_.
401             // We interpret this syntax specially, as a wildcard.
402             unsigned Root = makeDot(DRE->getDecl(), false);
403             makeWildcard();
404             NodeVec[Root].setSize(2);
405             return 2;
406           }
407         }
408         if (NDeref) --(*NDeref);
409         return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
410       }
411       unsigned Root = makeUnary();
412       unsigned Sz = buildSExpr(UOE->getSubExpr(), CallCtx);
413       NodeVec[Root].setSize(Sz);
414       return Sz;
415     } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(Exp)) {
416       unsigned Root = makeIndex();
417       unsigned Sz = buildSExpr(ASE->getBase(), CallCtx);
418       Sz += buildSExpr(ASE->getIdx(), CallCtx);
419       NodeVec[Root].setSize(Sz);
420       return Sz;
421     } else if (AbstractConditionalOperator *CE =
422                dyn_cast<AbstractConditionalOperator>(Exp)) {
423       unsigned Root = makeUnknown(3);
424       unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
425       Sz += buildSExpr(CE->getTrueExpr(), CallCtx);
426       Sz += buildSExpr(CE->getFalseExpr(), CallCtx);
427       NodeVec[Root].setSize(Sz);
428       return Sz;
429     } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) {
430       unsigned Root = makeUnknown(3);
431       unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
432       Sz += buildSExpr(CE->getLHS(), CallCtx);
433       Sz += buildSExpr(CE->getRHS(), CallCtx);
434       NodeVec[Root].setSize(Sz);
435       return Sz;
436     } else if (CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
437       return buildSExpr(CE->getSubExpr(), CallCtx, NDeref);
438     } else if (ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
439       return buildSExpr(PE->getSubExpr(), CallCtx, NDeref);
440     } else if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Exp)) {
441       return buildSExpr(EWC->getSubExpr(), CallCtx, NDeref);
442     } else if (CXXBindTemporaryExpr *E = dyn_cast<CXXBindTemporaryExpr>(Exp)) {
443       return buildSExpr(E->getSubExpr(), CallCtx, NDeref);
444     } else if (isa<CharacterLiteral>(Exp) ||
445                isa<CXXNullPtrLiteralExpr>(Exp) ||
446                isa<GNUNullExpr>(Exp) ||
447                isa<CXXBoolLiteralExpr>(Exp) ||
448                isa<FloatingLiteral>(Exp) ||
449                isa<ImaginaryLiteral>(Exp) ||
450                isa<IntegerLiteral>(Exp) ||
451                isa<StringLiteral>(Exp) ||
452                isa<ObjCStringLiteral>(Exp)) {
453       makeNop();
454       return 1;  // FIXME: Ignore literals for now
455     } else {
456       makeNop();
457       return 1;  // Ignore.  FIXME: mark as invalid expression?
458     }
459   }
460
461   /// \brief Construct a SExpr from an expression.
462   /// \param MutexExp The original mutex expression within an attribute
463   /// \param DeclExp An expression involving the Decl on which the attribute
464   ///        occurs.
465   /// \param D  The declaration to which the lock/unlock attribute is attached.
466   void buildSExprFromExpr(Expr *MutexExp, Expr *DeclExp, const NamedDecl *D,
467                           VarDecl *SelfDecl = 0) {
468     CallingContext CallCtx(D);
469
470     if (MutexExp) {
471       if (StringLiteral* SLit = dyn_cast<StringLiteral>(MutexExp)) {
472         if (SLit->getString() == StringRef("*"))
473           // The "*" expr is a universal lock, which essentially turns off
474           // checks until it is removed from the lockset.
475           makeUniversal();
476         else
477           // Ignore other string literals for now.
478           makeNop();
479         return;
480       }
481     }
482
483     // If we are processing a raw attribute expression, with no substitutions.
484     if (DeclExp == 0) {
485       buildSExpr(MutexExp, 0);
486       return;
487     }
488
489     // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
490     // for formal parameters when we call buildMutexID later.
491     if (MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
492       CallCtx.SelfArg   = ME->getBase();
493       CallCtx.SelfArrow = ME->isArrow();
494     } else if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(DeclExp)) {
495       CallCtx.SelfArg   = CE->getImplicitObjectArgument();
496       CallCtx.SelfArrow = dyn_cast<MemberExpr>(CE->getCallee())->isArrow();
497       CallCtx.NumArgs   = CE->getNumArgs();
498       CallCtx.FunArgs   = CE->getArgs();
499     } else if (CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
500       CallCtx.NumArgs = CE->getNumArgs();
501       CallCtx.FunArgs = CE->getArgs();
502     } else if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(DeclExp)) {
503       CallCtx.SelfArg = 0;  // Will be set below
504       CallCtx.NumArgs = CE->getNumArgs();
505       CallCtx.FunArgs = CE->getArgs();
506     } else if (D && isa<CXXDestructorDecl>(D)) {
507       // There's no such thing as a "destructor call" in the AST.
508       CallCtx.SelfArg = DeclExp;
509     }
510
511     // Hack to handle constructors, where self cannot be recovered from
512     // the expression.
513     if (SelfDecl && !CallCtx.SelfArg) {
514       DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue,
515                           SelfDecl->getLocation());
516       CallCtx.SelfArg = &SelfDRE;
517
518       // If the attribute has no arguments, then assume the argument is "this".
519       if (MutexExp == 0)
520         buildSExpr(CallCtx.SelfArg, 0);
521       else  // For most attributes.
522         buildSExpr(MutexExp, &CallCtx);
523       return;
524     }
525
526     // If the attribute has no arguments, then assume the argument is "this".
527     if (MutexExp == 0)
528       buildSExpr(CallCtx.SelfArg, 0);
529     else  // For most attributes.
530       buildSExpr(MutexExp, &CallCtx);
531   }
532
533   /// \brief Get index of next sibling of node i.
534   unsigned getNextSibling(unsigned i) const {
535     return i + NodeVec[i].size();
536   }
537
538 public:
539   explicit SExpr(clang::Decl::EmptyShell e) { NodeVec.clear(); }
540
541   /// \param MutexExp The original mutex expression within an attribute
542   /// \param DeclExp An expression involving the Decl on which the attribute
543   ///        occurs.
544   /// \param D  The declaration to which the lock/unlock attribute is attached.
545   /// Caller must check isValid() after construction.
546   SExpr(Expr* MutexExp, Expr *DeclExp, const NamedDecl* D,
547         VarDecl *SelfDecl=0) {
548     buildSExprFromExpr(MutexExp, DeclExp, D, SelfDecl);
549   }
550
551   /// Return true if this is a valid decl sequence.
552   /// Caller must call this by hand after construction to handle errors.
553   bool isValid() const {
554     return !NodeVec.empty();
555   }
556
557   bool shouldIgnore() const {
558     // Nop is a mutex that we have decided to deliberately ignore.
559     assert(NodeVec.size() > 0 && "Invalid Mutex");
560     return NodeVec[0].kind() == EOP_Nop;
561   }
562
563   bool isUniversal() const {
564     assert(NodeVec.size() > 0 && "Invalid Mutex");
565     return NodeVec[0].kind() == EOP_Universal;
566   }
567
568   /// Issue a warning about an invalid lock expression
569   static void warnInvalidLock(ThreadSafetyHandler &Handler, Expr* MutexExp,
570                               Expr *DeclExp, const NamedDecl* D) {
571     SourceLocation Loc;
572     if (DeclExp)
573       Loc = DeclExp->getExprLoc();
574
575     // FIXME: add a note about the attribute location in MutexExp or D
576     if (Loc.isValid())
577       Handler.handleInvalidLockExp(Loc);
578   }
579
580   bool operator==(const SExpr &other) const {
581     return NodeVec == other.NodeVec;
582   }
583
584   bool operator!=(const SExpr &other) const {
585     return !(*this == other);
586   }
587
588   bool matches(const SExpr &Other, unsigned i = 0, unsigned j = 0) const {
589     if (NodeVec[i].matches(Other.NodeVec[j])) {
590       unsigned ni = NodeVec[i].arity();
591       unsigned nj = Other.NodeVec[j].arity();
592       unsigned n = (ni < nj) ? ni : nj;
593       bool Result = true;
594       unsigned ci = i+1;  // first child of i
595       unsigned cj = j+1;  // first child of j
596       for (unsigned k = 0; k < n;
597            ++k, ci=getNextSibling(ci), cj = Other.getNextSibling(cj)) {
598         Result = Result && matches(Other, ci, cj);
599       }
600       return Result;
601     }
602     return false;
603   }
604
605   // A partial match between a.mu and b.mu returns true a and b have the same
606   // type (and thus mu refers to the same mutex declaration), regardless of
607   // whether a and b are different objects or not.
608   bool partiallyMatches(const SExpr &Other) const {
609     if (NodeVec[0].kind() == EOP_Dot)
610       return NodeVec[0].matches(Other.NodeVec[0]);
611     return false;
612   }
613
614   /// \brief Pretty print a lock expression for use in error messages.
615   std::string toString(unsigned i = 0) const {
616     assert(isValid());
617     if (i >= NodeVec.size())
618       return "";
619
620     const SExprNode* N = &NodeVec[i];
621     switch (N->kind()) {
622       case EOP_Nop:
623         return "_";
624       case EOP_Wildcard:
625         return "(?)";
626       case EOP_Universal:
627         return "*";
628       case EOP_This:
629         return "this";
630       case EOP_NVar:
631       case EOP_LVar: {
632         return N->getNamedDecl()->getNameAsString();
633       }
634       case EOP_Dot: {
635         if (NodeVec[i+1].kind() == EOP_Wildcard) {
636           std::string S = "&";
637           S += N->getNamedDecl()->getQualifiedNameAsString();
638           return S;
639         }
640         std::string FieldName = N->getNamedDecl()->getNameAsString();
641         if (NodeVec[i+1].kind() == EOP_This)
642           return FieldName;
643
644         std::string S = toString(i+1);
645         if (N->isArrow())
646           return S + "->" + FieldName;
647         else
648           return S + "." + FieldName;
649       }
650       case EOP_Call: {
651         std::string S = toString(i+1) + "(";
652         unsigned NumArgs = N->arity()-1;
653         unsigned ci = getNextSibling(i+1);
654         for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
655           S += toString(ci);
656           if (k+1 < NumArgs) S += ",";
657         }
658         S += ")";
659         return S;
660       }
661       case EOP_MCall: {
662         std::string S = "";
663         if (NodeVec[i+1].kind() != EOP_This)
664           S = toString(i+1) + ".";
665         if (const NamedDecl *D = N->getFunctionDecl())
666           S += D->getNameAsString() + "(";
667         else
668           S += "#(";
669         unsigned NumArgs = N->arity()-1;
670         unsigned ci = getNextSibling(i+1);
671         for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
672           S += toString(ci);
673           if (k+1 < NumArgs) S += ",";
674         }
675         S += ")";
676         return S;
677       }
678       case EOP_Index: {
679         std::string S1 = toString(i+1);
680         std::string S2 = toString(i+1 + NodeVec[i+1].size());
681         return S1 + "[" + S2 + "]";
682       }
683       case EOP_Unary: {
684         std::string S = toString(i+1);
685         return "#" + S;
686       }
687       case EOP_Binary: {
688         std::string S1 = toString(i+1);
689         std::string S2 = toString(i+1 + NodeVec[i+1].size());
690         return "(" + S1 + "#" + S2 + ")";
691       }
692       case EOP_Unknown: {
693         unsigned NumChildren = N->arity();
694         if (NumChildren == 0)
695           return "(...)";
696         std::string S = "(";
697         unsigned ci = i+1;
698         for (unsigned j = 0; j < NumChildren; ++j, ci = getNextSibling(ci)) {
699           S += toString(ci);
700           if (j+1 < NumChildren) S += "#";
701         }
702         S += ")";
703         return S;
704       }
705     }
706     return "";
707   }
708 };
709
710
711
712 /// \brief A short list of SExprs
713 class MutexIDList : public SmallVector<SExpr, 3> {
714 public:
715   /// \brief Return true if the list contains the specified SExpr
716   /// Performs a linear search, because these lists are almost always very small.
717   bool contains(const SExpr& M) {
718     for (iterator I=begin(),E=end(); I != E; ++I)
719       if ((*I) == M) return true;
720     return false;
721   }
722
723   /// \brief Push M onto list, bud discard duplicates
724   void push_back_nodup(const SExpr& M) {
725     if (!contains(M)) push_back(M);
726   }
727 };
728
729
730
731 /// \brief This is a helper class that stores info about the most recent
732 /// accquire of a Lock.
733 ///
734 /// The main body of the analysis maps MutexIDs to LockDatas.
735 struct LockData {
736   SourceLocation AcquireLoc;
737
738   /// \brief LKind stores whether a lock is held shared or exclusively.
739   /// Note that this analysis does not currently support either re-entrant
740   /// locking or lock "upgrading" and "downgrading" between exclusive and
741   /// shared.
742   ///
743   /// FIXME: add support for re-entrant locking and lock up/downgrading
744   LockKind LKind;
745   bool     Managed;            // for ScopedLockable objects
746   SExpr    UnderlyingMutex;    // for ScopedLockable objects
747
748   LockData(SourceLocation AcquireLoc, LockKind LKind, bool M = false)
749     : AcquireLoc(AcquireLoc), LKind(LKind), Managed(M),
750       UnderlyingMutex(Decl::EmptyShell())
751   {}
752
753   LockData(SourceLocation AcquireLoc, LockKind LKind, const SExpr &Mu)
754     : AcquireLoc(AcquireLoc), LKind(LKind), Managed(false),
755       UnderlyingMutex(Mu)
756   {}
757
758   bool operator==(const LockData &other) const {
759     return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
760   }
761
762   bool operator!=(const LockData &other) const {
763     return !(*this == other);
764   }
765
766   void Profile(llvm::FoldingSetNodeID &ID) const {
767     ID.AddInteger(AcquireLoc.getRawEncoding());
768     ID.AddInteger(LKind);
769   }
770
771   bool isAtLeast(LockKind LK) {
772     return (LK == LK_Shared) || (LKind == LK_Exclusive);
773   }
774 };
775
776
777 /// \brief A FactEntry stores a single fact that is known at a particular point
778 /// in the program execution.  Currently, this is information regarding a lock
779 /// that is held at that point.  
780 struct FactEntry {
781   SExpr    MutID;
782   LockData LDat;
783
784   FactEntry(const SExpr& M, const LockData& L)
785     : MutID(M), LDat(L)
786   { }
787 };
788
789
790 typedef unsigned short FactID;
791
792 /// \brief FactManager manages the memory for all facts that are created during 
793 /// the analysis of a single routine.
794 class FactManager {
795 private:
796   std::vector<FactEntry> Facts;
797
798 public:
799   FactID newLock(const SExpr& M, const LockData& L) {
800     Facts.push_back(FactEntry(M,L));
801     return static_cast<unsigned short>(Facts.size() - 1);
802   }
803
804   const FactEntry& operator[](FactID F) const { return Facts[F]; }
805   FactEntry&       operator[](FactID F)       { return Facts[F]; }
806 };
807
808
809 /// \brief A FactSet is the set of facts that are known to be true at a
810 /// particular program point.  FactSets must be small, because they are 
811 /// frequently copied, and are thus implemented as a set of indices into a
812 /// table maintained by a FactManager.  A typical FactSet only holds 1 or 2 
813 /// locks, so we can get away with doing a linear search for lookup.  Note
814 /// that a hashtable or map is inappropriate in this case, because lookups
815 /// may involve partial pattern matches, rather than exact matches.
816 class FactSet {
817 private:
818   typedef SmallVector<FactID, 4> FactVec;
819
820   FactVec FactIDs;
821
822 public:
823   typedef FactVec::iterator       iterator;
824   typedef FactVec::const_iterator const_iterator;
825
826   iterator       begin()       { return FactIDs.begin(); }
827   const_iterator begin() const { return FactIDs.begin(); }
828
829   iterator       end()       { return FactIDs.end(); }
830   const_iterator end() const { return FactIDs.end(); }
831
832   bool isEmpty() const { return FactIDs.size() == 0; }
833
834   FactID addLock(FactManager& FM, const SExpr& M, const LockData& L) {
835     FactID F = FM.newLock(M, L);
836     FactIDs.push_back(F);
837     return F;
838   }
839
840   bool removeLock(FactManager& FM, const SExpr& M) {
841     unsigned n = FactIDs.size();
842     if (n == 0)
843       return false;
844
845     for (unsigned i = 0; i < n-1; ++i) {
846       if (FM[FactIDs[i]].MutID.matches(M)) {
847         FactIDs[i] = FactIDs[n-1];
848         FactIDs.pop_back();
849         return true;
850       }
851     }
852     if (FM[FactIDs[n-1]].MutID.matches(M)) {
853       FactIDs.pop_back();
854       return true;
855     }
856     return false;
857   }
858
859   LockData* findLock(FactManager &FM, const SExpr &M) const {
860     for (const_iterator I = begin(), E = end(); I != E; ++I) {
861       const SExpr &Exp = FM[*I].MutID;
862       if (Exp.matches(M))
863         return &FM[*I].LDat;
864     }
865     return 0;
866   }
867
868   LockData* findLockUniv(FactManager &FM, const SExpr &M) const {
869     for (const_iterator I = begin(), E = end(); I != E; ++I) {
870       const SExpr &Exp = FM[*I].MutID;
871       if (Exp.matches(M) || Exp.isUniversal())
872         return &FM[*I].LDat;
873     }
874     return 0;
875   }
876
877   FactEntry* findPartialMatch(FactManager &FM, const SExpr &M) const {
878     for (const_iterator I=begin(), E=end(); I != E; ++I) {
879       const SExpr& Exp = FM[*I].MutID;
880       if (Exp.partiallyMatches(M)) return &FM[*I];
881     }
882     return 0;
883   }
884 };
885
886
887
888 /// A Lockset maps each SExpr (defined above) to information about how it has
889 /// been locked.
890 typedef llvm::ImmutableMap<SExpr, LockData> Lockset;
891 typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
892
893 class LocalVariableMap;
894
895 /// A side (entry or exit) of a CFG node.
896 enum CFGBlockSide { CBS_Entry, CBS_Exit };
897
898 /// CFGBlockInfo is a struct which contains all the information that is
899 /// maintained for each block in the CFG.  See LocalVariableMap for more
900 /// information about the contexts.
901 struct CFGBlockInfo {
902   FactSet EntrySet;             // Lockset held at entry to block
903   FactSet ExitSet;              // Lockset held at exit from block
904   LocalVarContext EntryContext; // Context held at entry to block
905   LocalVarContext ExitContext;  // Context held at exit from block
906   SourceLocation EntryLoc;      // Location of first statement in block
907   SourceLocation ExitLoc;       // Location of last statement in block.
908   unsigned EntryIndex;          // Used to replay contexts later
909   bool Reachable;               // Is this block reachable?
910
911   const FactSet &getSet(CFGBlockSide Side) const {
912     return Side == CBS_Entry ? EntrySet : ExitSet;
913   }
914   SourceLocation getLocation(CFGBlockSide Side) const {
915     return Side == CBS_Entry ? EntryLoc : ExitLoc;
916   }
917
918 private:
919   CFGBlockInfo(LocalVarContext EmptyCtx)
920     : EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false)
921   { }
922
923 public:
924   static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M);
925 };
926
927
928
929 // A LocalVariableMap maintains a map from local variables to their currently
930 // valid definitions.  It provides SSA-like functionality when traversing the
931 // CFG.  Like SSA, each definition or assignment to a variable is assigned a
932 // unique name (an integer), which acts as the SSA name for that definition.
933 // The total set of names is shared among all CFG basic blocks.
934 // Unlike SSA, we do not rewrite expressions to replace local variables declrefs
935 // with their SSA-names.  Instead, we compute a Context for each point in the
936 // code, which maps local variables to the appropriate SSA-name.  This map
937 // changes with each assignment.
938 //
939 // The map is computed in a single pass over the CFG.  Subsequent analyses can
940 // then query the map to find the appropriate Context for a statement, and use
941 // that Context to look up the definitions of variables.
942 class LocalVariableMap {
943 public:
944   typedef LocalVarContext Context;
945
946   /// A VarDefinition consists of an expression, representing the value of the
947   /// variable, along with the context in which that expression should be
948   /// interpreted.  A reference VarDefinition does not itself contain this
949   /// information, but instead contains a pointer to a previous VarDefinition.
950   struct VarDefinition {
951   public:
952     friend class LocalVariableMap;
953
954     const NamedDecl *Dec;  // The original declaration for this variable.
955     const Expr *Exp;       // The expression for this variable, OR
956     unsigned Ref;          // Reference to another VarDefinition
957     Context Ctx;           // The map with which Exp should be interpreted.
958
959     bool isReference() { return !Exp; }
960
961   private:
962     // Create ordinary variable definition
963     VarDefinition(const NamedDecl *D, const Expr *E, Context C)
964       : Dec(D), Exp(E), Ref(0), Ctx(C)
965     { }
966
967     // Create reference to previous definition
968     VarDefinition(const NamedDecl *D, unsigned R, Context C)
969       : Dec(D), Exp(0), Ref(R), Ctx(C)
970     { }
971   };
972
973 private:
974   Context::Factory ContextFactory;
975   std::vector<VarDefinition> VarDefinitions;
976   std::vector<unsigned> CtxIndices;
977   std::vector<std::pair<Stmt*, Context> > SavedContexts;
978
979 public:
980   LocalVariableMap() {
981     // index 0 is a placeholder for undefined variables (aka phi-nodes).
982     VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext()));
983   }
984
985   /// Look up a definition, within the given context.
986   const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
987     const unsigned *i = Ctx.lookup(D);
988     if (!i)
989       return 0;
990     assert(*i < VarDefinitions.size());
991     return &VarDefinitions[*i];
992   }
993
994   /// Look up the definition for D within the given context.  Returns
995   /// NULL if the expression is not statically known.  If successful, also
996   /// modifies Ctx to hold the context of the return Expr.
997   const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
998     const unsigned *P = Ctx.lookup(D);
999     if (!P)
1000       return 0;
1001
1002     unsigned i = *P;
1003     while (i > 0) {
1004       if (VarDefinitions[i].Exp) {
1005         Ctx = VarDefinitions[i].Ctx;
1006         return VarDefinitions[i].Exp;
1007       }
1008       i = VarDefinitions[i].Ref;
1009     }
1010     return 0;
1011   }
1012
1013   Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
1014
1015   /// Return the next context after processing S.  This function is used by
1016   /// clients of the class to get the appropriate context when traversing the
1017   /// CFG.  It must be called for every assignment or DeclStmt.
1018   Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
1019     if (SavedContexts[CtxIndex+1].first == S) {
1020       CtxIndex++;
1021       Context Result = SavedContexts[CtxIndex].second;
1022       return Result;
1023     }
1024     return C;
1025   }
1026
1027   void dumpVarDefinitionName(unsigned i) {
1028     if (i == 0) {
1029       llvm::errs() << "Undefined";
1030       return;
1031     }
1032     const NamedDecl *Dec = VarDefinitions[i].Dec;
1033     if (!Dec) {
1034       llvm::errs() << "<<NULL>>";
1035       return;
1036     }
1037     Dec->printName(llvm::errs());
1038     llvm::errs() << "." << i << " " << ((const void*) Dec);
1039   }
1040
1041   /// Dumps an ASCII representation of the variable map to llvm::errs()
1042   void dump() {
1043     for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
1044       const Expr *Exp = VarDefinitions[i].Exp;
1045       unsigned Ref = VarDefinitions[i].Ref;
1046
1047       dumpVarDefinitionName(i);
1048       llvm::errs() << " = ";
1049       if (Exp) Exp->dump();
1050       else {
1051         dumpVarDefinitionName(Ref);
1052         llvm::errs() << "\n";
1053       }
1054     }
1055   }
1056
1057   /// Dumps an ASCII representation of a Context to llvm::errs()
1058   void dumpContext(Context C) {
1059     for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
1060       const NamedDecl *D = I.getKey();
1061       D->printName(llvm::errs());
1062       const unsigned *i = C.lookup(D);
1063       llvm::errs() << " -> ";
1064       dumpVarDefinitionName(*i);
1065       llvm::errs() << "\n";
1066     }
1067   }
1068
1069   /// Builds the variable map.
1070   void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph,
1071                      std::vector<CFGBlockInfo> &BlockInfo);
1072
1073 protected:
1074   // Get the current context index
1075   unsigned getContextIndex() { return SavedContexts.size()-1; }
1076
1077   // Save the current context for later replay
1078   void saveContext(Stmt *S, Context C) {
1079     SavedContexts.push_back(std::make_pair(S,C));
1080   }
1081
1082   // Adds a new definition to the given context, and returns a new context.
1083   // This method should be called when declaring a new variable.
1084   Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
1085     assert(!Ctx.contains(D));
1086     unsigned newID = VarDefinitions.size();
1087     Context NewCtx = ContextFactory.add(Ctx, D, newID);
1088     VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1089     return NewCtx;
1090   }
1091
1092   // Add a new reference to an existing definition.
1093   Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
1094     unsigned newID = VarDefinitions.size();
1095     Context NewCtx = ContextFactory.add(Ctx, D, newID);
1096     VarDefinitions.push_back(VarDefinition(D, i, Ctx));
1097     return NewCtx;
1098   }
1099
1100   // Updates a definition only if that definition is already in the map.
1101   // This method should be called when assigning to an existing variable.
1102   Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
1103     if (Ctx.contains(D)) {
1104       unsigned newID = VarDefinitions.size();
1105       Context NewCtx = ContextFactory.remove(Ctx, D);
1106       NewCtx = ContextFactory.add(NewCtx, D, newID);
1107       VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1108       return NewCtx;
1109     }
1110     return Ctx;
1111   }
1112
1113   // Removes a definition from the context, but keeps the variable name
1114   // as a valid variable.  The index 0 is a placeholder for cleared definitions.
1115   Context clearDefinition(const NamedDecl *D, Context Ctx) {
1116     Context NewCtx = Ctx;
1117     if (NewCtx.contains(D)) {
1118       NewCtx = ContextFactory.remove(NewCtx, D);
1119       NewCtx = ContextFactory.add(NewCtx, D, 0);
1120     }
1121     return NewCtx;
1122   }
1123
1124   // Remove a definition entirely frmo the context.
1125   Context removeDefinition(const NamedDecl *D, Context Ctx) {
1126     Context NewCtx = Ctx;
1127     if (NewCtx.contains(D)) {
1128       NewCtx = ContextFactory.remove(NewCtx, D);
1129     }
1130     return NewCtx;
1131   }
1132
1133   Context intersectContexts(Context C1, Context C2);
1134   Context createReferenceContext(Context C);
1135   void intersectBackEdge(Context C1, Context C2);
1136
1137   friend class VarMapBuilder;
1138 };
1139
1140
1141 // This has to be defined after LocalVariableMap.
1142 CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) {
1143   return CFGBlockInfo(M.getEmptyContext());
1144 }
1145
1146
1147 /// Visitor which builds a LocalVariableMap
1148 class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
1149 public:
1150   LocalVariableMap* VMap;
1151   LocalVariableMap::Context Ctx;
1152
1153   VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
1154     : VMap(VM), Ctx(C) {}
1155
1156   void VisitDeclStmt(DeclStmt *S);
1157   void VisitBinaryOperator(BinaryOperator *BO);
1158 };
1159
1160
1161 // Add new local variables to the variable map
1162 void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
1163   bool modifiedCtx = false;
1164   DeclGroupRef DGrp = S->getDeclGroup();
1165   for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1166     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
1167       Expr *E = VD->getInit();
1168
1169       // Add local variables with trivial type to the variable map
1170       QualType T = VD->getType();
1171       if (T.isTrivialType(VD->getASTContext())) {
1172         Ctx = VMap->addDefinition(VD, E, Ctx);
1173         modifiedCtx = true;
1174       }
1175     }
1176   }
1177   if (modifiedCtx)
1178     VMap->saveContext(S, Ctx);
1179 }
1180
1181 // Update local variable definitions in variable map
1182 void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
1183   if (!BO->isAssignmentOp())
1184     return;
1185
1186   Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1187
1188   // Update the variable map and current context.
1189   if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
1190     ValueDecl *VDec = DRE->getDecl();
1191     if (Ctx.lookup(VDec)) {
1192       if (BO->getOpcode() == BO_Assign)
1193         Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
1194       else
1195         // FIXME -- handle compound assignment operators
1196         Ctx = VMap->clearDefinition(VDec, Ctx);
1197       VMap->saveContext(BO, Ctx);
1198     }
1199   }
1200 }
1201
1202
1203 // Computes the intersection of two contexts.  The intersection is the
1204 // set of variables which have the same definition in both contexts;
1205 // variables with different definitions are discarded.
1206 LocalVariableMap::Context
1207 LocalVariableMap::intersectContexts(Context C1, Context C2) {
1208   Context Result = C1;
1209   for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
1210     const NamedDecl *Dec = I.getKey();
1211     unsigned i1 = I.getData();
1212     const unsigned *i2 = C2.lookup(Dec);
1213     if (!i2)             // variable doesn't exist on second path
1214       Result = removeDefinition(Dec, Result);
1215     else if (*i2 != i1)  // variable exists, but has different definition
1216       Result = clearDefinition(Dec, Result);
1217   }
1218   return Result;
1219 }
1220
1221 // For every variable in C, create a new variable that refers to the
1222 // definition in C.  Return a new context that contains these new variables.
1223 // (We use this for a naive implementation of SSA on loop back-edges.)
1224 LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
1225   Context Result = getEmptyContext();
1226   for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
1227     const NamedDecl *Dec = I.getKey();
1228     unsigned i = I.getData();
1229     Result = addReference(Dec, i, Result);
1230   }
1231   return Result;
1232 }
1233
1234 // This routine also takes the intersection of C1 and C2, but it does so by
1235 // altering the VarDefinitions.  C1 must be the result of an earlier call to
1236 // createReferenceContext.
1237 void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
1238   for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
1239     const NamedDecl *Dec = I.getKey();
1240     unsigned i1 = I.getData();
1241     VarDefinition *VDef = &VarDefinitions[i1];
1242     assert(VDef->isReference());
1243
1244     const unsigned *i2 = C2.lookup(Dec);
1245     if (!i2 || (*i2 != i1))
1246       VDef->Ref = 0;    // Mark this variable as undefined
1247   }
1248 }
1249
1250
1251 // Traverse the CFG in topological order, so all predecessors of a block
1252 // (excluding back-edges) are visited before the block itself.  At
1253 // each point in the code, we calculate a Context, which holds the set of
1254 // variable definitions which are visible at that point in execution.
1255 // Visible variables are mapped to their definitions using an array that
1256 // contains all definitions.
1257 //
1258 // At join points in the CFG, the set is computed as the intersection of
1259 // the incoming sets along each edge, E.g.
1260 //
1261 //                       { Context                 | VarDefinitions }
1262 //   int x = 0;          { x -> x1                 | x1 = 0 }
1263 //   int y = 0;          { x -> x1, y -> y1        | y1 = 0, x1 = 0 }
1264 //   if (b) x = 1;       { x -> x2, y -> y1        | x2 = 1, y1 = 0, ... }
1265 //   else   x = 2;       { x -> x3, y -> y1        | x3 = 2, x2 = 1, ... }
1266 //   ...                 { y -> y1  (x is unknown) | x3 = 2, x2 = 1, ... }
1267 //
1268 // This is essentially a simpler and more naive version of the standard SSA
1269 // algorithm.  Those definitions that remain in the intersection are from blocks
1270 // that strictly dominate the current block.  We do not bother to insert proper
1271 // phi nodes, because they are not used in our analysis; instead, wherever
1272 // a phi node would be required, we simply remove that definition from the
1273 // context (E.g. x above).
1274 //
1275 // The initial traversal does not capture back-edges, so those need to be
1276 // handled on a separate pass.  Whenever the first pass encounters an
1277 // incoming back edge, it duplicates the context, creating new definitions
1278 // that refer back to the originals.  (These correspond to places where SSA
1279 // might have to insert a phi node.)  On the second pass, these definitions are
1280 // set to NULL if the variable has changed on the back-edge (i.e. a phi
1281 // node was actually required.)  E.g.
1282 //
1283 //                       { Context           | VarDefinitions }
1284 //   int x = 0, y = 0;   { x -> x1, y -> y1  | y1 = 0, x1 = 0 }
1285 //   while (b)           { x -> x2, y -> y1  | [1st:] x2=x1; [2nd:] x2=NULL; }
1286 //     x = x+1;          { x -> x3, y -> y1  | x3 = x2 + 1, ... }
1287 //   ...                 { y -> y1           | x3 = 2, x2 = 1, ... }
1288 //
1289 void LocalVariableMap::traverseCFG(CFG *CFGraph,
1290                                    PostOrderCFGView *SortedGraph,
1291                                    std::vector<CFGBlockInfo> &BlockInfo) {
1292   PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
1293
1294   CtxIndices.resize(CFGraph->getNumBlockIDs());
1295
1296   for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1297        E = SortedGraph->end(); I!= E; ++I) {
1298     const CFGBlock *CurrBlock = *I;
1299     int CurrBlockID = CurrBlock->getBlockID();
1300     CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
1301
1302     VisitedBlocks.insert(CurrBlock);
1303
1304     // Calculate the entry context for the current block
1305     bool HasBackEdges = false;
1306     bool CtxInit = true;
1307     for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
1308          PE  = CurrBlock->pred_end(); PI != PE; ++PI) {
1309       // if *PI -> CurrBlock is a back edge, so skip it
1310       if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) {
1311         HasBackEdges = true;
1312         continue;
1313       }
1314
1315       int PrevBlockID = (*PI)->getBlockID();
1316       CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
1317
1318       if (CtxInit) {
1319         CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
1320         CtxInit = false;
1321       }
1322       else {
1323         CurrBlockInfo->EntryContext =
1324           intersectContexts(CurrBlockInfo->EntryContext,
1325                             PrevBlockInfo->ExitContext);
1326       }
1327     }
1328
1329     // Duplicate the context if we have back-edges, so we can call
1330     // intersectBackEdges later.
1331     if (HasBackEdges)
1332       CurrBlockInfo->EntryContext =
1333         createReferenceContext(CurrBlockInfo->EntryContext);
1334
1335     // Create a starting context index for the current block
1336     saveContext(0, CurrBlockInfo->EntryContext);
1337     CurrBlockInfo->EntryIndex = getContextIndex();
1338
1339     // Visit all the statements in the basic block.
1340     VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
1341     for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1342          BE = CurrBlock->end(); BI != BE; ++BI) {
1343       switch (BI->getKind()) {
1344         case CFGElement::Statement: {
1345           const CFGStmt *CS = cast<CFGStmt>(&*BI);
1346           VMapBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
1347           break;
1348         }
1349         default:
1350           break;
1351       }
1352     }
1353     CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
1354
1355     // Mark variables on back edges as "unknown" if they've been changed.
1356     for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1357          SE  = CurrBlock->succ_end(); SI != SE; ++SI) {
1358       // if CurrBlock -> *SI is *not* a back edge
1359       if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
1360         continue;
1361
1362       CFGBlock *FirstLoopBlock = *SI;
1363       Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
1364       Context LoopEnd   = CurrBlockInfo->ExitContext;
1365       intersectBackEdge(LoopBegin, LoopEnd);
1366     }
1367   }
1368
1369   // Put an extra entry at the end of the indexed context array
1370   unsigned exitID = CFGraph->getExit().getBlockID();
1371   saveContext(0, BlockInfo[exitID].ExitContext);
1372 }
1373
1374 /// Find the appropriate source locations to use when producing diagnostics for
1375 /// each block in the CFG.
1376 static void findBlockLocations(CFG *CFGraph,
1377                                PostOrderCFGView *SortedGraph,
1378                                std::vector<CFGBlockInfo> &BlockInfo) {
1379   for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1380        E = SortedGraph->end(); I!= E; ++I) {
1381     const CFGBlock *CurrBlock = *I;
1382     CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
1383
1384     // Find the source location of the last statement in the block, if the
1385     // block is not empty.
1386     if (const Stmt *S = CurrBlock->getTerminator()) {
1387       CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
1388     } else {
1389       for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
1390            BE = CurrBlock->rend(); BI != BE; ++BI) {
1391         // FIXME: Handle other CFGElement kinds.
1392         if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
1393           CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
1394           break;
1395         }
1396       }
1397     }
1398
1399     if (!CurrBlockInfo->ExitLoc.isInvalid()) {
1400       // This block contains at least one statement. Find the source location
1401       // of the first statement in the block.
1402       for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1403            BE = CurrBlock->end(); BI != BE; ++BI) {
1404         // FIXME: Handle other CFGElement kinds.
1405         if (const CFGStmt *CS = dyn_cast<CFGStmt>(&*BI)) {
1406           CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
1407           break;
1408         }
1409       }
1410     } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
1411                CurrBlock != &CFGraph->getExit()) {
1412       // The block is empty, and has a single predecessor. Use its exit
1413       // location.
1414       CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
1415           BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
1416     }
1417   }
1418 }
1419
1420 /// \brief Class which implements the core thread safety analysis routines.
1421 class ThreadSafetyAnalyzer {
1422   friend class BuildLockset;
1423
1424   ThreadSafetyHandler       &Handler;
1425   LocalVariableMap          LocalVarMap;
1426   FactManager               FactMan;
1427   std::vector<CFGBlockInfo> BlockInfo;
1428
1429 public:
1430   ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
1431
1432   void addLock(FactSet &FSet, const SExpr &Mutex, const LockData &LDat);
1433   void removeLock(FactSet &FSet, const SExpr &Mutex,
1434                   SourceLocation UnlockLoc, bool FullyRemove=false);
1435
1436   template <typename AttrType>
1437   void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1438                    const NamedDecl *D, VarDecl *SelfDecl=0);
1439
1440   template <class AttrType>
1441   void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1442                    const NamedDecl *D,
1443                    const CFGBlock *PredBlock, const CFGBlock *CurrBlock,
1444                    Expr *BrE, bool Neg);
1445
1446   const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
1447                                      bool &Negate);
1448
1449   void getEdgeLockset(FactSet &Result, const FactSet &ExitSet,
1450                       const CFGBlock* PredBlock,
1451                       const CFGBlock *CurrBlock);
1452
1453   void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1454                         SourceLocation JoinLoc,
1455                         LockErrorKind LEK1, LockErrorKind LEK2,
1456                         bool Modify=true);
1457
1458   void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1459                         SourceLocation JoinLoc, LockErrorKind LEK1,
1460                         bool Modify=true) {
1461     intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify);
1462   }
1463
1464   void runAnalysis(AnalysisDeclContext &AC);
1465 };
1466
1467
1468 /// \brief Add a new lock to the lockset, warning if the lock is already there.
1469 /// \param Mutex -- the Mutex expression for the lock
1470 /// \param LDat  -- the LockData for the lock
1471 void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const SExpr &Mutex,
1472                                    const LockData &LDat) {
1473   // FIXME: deal with acquired before/after annotations.
1474   // FIXME: Don't always warn when we have support for reentrant locks.
1475   if (Mutex.shouldIgnore())
1476     return;
1477
1478   if (FSet.findLock(FactMan, Mutex)) {
1479     Handler.handleDoubleLock(Mutex.toString(), LDat.AcquireLoc);
1480   } else {
1481     FSet.addLock(FactMan, Mutex, LDat);
1482   }
1483 }
1484
1485
1486 /// \brief Remove a lock from the lockset, warning if the lock is not there.
1487 /// \param Mutex The lock expression corresponding to the lock to be removed
1488 /// \param UnlockLoc The source location of the unlock (only used in error msg)
1489 void ThreadSafetyAnalyzer::removeLock(FactSet &FSet,
1490                                       const SExpr &Mutex,
1491                                       SourceLocation UnlockLoc,
1492                                       bool FullyRemove) {
1493   if (Mutex.shouldIgnore())
1494     return;
1495
1496   const LockData *LDat = FSet.findLock(FactMan, Mutex);
1497   if (!LDat) {
1498     Handler.handleUnmatchedUnlock(Mutex.toString(), UnlockLoc);
1499     return;
1500   }
1501
1502   if (LDat->UnderlyingMutex.isValid()) {
1503     // This is scoped lockable object, which manages the real mutex.
1504     if (FullyRemove) {
1505       // We're destroying the managing object.
1506       // Remove the underlying mutex if it exists; but don't warn.
1507       if (FSet.findLock(FactMan, LDat->UnderlyingMutex))
1508         FSet.removeLock(FactMan, LDat->UnderlyingMutex);
1509     } else {
1510       // We're releasing the underlying mutex, but not destroying the
1511       // managing object.  Warn on dual release.
1512       if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) {
1513         Handler.handleUnmatchedUnlock(LDat->UnderlyingMutex.toString(),
1514                                       UnlockLoc);
1515       }
1516       FSet.removeLock(FactMan, LDat->UnderlyingMutex);
1517       return;
1518     }
1519   }
1520   FSet.removeLock(FactMan, Mutex);
1521 }
1522
1523
1524 /// \brief Extract the list of mutexIDs from the attribute on an expression,
1525 /// and push them onto Mtxs, discarding any duplicates.
1526 template <typename AttrType>
1527 void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1528                                        Expr *Exp, const NamedDecl *D,
1529                                        VarDecl *SelfDecl) {
1530   typedef typename AttrType::args_iterator iterator_type;
1531
1532   if (Attr->args_size() == 0) {
1533     // The mutex held is the "this" object.
1534     SExpr Mu(0, Exp, D, SelfDecl);
1535     if (!Mu.isValid())
1536       SExpr::warnInvalidLock(Handler, 0, Exp, D);
1537     else
1538       Mtxs.push_back_nodup(Mu);
1539     return;
1540   }
1541
1542   for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
1543     SExpr Mu(*I, Exp, D, SelfDecl);
1544     if (!Mu.isValid())
1545       SExpr::warnInvalidLock(Handler, *I, Exp, D);
1546     else
1547       Mtxs.push_back_nodup(Mu);
1548   }
1549 }
1550
1551
1552 /// \brief Extract the list of mutexIDs from a trylock attribute.  If the
1553 /// trylock applies to the given edge, then push them onto Mtxs, discarding
1554 /// any duplicates.
1555 template <class AttrType>
1556 void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1557                                        Expr *Exp, const NamedDecl *D,
1558                                        const CFGBlock *PredBlock,
1559                                        const CFGBlock *CurrBlock,
1560                                        Expr *BrE, bool Neg) {
1561   // Find out which branch has the lock
1562   bool branch = 0;
1563   if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) {
1564     branch = BLE->getValue();
1565   }
1566   else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) {
1567     branch = ILE->getValue().getBoolValue();
1568   }
1569   int branchnum = branch ? 0 : 1;
1570   if (Neg) branchnum = !branchnum;
1571
1572   // If we've taken the trylock branch, then add the lock
1573   int i = 0;
1574   for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
1575        SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
1576     if (*SI == CurrBlock && i == branchnum) {
1577       getMutexIDs(Mtxs, Attr, Exp, D);
1578     }
1579   }
1580 }
1581
1582
1583 bool getStaticBooleanValue(Expr* E, bool& TCond) {
1584   if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) {
1585     TCond = false;
1586     return true;
1587   } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) {
1588     TCond = BLE->getValue();
1589     return true;
1590   } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) {
1591     TCond = ILE->getValue().getBoolValue();
1592     return true;
1593   } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
1594     return getStaticBooleanValue(CE->getSubExpr(), TCond);
1595   }
1596   return false;
1597 }
1598
1599
1600 // If Cond can be traced back to a function call, return the call expression.
1601 // The negate variable should be called with false, and will be set to true
1602 // if the function call is negated, e.g. if (!mu.tryLock(...))
1603 const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
1604                                                          LocalVarContext C,
1605                                                          bool &Negate) {
1606   if (!Cond)
1607     return 0;
1608
1609   if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
1610     return CallExp;
1611   }
1612   else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) {
1613     return getTrylockCallExpr(PE->getSubExpr(), C, Negate);
1614   }
1615   else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
1616     return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
1617   }
1618   else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) {
1619     return getTrylockCallExpr(EWC->getSubExpr(), C, Negate);
1620   }
1621   else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
1622     const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
1623     return getTrylockCallExpr(E, C, Negate);
1624   }
1625   else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
1626     if (UOP->getOpcode() == UO_LNot) {
1627       Negate = !Negate;
1628       return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
1629     }
1630     return 0;
1631   }
1632   else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) {
1633     if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) {
1634       if (BOP->getOpcode() == BO_NE)
1635         Negate = !Negate;
1636
1637       bool TCond = false;
1638       if (getStaticBooleanValue(BOP->getRHS(), TCond)) {
1639         if (!TCond) Negate = !Negate;
1640         return getTrylockCallExpr(BOP->getLHS(), C, Negate);
1641       }
1642       else if (getStaticBooleanValue(BOP->getLHS(), TCond)) {
1643         if (!TCond) Negate = !Negate;
1644         return getTrylockCallExpr(BOP->getRHS(), C, Negate);
1645       }
1646       return 0;
1647     }
1648     return 0;
1649   }
1650   // FIXME -- handle && and || as well.
1651   return 0;
1652 }
1653
1654
1655 /// \brief Find the lockset that holds on the edge between PredBlock
1656 /// and CurrBlock.  The edge set is the exit set of PredBlock (passed
1657 /// as the ExitSet parameter) plus any trylocks, which are conditionally held.
1658 void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result,
1659                                           const FactSet &ExitSet,
1660                                           const CFGBlock *PredBlock,
1661                                           const CFGBlock *CurrBlock) {
1662   Result = ExitSet;
1663
1664   if (!PredBlock->getTerminatorCondition())
1665     return;
1666
1667   bool Negate = false;
1668   const Stmt *Cond = PredBlock->getTerminatorCondition();
1669   const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
1670   const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
1671
1672   CallExpr *Exp =
1673     const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate));
1674   if (!Exp)
1675     return;
1676
1677   NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1678   if(!FunDecl || !FunDecl->hasAttrs())
1679     return;
1680
1681
1682   MutexIDList ExclusiveLocksToAdd;
1683   MutexIDList SharedLocksToAdd;
1684
1685   // If the condition is a call to a Trylock function, then grab the attributes
1686   AttrVec &ArgAttrs = FunDecl->getAttrs();
1687   for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
1688     Attr *Attr = ArgAttrs[i];
1689     switch (Attr->getKind()) {
1690       case attr::ExclusiveTrylockFunction: {
1691         ExclusiveTrylockFunctionAttr *A =
1692           cast<ExclusiveTrylockFunctionAttr>(Attr);
1693         getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl,
1694                     PredBlock, CurrBlock, A->getSuccessValue(), Negate);
1695         break;
1696       }
1697       case attr::SharedTrylockFunction: {
1698         SharedTrylockFunctionAttr *A =
1699           cast<SharedTrylockFunctionAttr>(Attr);
1700         getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl,
1701                     PredBlock, CurrBlock, A->getSuccessValue(), Negate);
1702         break;
1703       }
1704       default:
1705         break;
1706     }
1707   }
1708
1709   // Add and remove locks.
1710   SourceLocation Loc = Exp->getExprLoc();
1711   for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
1712     addLock(Result, ExclusiveLocksToAdd[i],
1713             LockData(Loc, LK_Exclusive));
1714   }
1715   for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
1716     addLock(Result, SharedLocksToAdd[i],
1717             LockData(Loc, LK_Shared));
1718   }
1719 }
1720
1721
1722 /// \brief We use this class to visit different types of expressions in
1723 /// CFGBlocks, and build up the lockset.
1724 /// An expression may cause us to add or remove locks from the lockset, or else
1725 /// output error messages related to missing locks.
1726 /// FIXME: In future, we may be able to not inherit from a visitor.
1727 class BuildLockset : public StmtVisitor<BuildLockset> {
1728   friend class ThreadSafetyAnalyzer;
1729
1730   ThreadSafetyAnalyzer *Analyzer;
1731   FactSet FSet;
1732   LocalVariableMap::Context LVarCtx;
1733   unsigned CtxIndex;
1734
1735   // Helper functions
1736   const ValueDecl *getValueDecl(Expr *Exp);
1737
1738   void warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp, AccessKind AK,
1739                           Expr *MutexExp, ProtectedOperationKind POK);
1740   void warnIfMutexHeld(const NamedDecl *D, Expr *Exp, Expr *MutexExp);
1741
1742   void checkAccess(Expr *Exp, AccessKind AK);
1743   void checkDereference(Expr *Exp, AccessKind AK);
1744   void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = 0);
1745
1746 public:
1747   BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
1748     : StmtVisitor<BuildLockset>(),
1749       Analyzer(Anlzr),
1750       FSet(Info.EntrySet),
1751       LVarCtx(Info.EntryContext),
1752       CtxIndex(Info.EntryIndex)
1753   {}
1754
1755   void VisitUnaryOperator(UnaryOperator *UO);
1756   void VisitBinaryOperator(BinaryOperator *BO);
1757   void VisitCastExpr(CastExpr *CE);
1758   void VisitCallExpr(CallExpr *Exp);
1759   void VisitCXXConstructExpr(CXXConstructExpr *Exp);
1760   void VisitDeclStmt(DeclStmt *S);
1761 };
1762
1763
1764 /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
1765 const ValueDecl *BuildLockset::getValueDecl(Expr *Exp) {
1766   if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
1767     return DR->getDecl();
1768
1769   if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
1770     return ME->getMemberDecl();
1771
1772   return 0;
1773 }
1774
1775 /// \brief Warn if the LSet does not contain a lock sufficient to protect access
1776 /// of at least the passed in AccessKind.
1777 void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, Expr *Exp,
1778                                       AccessKind AK, Expr *MutexExp,
1779                                       ProtectedOperationKind POK) {
1780   LockKind LK = getLockKindFromAccessKind(AK);
1781
1782   SExpr Mutex(MutexExp, Exp, D);
1783   if (!Mutex.isValid()) {
1784     SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
1785     return;
1786   } else if (Mutex.shouldIgnore()) {
1787     return;
1788   }
1789
1790   LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex);
1791   bool NoError = true;
1792   if (!LDat) {
1793     // No exact match found.  Look for a partial match.
1794     FactEntry* FEntry = FSet.findPartialMatch(Analyzer->FactMan, Mutex);
1795     if (FEntry) {
1796       // Warn that there's no precise match.
1797       LDat = &FEntry->LDat;
1798       std::string PartMatchStr = FEntry->MutID.toString();
1799       StringRef   PartMatchName(PartMatchStr);
1800       Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1801                                            Exp->getExprLoc(), &PartMatchName);
1802     } else {
1803       // Warn that there's no match at all.
1804       Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1805                                            Exp->getExprLoc());
1806     }
1807     NoError = false;
1808   }
1809   // Make sure the mutex we found is the right kind.
1810   if (NoError && LDat && !LDat->isAtLeast(LK))
1811     Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1812                                          Exp->getExprLoc());
1813 }
1814
1815 /// \brief Warn if the LSet contains the given lock.
1816 void BuildLockset::warnIfMutexHeld(const NamedDecl *D, Expr* Exp,
1817                                    Expr *MutexExp) {
1818   SExpr Mutex(MutexExp, Exp, D);
1819   if (!Mutex.isValid()) {
1820     SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
1821     return;
1822   }
1823
1824   LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex);
1825   if (LDat) {
1826     std::string DeclName = D->getNameAsString();
1827     StringRef   DeclNameSR (DeclName);
1828     Analyzer->Handler.handleFunExcludesLock(DeclNameSR, Mutex.toString(),
1829                                             Exp->getExprLoc());
1830   }
1831 }
1832
1833
1834 /// \brief This method identifies variable dereferences and checks pt_guarded_by
1835 /// and pt_guarded_var annotations. Note that we only check these annotations
1836 /// at the time a pointer is dereferenced.
1837 /// FIXME: We need to check for other types of pointer dereferences
1838 /// (e.g. [], ->) and deal with them here.
1839 /// \param Exp An expression that has been read or written.
1840 void BuildLockset::checkDereference(Expr *Exp, AccessKind AK) {
1841   UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp);
1842   if (!UO || UO->getOpcode() != clang::UO_Deref)
1843     return;
1844   Exp = UO->getSubExpr()->IgnoreParenCasts();
1845
1846   const ValueDecl *D = getValueDecl(Exp);
1847   if(!D || !D->hasAttrs())
1848     return;
1849
1850   if (D->getAttr<PtGuardedVarAttr>() && FSet.isEmpty())
1851     Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK,
1852                                         Exp->getExprLoc());
1853
1854   const AttrVec &ArgAttrs = D->getAttrs();
1855   for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1856     if (PtGuardedByAttr *PGBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
1857       warnIfMutexNotHeld(D, Exp, AK, PGBAttr->getArg(), POK_VarDereference);
1858 }
1859
1860 /// \brief Checks guarded_by and guarded_var attributes.
1861 /// Whenever we identify an access (read or write) of a DeclRefExpr or
1862 /// MemberExpr, we need to check whether there are any guarded_by or
1863 /// guarded_var attributes, and make sure we hold the appropriate mutexes.
1864 void BuildLockset::checkAccess(Expr *Exp, AccessKind AK) {
1865   const ValueDecl *D = getValueDecl(Exp);
1866   if(!D || !D->hasAttrs())
1867     return;
1868
1869   if (D->getAttr<GuardedVarAttr>() && FSet.isEmpty())
1870     Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK,
1871                                         Exp->getExprLoc());
1872
1873   const AttrVec &ArgAttrs = D->getAttrs();
1874   for(unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1875     if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
1876       warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
1877 }
1878
1879 /// \brief Process a function call, method call, constructor call,
1880 /// or destructor call.  This involves looking at the attributes on the
1881 /// corresponding function/method/constructor/destructor, issuing warnings,
1882 /// and updating the locksets accordingly.
1883 ///
1884 /// FIXME: For classes annotated with one of the guarded annotations, we need
1885 /// to treat const method calls as reads and non-const method calls as writes,
1886 /// and check that the appropriate locks are held. Non-const method calls with
1887 /// the same signature as const method calls can be also treated as reads.
1888 ///
1889 void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) {
1890   const AttrVec &ArgAttrs = D->getAttrs();
1891   MutexIDList ExclusiveLocksToAdd;
1892   MutexIDList SharedLocksToAdd;
1893   MutexIDList LocksToRemove;
1894
1895   for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
1896     Attr *At = const_cast<Attr*>(ArgAttrs[i]);
1897     switch (At->getKind()) {
1898       // When we encounter an exclusive lock function, we need to add the lock
1899       // to our lockset with kind exclusive.
1900       case attr::ExclusiveLockFunction: {
1901         ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(At);
1902         Analyzer->getMutexIDs(ExclusiveLocksToAdd, A, Exp, D, VD);
1903         break;
1904       }
1905
1906       // When we encounter a shared lock function, we need to add the lock
1907       // to our lockset with kind shared.
1908       case attr::SharedLockFunction: {
1909         SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(At);
1910         Analyzer->getMutexIDs(SharedLocksToAdd, A, Exp, D, VD);
1911         break;
1912       }
1913
1914       // When we encounter an unlock function, we need to remove unlocked
1915       // mutexes from the lockset, and flag a warning if they are not there.
1916       case attr::UnlockFunction: {
1917         UnlockFunctionAttr *A = cast<UnlockFunctionAttr>(At);
1918         Analyzer->getMutexIDs(LocksToRemove, A, Exp, D, VD);
1919         break;
1920       }
1921
1922       case attr::ExclusiveLocksRequired: {
1923         ExclusiveLocksRequiredAttr *A = cast<ExclusiveLocksRequiredAttr>(At);
1924
1925         for (ExclusiveLocksRequiredAttr::args_iterator
1926              I = A->args_begin(), E = A->args_end(); I != E; ++I)
1927           warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
1928         break;
1929       }
1930
1931       case attr::SharedLocksRequired: {
1932         SharedLocksRequiredAttr *A = cast<SharedLocksRequiredAttr>(At);
1933
1934         for (SharedLocksRequiredAttr::args_iterator I = A->args_begin(),
1935              E = A->args_end(); I != E; ++I)
1936           warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
1937         break;
1938       }
1939
1940       case attr::LocksExcluded: {
1941         LocksExcludedAttr *A = cast<LocksExcludedAttr>(At);
1942
1943         for (LocksExcludedAttr::args_iterator I = A->args_begin(),
1944             E = A->args_end(); I != E; ++I) {
1945           warnIfMutexHeld(D, Exp, *I);
1946         }
1947         break;
1948       }
1949
1950       // Ignore other (non thread-safety) attributes
1951       default:
1952         break;
1953     }
1954   }
1955
1956   // Figure out if we're calling the constructor of scoped lockable class
1957   bool isScopedVar = false;
1958   if (VD) {
1959     if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) {
1960       const CXXRecordDecl* PD = CD->getParent();
1961       if (PD && PD->getAttr<ScopedLockableAttr>())
1962         isScopedVar = true;
1963     }
1964   }
1965
1966   // Add locks.
1967   SourceLocation Loc = Exp->getExprLoc();
1968   for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
1969     Analyzer->addLock(FSet, ExclusiveLocksToAdd[i],
1970                             LockData(Loc, LK_Exclusive, isScopedVar));
1971   }
1972   for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
1973     Analyzer->addLock(FSet, SharedLocksToAdd[i],
1974                             LockData(Loc, LK_Shared, isScopedVar));
1975   }
1976
1977   // Add the managing object as a dummy mutex, mapped to the underlying mutex.
1978   // FIXME -- this doesn't work if we acquire multiple locks.
1979   if (isScopedVar) {
1980     SourceLocation MLoc = VD->getLocation();
1981     DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
1982     SExpr SMutex(&DRE, 0, 0);
1983
1984     for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
1985       Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive,
1986                                                ExclusiveLocksToAdd[i]));
1987     }
1988     for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
1989       Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared,
1990                                                SharedLocksToAdd[i]));
1991     }
1992   }
1993
1994   // Remove locks.
1995   // FIXME -- should only fully remove if the attribute refers to 'this'.
1996   bool Dtor = isa<CXXDestructorDecl>(D);
1997   for (unsigned i=0,n=LocksToRemove.size(); i<n; ++i) {
1998     Analyzer->removeLock(FSet, LocksToRemove[i], Loc, Dtor);
1999   }
2000 }
2001
2002
2003 /// \brief For unary operations which read and write a variable, we need to
2004 /// check whether we hold any required mutexes. Reads are checked in
2005 /// VisitCastExpr.
2006 void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
2007   switch (UO->getOpcode()) {
2008     case clang::UO_PostDec:
2009     case clang::UO_PostInc:
2010     case clang::UO_PreDec:
2011     case clang::UO_PreInc: {
2012       Expr *SubExp = UO->getSubExpr()->IgnoreParenCasts();
2013       checkAccess(SubExp, AK_Written);
2014       checkDereference(SubExp, AK_Written);
2015       break;
2016     }
2017     default:
2018       break;
2019   }
2020 }
2021
2022 /// For binary operations which assign to a variable (writes), we need to check
2023 /// whether we hold any required mutexes.
2024 /// FIXME: Deal with non-primitive types.
2025 void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
2026   if (!BO->isAssignmentOp())
2027     return;
2028
2029   // adjust the context
2030   LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
2031
2032   Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
2033   checkAccess(LHSExp, AK_Written);
2034   checkDereference(LHSExp, AK_Written);
2035 }
2036
2037 /// Whenever we do an LValue to Rvalue cast, we are reading a variable and
2038 /// need to ensure we hold any required mutexes.
2039 /// FIXME: Deal with non-primitive types.
2040 void BuildLockset::VisitCastExpr(CastExpr *CE) {
2041   if (CE->getCastKind() != CK_LValueToRValue)
2042     return;
2043   Expr *SubExp = CE->getSubExpr()->IgnoreParenCasts();
2044   checkAccess(SubExp, AK_Read);
2045   checkDereference(SubExp, AK_Read);
2046 }
2047
2048
2049 void BuildLockset::VisitCallExpr(CallExpr *Exp) {
2050   NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
2051   if(!D || !D->hasAttrs())
2052     return;
2053   handleCall(Exp, D);
2054 }
2055
2056 void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
2057   // FIXME -- only handles constructors in DeclStmt below.
2058 }
2059
2060 void BuildLockset::VisitDeclStmt(DeclStmt *S) {
2061   // adjust the context
2062   LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
2063
2064   DeclGroupRef DGrp = S->getDeclGroup();
2065   for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
2066     Decl *D = *I;
2067     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
2068       Expr *E = VD->getInit();
2069       // handle constructors that involve temporaries
2070       if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E))
2071         E = EWC->getSubExpr();
2072
2073       if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
2074         NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
2075         if (!CtorD || !CtorD->hasAttrs())
2076           return;
2077         handleCall(CE, CtorD, VD);
2078       }
2079     }
2080   }
2081 }
2082
2083
2084
2085 /// \brief Compute the intersection of two locksets and issue warnings for any
2086 /// locks in the symmetric difference.
2087 ///
2088 /// This function is used at a merge point in the CFG when comparing the lockset
2089 /// of each branch being merged. For example, given the following sequence:
2090 /// A; if () then B; else C; D; we need to check that the lockset after B and C
2091 /// are the same. In the event of a difference, we use the intersection of these
2092 /// two locksets at the start of D.
2093 ///
2094 /// \param FSet1 The first lockset.
2095 /// \param FSet2 The second lockset.
2096 /// \param JoinLoc The location of the join point for error reporting
2097 /// \param LEK1 The error message to report if a mutex is missing from LSet1
2098 /// \param LEK2 The error message to report if a mutex is missing from Lset2
2099 void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1,
2100                                             const FactSet &FSet2,
2101                                             SourceLocation JoinLoc,
2102                                             LockErrorKind LEK1,
2103                                             LockErrorKind LEK2,
2104                                             bool Modify) {
2105   FactSet FSet1Orig = FSet1;
2106
2107   for (FactSet::const_iterator I = FSet2.begin(), E = FSet2.end();
2108        I != E; ++I) {
2109     const SExpr &FSet2Mutex = FactMan[*I].MutID;
2110     const LockData &LDat2 = FactMan[*I].LDat;
2111
2112     if (const LockData *LDat1 = FSet1.findLock(FactMan, FSet2Mutex)) {
2113       if (LDat1->LKind != LDat2.LKind) {
2114         Handler.handleExclusiveAndShared(FSet2Mutex.toString(),
2115                                          LDat2.AcquireLoc,
2116                                          LDat1->AcquireLoc);
2117         if (Modify && LDat1->LKind != LK_Exclusive) {
2118           FSet1.removeLock(FactMan, FSet2Mutex);
2119           FSet1.addLock(FactMan, FSet2Mutex, LDat2);
2120         }
2121       }
2122     } else {
2123       if (LDat2.UnderlyingMutex.isValid()) {
2124         if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) {
2125           // If this is a scoped lock that manages another mutex, and if the
2126           // underlying mutex is still held, then warn about the underlying
2127           // mutex.
2128           Handler.handleMutexHeldEndOfScope(LDat2.UnderlyingMutex.toString(),
2129                                             LDat2.AcquireLoc,
2130                                             JoinLoc, LEK1);
2131         }
2132       }
2133       else if (!LDat2.Managed && !FSet2Mutex.isUniversal())
2134         Handler.handleMutexHeldEndOfScope(FSet2Mutex.toString(),
2135                                           LDat2.AcquireLoc,
2136                                           JoinLoc, LEK1);
2137     }
2138   }
2139
2140   for (FactSet::const_iterator I = FSet1.begin(), E = FSet1.end();
2141        I != E; ++I) {
2142     const SExpr &FSet1Mutex = FactMan[*I].MutID;
2143     const LockData &LDat1 = FactMan[*I].LDat;
2144
2145     if (!FSet2.findLock(FactMan, FSet1Mutex)) {
2146       if (LDat1.UnderlyingMutex.isValid()) {
2147         if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) {
2148           // If this is a scoped lock that manages another mutex, and if the
2149           // underlying mutex is still held, then warn about the underlying
2150           // mutex.
2151           Handler.handleMutexHeldEndOfScope(LDat1.UnderlyingMutex.toString(),
2152                                             LDat1.AcquireLoc,
2153                                             JoinLoc, LEK1);
2154         }
2155       }
2156       else if (!LDat1.Managed && !FSet1Mutex.isUniversal())
2157         Handler.handleMutexHeldEndOfScope(FSet1Mutex.toString(),
2158                                           LDat1.AcquireLoc,
2159                                           JoinLoc, LEK2);
2160       if (Modify)
2161         FSet1.removeLock(FactMan, FSet1Mutex);
2162     }
2163   }
2164 }
2165
2166
2167
2168 /// \brief Check a function's CFG for thread-safety violations.
2169 ///
2170 /// We traverse the blocks in the CFG, compute the set of mutexes that are held
2171 /// at the end of each block, and issue warnings for thread safety violations.
2172 /// Each block in the CFG is traversed exactly once.
2173 void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
2174   CFG *CFGraph = AC.getCFG();
2175   if (!CFGraph) return;
2176   const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
2177
2178   // AC.dumpCFG(true);
2179
2180   if (!D)
2181     return;  // Ignore anonymous functions for now.
2182   if (D->getAttr<NoThreadSafetyAnalysisAttr>())
2183     return;
2184   // FIXME: Do something a bit more intelligent inside constructor and
2185   // destructor code.  Constructors and destructors must assume unique access
2186   // to 'this', so checks on member variable access is disabled, but we should
2187   // still enable checks on other objects.
2188   if (isa<CXXConstructorDecl>(D))
2189     return;  // Don't check inside constructors.
2190   if (isa<CXXDestructorDecl>(D))
2191     return;  // Don't check inside destructors.
2192
2193   BlockInfo.resize(CFGraph->getNumBlockIDs(),
2194     CFGBlockInfo::getEmptyBlockInfo(LocalVarMap));
2195
2196   // We need to explore the CFG via a "topological" ordering.
2197   // That way, we will be guaranteed to have information about required
2198   // predecessor locksets when exploring a new block.
2199   PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
2200   PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
2201
2202   // Mark entry block as reachable
2203   BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true;
2204
2205   // Compute SSA names for local variables
2206   LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
2207
2208   // Fill in source locations for all CFGBlocks.
2209   findBlockLocations(CFGraph, SortedGraph, BlockInfo);
2210
2211   // Add locks from exclusive_locks_required and shared_locks_required
2212   // to initial lockset. Also turn off checking for lock and unlock functions.
2213   // FIXME: is there a more intelligent way to check lock/unlock functions?
2214   if (!SortedGraph->empty() && D->hasAttrs()) {
2215     const CFGBlock *FirstBlock = *SortedGraph->begin();
2216     FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
2217     const AttrVec &ArgAttrs = D->getAttrs();
2218
2219     MutexIDList ExclusiveLocksToAdd;
2220     MutexIDList SharedLocksToAdd;
2221
2222     SourceLocation Loc = D->getLocation();
2223     for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
2224       Attr *Attr = ArgAttrs[i];
2225       Loc = Attr->getLocation();
2226       if (ExclusiveLocksRequiredAttr *A
2227             = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
2228         getMutexIDs(ExclusiveLocksToAdd, A, (Expr*) 0, D);
2229       } else if (SharedLocksRequiredAttr *A
2230                    = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
2231         getMutexIDs(SharedLocksToAdd, A, (Expr*) 0, D);
2232       } else if (isa<UnlockFunctionAttr>(Attr)) {
2233         // Don't try to check unlock functions for now
2234         return;
2235       } else if (isa<ExclusiveLockFunctionAttr>(Attr)) {
2236         // Don't try to check lock functions for now
2237         return;
2238       } else if (isa<SharedLockFunctionAttr>(Attr)) {
2239         // Don't try to check lock functions for now
2240         return;
2241       } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) {
2242         // Don't try to check trylock functions for now
2243         return;
2244       } else if (isa<SharedTrylockFunctionAttr>(Attr)) {
2245         // Don't try to check trylock functions for now
2246         return;
2247       }
2248     }
2249
2250     // FIXME -- Loc can be wrong here.
2251     for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
2252       addLock(InitialLockset, ExclusiveLocksToAdd[i],
2253               LockData(Loc, LK_Exclusive));
2254     }
2255     for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
2256       addLock(InitialLockset, SharedLocksToAdd[i],
2257               LockData(Loc, LK_Shared));
2258     }
2259   }
2260
2261   for (PostOrderCFGView::iterator I = SortedGraph->begin(),
2262        E = SortedGraph->end(); I!= E; ++I) {
2263     const CFGBlock *CurrBlock = *I;
2264     int CurrBlockID = CurrBlock->getBlockID();
2265     CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
2266
2267     // Use the default initial lockset in case there are no predecessors.
2268     VisitedBlocks.insert(CurrBlock);
2269
2270     // Iterate through the predecessor blocks and warn if the lockset for all
2271     // predecessors is not the same. We take the entry lockset of the current
2272     // block to be the intersection of all previous locksets.
2273     // FIXME: By keeping the intersection, we may output more errors in future
2274     // for a lock which is not in the intersection, but was in the union. We
2275     // may want to also keep the union in future. As an example, let's say
2276     // the intersection contains Mutex L, and the union contains L and M.
2277     // Later we unlock M. At this point, we would output an error because we
2278     // never locked M; although the real error is probably that we forgot to
2279     // lock M on all code paths. Conversely, let's say that later we lock M.
2280     // In this case, we should compare against the intersection instead of the
2281     // union because the real error is probably that we forgot to unlock M on
2282     // all code paths.
2283     bool LocksetInitialized = false;
2284     llvm::SmallVector<CFGBlock*, 8> SpecialBlocks;
2285     for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
2286          PE  = CurrBlock->pred_end(); PI != PE; ++PI) {
2287
2288       // if *PI -> CurrBlock is a back edge
2289       if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
2290         continue;
2291
2292       int PrevBlockID = (*PI)->getBlockID();
2293       CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2294
2295       // Ignore edges from blocks that can't return.
2296       if ((*PI)->hasNoReturnElement() || !PrevBlockInfo->Reachable)
2297         continue;
2298
2299       // Okay, we can reach this block from the entry.
2300       CurrBlockInfo->Reachable = true;
2301
2302       // If the previous block ended in a 'continue' or 'break' statement, then
2303       // a difference in locksets is probably due to a bug in that block, rather
2304       // than in some other predecessor. In that case, keep the other
2305       // predecessor's lockset.
2306       if (const Stmt *Terminator = (*PI)->getTerminator()) {
2307         if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
2308           SpecialBlocks.push_back(*PI);
2309           continue;
2310         }
2311       }
2312
2313
2314       FactSet PrevLockset;
2315       getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock);
2316
2317       if (!LocksetInitialized) {
2318         CurrBlockInfo->EntrySet = PrevLockset;
2319         LocksetInitialized = true;
2320       } else {
2321         intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2322                          CurrBlockInfo->EntryLoc,
2323                          LEK_LockedSomePredecessors);
2324       }
2325     }
2326
2327     // Skip rest of block if it's not reachable.
2328     if (!CurrBlockInfo->Reachable)
2329       continue;
2330
2331     // Process continue and break blocks. Assume that the lockset for the
2332     // resulting block is unaffected by any discrepancies in them.
2333     for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size();
2334          SpecialI < SpecialN; ++SpecialI) {
2335       CFGBlock *PrevBlock = SpecialBlocks[SpecialI];
2336       int PrevBlockID = PrevBlock->getBlockID();
2337       CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2338
2339       if (!LocksetInitialized) {
2340         CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
2341         LocksetInitialized = true;
2342       } else {
2343         // Determine whether this edge is a loop terminator for diagnostic
2344         // purposes. FIXME: A 'break' statement might be a loop terminator, but
2345         // it might also be part of a switch. Also, a subsequent destructor
2346         // might add to the lockset, in which case the real issue might be a
2347         // double lock on the other path.
2348         const Stmt *Terminator = PrevBlock->getTerminator();
2349         bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
2350
2351         FactSet PrevLockset;
2352         getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet,
2353                        PrevBlock, CurrBlock);
2354
2355         // Do not update EntrySet.
2356         intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2357                          PrevBlockInfo->ExitLoc,
2358                          IsLoop ? LEK_LockedSomeLoopIterations
2359                                 : LEK_LockedSomePredecessors,
2360                          false);
2361       }
2362     }
2363
2364     BuildLockset LocksetBuilder(this, *CurrBlockInfo);
2365
2366     // Visit all the statements in the basic block.
2367     for (CFGBlock::const_iterator BI = CurrBlock->begin(),
2368          BE = CurrBlock->end(); BI != BE; ++BI) {
2369       switch (BI->getKind()) {
2370         case CFGElement::Statement: {
2371           const CFGStmt *CS = cast<CFGStmt>(&*BI);
2372           LocksetBuilder.Visit(const_cast<Stmt*>(CS->getStmt()));
2373           break;
2374         }
2375         // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
2376         case CFGElement::AutomaticObjectDtor: {
2377           const CFGAutomaticObjDtor *AD = cast<CFGAutomaticObjDtor>(&*BI);
2378           CXXDestructorDecl *DD = const_cast<CXXDestructorDecl*>(
2379             AD->getDestructorDecl(AC.getASTContext()));
2380           if (!DD->hasAttrs())
2381             break;
2382
2383           // Create a dummy expression,
2384           VarDecl *VD = const_cast<VarDecl*>(AD->getVarDecl());
2385           DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
2386                           AD->getTriggerStmt()->getLocEnd());
2387           LocksetBuilder.handleCall(&DRE, DD);
2388           break;
2389         }
2390         default:
2391           break;
2392       }
2393     }
2394     CurrBlockInfo->ExitSet = LocksetBuilder.FSet;
2395
2396     // For every back edge from CurrBlock (the end of the loop) to another block
2397     // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
2398     // the one held at the beginning of FirstLoopBlock. We can look up the
2399     // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
2400     for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
2401          SE  = CurrBlock->succ_end(); SI != SE; ++SI) {
2402
2403       // if CurrBlock -> *SI is *not* a back edge
2404       if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
2405         continue;
2406
2407       CFGBlock *FirstLoopBlock = *SI;
2408       CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
2409       CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
2410       intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
2411                        PreLoop->EntryLoc,
2412                        LEK_LockedSomeLoopIterations,
2413                        false);
2414     }
2415   }
2416
2417   CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
2418   CFGBlockInfo *Final   = &BlockInfo[CFGraph->getExit().getBlockID()];
2419
2420   // Skip the final check if the exit block is unreachable.
2421   if (!Final->Reachable)
2422     return;
2423
2424   // FIXME: Should we call this function for all blocks which exit the function?
2425   intersectAndWarn(Initial->EntrySet, Final->ExitSet,
2426                    Final->ExitLoc,
2427                    LEK_LockedAtEndOfFunction,
2428                    LEK_NotLockedAtEndOfFunction,
2429                    false);
2430 }
2431
2432 } // end anonymous namespace
2433
2434
2435 namespace clang {
2436 namespace thread_safety {
2437
2438 /// \brief Check a function's CFG for thread-safety violations.
2439 ///
2440 /// We traverse the blocks in the CFG, compute the set of mutexes that are held
2441 /// at the end of each block, and issue warnings for thread safety violations.
2442 /// Each block in the CFG is traversed exactly once.
2443 void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
2444                              ThreadSafetyHandler &Handler) {
2445   ThreadSafetyAnalyzer Analyzer(Handler);
2446   Analyzer.runAnalysis(AC);
2447 }
2448
2449 /// \brief Helper function that returns a LockKind required for the given level
2450 /// of access.
2451 LockKind getLockKindFromAccessKind(AccessKind AK) {
2452   switch (AK) {
2453     case AK_Read :
2454       return LK_Shared;
2455     case AK_Written :
2456       return LK_Exclusive;
2457   }
2458   llvm_unreachable("Unknown AccessKind");
2459 }
2460
2461 }} // end namespace clang::thread_safety