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