1 //===- ThreadSafety.cpp ----------------------------------------*- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A intra-procedural analysis for thread safety (e.g. deadlocks and race
11 // conditions), based off of an annotation system.
13 // See http://clang.llvm.org/docs/LanguageExtensions.html#thread-safety-annotation-checking
14 // for more information.
16 //===----------------------------------------------------------------------===//
18 #include "clang/Analysis/Analyses/ThreadSafety.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/Analysis/Analyses/PostOrderCFGView.h"
25 #include "clang/Analysis/AnalysisContext.h"
26 #include "clang/Analysis/CFG.h"
27 #include "clang/Analysis/CFGStmtMap.h"
28 #include "clang/Basic/OperatorKinds.h"
29 #include "clang/Basic/SourceLocation.h"
30 #include "clang/Basic/SourceManager.h"
31 #include "llvm/ADT/BitVector.h"
32 #include "llvm/ADT/FoldingSet.h"
33 #include "llvm/ADT/ImmutableMap.h"
34 #include "llvm/ADT/PostOrderIterator.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/ADT/StringRef.h"
37 #include "llvm/Support/raw_ostream.h"
42 using namespace clang;
43 using namespace thread_safety;
45 // Key method definition
46 ThreadSafetyHandler::~ThreadSafetyHandler() {}
50 /// SExpr implements a simple expression language that is used to store,
51 /// compare, and pretty-print C++ expressions. Unlike a clang Expr, a SExpr
52 /// does not capture surface syntax, and it does not distinguish between
53 /// C++ concepts, like pointers and references, that have no real semantic
54 /// differences. This simplicity allows SExprs to be meaningfully compared,
57 /// (*this).foo = this->foo
60 /// Thread-safety analysis works by comparing lock expressions. Within the
61 /// body of a function, an expression such as "x->foo->bar.mu" will resolve to
62 /// a particular mutex object at run-time. Subsequent occurrences of the same
63 /// expression (where "same" means syntactic equality) will refer to the same
64 /// run-time object if three conditions hold:
65 /// (1) Local variables in the expression, such as "x" have not changed.
66 /// (2) Values on the heap that affect the expression have not changed.
67 /// (3) The expression involves only pure function calls.
69 /// The current implementation assumes, but does not verify, that multiple uses
70 /// of the same lock expression satisfies these criteria.
75 EOP_Wildcard, ///< Matches anything.
76 EOP_Universal, ///< Universal lock.
77 EOP_This, ///< This keyword.
78 EOP_NVar, ///< Named variable.
79 EOP_LVar, ///< Local variable.
80 EOP_Dot, ///< Field access
81 EOP_Call, ///< Function call
82 EOP_MCall, ///< Method call
83 EOP_Index, ///< Array index
84 EOP_Unary, ///< Unary operation
85 EOP_Binary, ///< Binary operation
86 EOP_Unknown ///< Catchall for everything else
92 unsigned char Op; ///< Opcode of the root node
93 unsigned char Flags; ///< Additional opcode-specific data
94 unsigned short Sz; ///< Number of child nodes
95 const void* Data; ///< Additional opcode-specific data
98 SExprNode(ExprOp O, unsigned F, const void* D)
99 : Op(static_cast<unsigned char>(O)),
100 Flags(static_cast<unsigned char>(F)), Sz(1), Data(D)
103 unsigned size() const { return Sz; }
104 void setSize(unsigned S) { Sz = S; }
106 ExprOp kind() const { return static_cast<ExprOp>(Op); }
108 const NamedDecl* getNamedDecl() const {
109 assert(Op == EOP_NVar || Op == EOP_LVar || Op == EOP_Dot);
110 return reinterpret_cast<const NamedDecl*>(Data);
113 const NamedDecl* getFunctionDecl() const {
114 assert(Op == EOP_Call || Op == EOP_MCall);
115 return reinterpret_cast<const NamedDecl*>(Data);
118 bool isArrow() const { return Op == EOP_Dot && Flags == 1; }
119 void setArrow(bool A) { Flags = A ? 1 : 0; }
121 unsigned arity() const {
123 case EOP_Nop: return 0;
124 case EOP_Wildcard: return 0;
125 case EOP_Universal: return 0;
126 case EOP_NVar: return 0;
127 case EOP_LVar: return 0;
128 case EOP_This: return 0;
129 case EOP_Dot: return 1;
130 case EOP_Call: return Flags+1; // First arg is function.
131 case EOP_MCall: return Flags+1; // First arg is implicit obj.
132 case EOP_Index: return 2;
133 case EOP_Unary: return 1;
134 case EOP_Binary: return 2;
135 case EOP_Unknown: return Flags;
140 bool operator==(const SExprNode& Other) const {
141 // Ignore flags and size -- they don't matter.
142 return (Op == Other.Op &&
146 bool operator!=(const SExprNode& Other) const {
147 return !(*this == Other);
150 bool matches(const SExprNode& Other) const {
151 return (*this == Other) ||
152 (Op == EOP_Wildcard) ||
153 (Other.Op == EOP_Wildcard);
158 /// \brief Encapsulates the lexical context of a function call. The lexical
159 /// context includes the arguments to the call, including the implicit object
160 /// argument. When an attribute containing a mutex expression is attached to
161 /// a method, the expression may refer to formal parameters of the method.
162 /// Actual arguments must be substituted for formal parameters to derive
163 /// the appropriate mutex expression in the lexical context where the function
164 /// is called. PrevCtx holds the context in which the arguments themselves
165 /// should be evaluated; multiple calling contexts can be chained together
166 /// by the lock_returned attribute.
167 struct CallingContext {
168 const NamedDecl* AttrDecl; // The decl to which the attribute is attached.
169 const Expr* SelfArg; // Implicit object argument -- e.g. 'this'
170 bool SelfArrow; // is Self referred to with -> or .?
171 unsigned NumArgs; // Number of funArgs
172 const Expr* const* FunArgs; // Function arguments
173 CallingContext* PrevCtx; // The previous context; or 0 if none.
175 CallingContext(const NamedDecl *D = 0, const Expr *S = 0,
176 unsigned N = 0, const Expr* const *A = 0,
177 CallingContext *P = 0)
178 : AttrDecl(D), SelfArg(S), SelfArrow(false),
179 NumArgs(N), FunArgs(A), PrevCtx(P)
183 typedef SmallVector<SExprNode, 4> NodeVector;
186 // A SExpr is a list of SExprNodes in prefix order. The Size field allows
187 // the list to be traversed as a tree.
192 NodeVec.push_back(SExprNode(EOP_Nop, 0, 0));
193 return NodeVec.size()-1;
196 unsigned makeWildcard() {
197 NodeVec.push_back(SExprNode(EOP_Wildcard, 0, 0));
198 return NodeVec.size()-1;
201 unsigned makeUniversal() {
202 NodeVec.push_back(SExprNode(EOP_Universal, 0, 0));
203 return NodeVec.size()-1;
206 unsigned makeNamedVar(const NamedDecl *D) {
207 NodeVec.push_back(SExprNode(EOP_NVar, 0, D));
208 return NodeVec.size()-1;
211 unsigned makeLocalVar(const NamedDecl *D) {
212 NodeVec.push_back(SExprNode(EOP_LVar, 0, D));
213 return NodeVec.size()-1;
216 unsigned makeThis() {
217 NodeVec.push_back(SExprNode(EOP_This, 0, 0));
218 return NodeVec.size()-1;
221 unsigned makeDot(const NamedDecl *D, bool Arrow) {
222 NodeVec.push_back(SExprNode(EOP_Dot, Arrow ? 1 : 0, D));
223 return NodeVec.size()-1;
226 unsigned makeCall(unsigned NumArgs, const NamedDecl *D) {
227 NodeVec.push_back(SExprNode(EOP_Call, NumArgs, D));
228 return NodeVec.size()-1;
231 // Grab the very first declaration of virtual method D
232 const CXXMethodDecl* getFirstVirtualDecl(const CXXMethodDecl *D) {
234 D = D->getCanonicalDecl();
235 CXXMethodDecl::method_iterator I = D->begin_overridden_methods(),
236 E = D->end_overridden_methods();
238 return D; // Method does not override anything
239 D = *I; // FIXME: this does not work with multiple inheritance.
244 unsigned makeMCall(unsigned NumArgs, const CXXMethodDecl *D) {
245 NodeVec.push_back(SExprNode(EOP_MCall, NumArgs, getFirstVirtualDecl(D)));
246 return NodeVec.size()-1;
249 unsigned makeIndex() {
250 NodeVec.push_back(SExprNode(EOP_Index, 0, 0));
251 return NodeVec.size()-1;
254 unsigned makeUnary() {
255 NodeVec.push_back(SExprNode(EOP_Unary, 0, 0));
256 return NodeVec.size()-1;
259 unsigned makeBinary() {
260 NodeVec.push_back(SExprNode(EOP_Binary, 0, 0));
261 return NodeVec.size()-1;
264 unsigned makeUnknown(unsigned Arity) {
265 NodeVec.push_back(SExprNode(EOP_Unknown, Arity, 0));
266 return NodeVec.size()-1;
269 inline bool isCalleeArrow(const Expr *E) {
270 const MemberExpr *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts());
271 return ME ? ME->isArrow() : false;
274 /// Build an SExpr from the given C++ expression.
275 /// Recursive function that terminates on DeclRefExpr.
276 /// Note: this function merely creates a SExpr; it does not check to
277 /// ensure that the original expression is a valid mutex expression.
279 /// NDeref returns the number of Derefence and AddressOf operations
280 /// preceeding the Expr; this is used to decide whether to pretty-print
281 /// SExprs with . or ->.
282 unsigned buildSExpr(const Expr *Exp, CallingContext* CallCtx,
287 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) {
288 const NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl());
289 const ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND);
291 const FunctionDecl *FD =
292 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
293 unsigned i = PV->getFunctionScopeIndex();
295 if (CallCtx && CallCtx->FunArgs &&
296 FD == CallCtx->AttrDecl->getCanonicalDecl()) {
297 // Substitute call arguments for references to function parameters
298 assert(i < CallCtx->NumArgs);
299 return buildSExpr(CallCtx->FunArgs[i], CallCtx->PrevCtx, NDeref);
301 // Map the param back to the param of the original function declaration.
302 makeNamedVar(FD->getParamDecl(i));
305 // Not a function parameter -- just store the reference.
308 } else if (isa<CXXThisExpr>(Exp)) {
309 // Substitute parent for 'this'
310 if (CallCtx && CallCtx->SelfArg) {
311 if (!CallCtx->SelfArrow && NDeref)
312 // 'this' is a pointer, but self is not, so need to take address.
314 return buildSExpr(CallCtx->SelfArg, CallCtx->PrevCtx, NDeref);
320 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
321 const NamedDecl *ND = ME->getMemberDecl();
322 int ImplicitDeref = ME->isArrow() ? 1 : 0;
323 unsigned Root = makeDot(ND, false);
324 unsigned Sz = buildSExpr(ME->getBase(), CallCtx, &ImplicitDeref);
325 NodeVec[Root].setArrow(ImplicitDeref > 0);
326 NodeVec[Root].setSize(Sz + 1);
328 } else if (const CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) {
329 // When calling a function with a lock_returned attribute, replace
330 // the function call with the expression in lock_returned.
331 const CXXMethodDecl *MD = CMCE->getMethodDecl()->getMostRecentDecl();
332 if (LockReturnedAttr* At = MD->getAttr<LockReturnedAttr>()) {
333 CallingContext LRCallCtx(CMCE->getMethodDecl());
334 LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument();
335 LRCallCtx.SelfArrow = isCalleeArrow(CMCE->getCallee());
336 LRCallCtx.NumArgs = CMCE->getNumArgs();
337 LRCallCtx.FunArgs = CMCE->getArgs();
338 LRCallCtx.PrevCtx = CallCtx;
339 return buildSExpr(At->getArg(), &LRCallCtx);
341 // Hack to treat smart pointers and iterators as pointers;
342 // ignore any method named get().
343 if (CMCE->getMethodDecl()->getNameAsString() == "get" &&
344 CMCE->getNumArgs() == 0) {
345 if (NDeref && isCalleeArrow(CMCE->getCallee()))
347 return buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx, NDeref);
349 unsigned NumCallArgs = CMCE->getNumArgs();
350 unsigned Root = makeMCall(NumCallArgs, CMCE->getMethodDecl());
351 unsigned Sz = buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx);
352 const Expr* const* CallArgs = CMCE->getArgs();
353 for (unsigned i = 0; i < NumCallArgs; ++i) {
354 Sz += buildSExpr(CallArgs[i], CallCtx);
356 NodeVec[Root].setSize(Sz + 1);
358 } else if (const CallExpr *CE = dyn_cast<CallExpr>(Exp)) {
359 const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl();
360 if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) {
361 CallingContext LRCallCtx(CE->getDirectCallee());
362 LRCallCtx.NumArgs = CE->getNumArgs();
363 LRCallCtx.FunArgs = CE->getArgs();
364 LRCallCtx.PrevCtx = CallCtx;
365 return buildSExpr(At->getArg(), &LRCallCtx);
367 // Treat smart pointers and iterators as pointers;
368 // ignore the * and -> operators.
369 if (const CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(CE)) {
370 OverloadedOperatorKind k = OE->getOperator();
372 if (NDeref) ++(*NDeref);
373 return buildSExpr(OE->getArg(0), CallCtx, NDeref);
375 else if (k == OO_Arrow) {
376 return buildSExpr(OE->getArg(0), CallCtx, NDeref);
379 unsigned NumCallArgs = CE->getNumArgs();
380 unsigned Root = makeCall(NumCallArgs, 0);
381 unsigned Sz = buildSExpr(CE->getCallee(), CallCtx);
382 const Expr* const* CallArgs = CE->getArgs();
383 for (unsigned i = 0; i < NumCallArgs; ++i) {
384 Sz += buildSExpr(CallArgs[i], CallCtx);
386 NodeVec[Root].setSize(Sz+1);
388 } else if (const BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) {
389 unsigned Root = makeBinary();
390 unsigned Sz = buildSExpr(BOE->getLHS(), CallCtx);
391 Sz += buildSExpr(BOE->getRHS(), CallCtx);
392 NodeVec[Root].setSize(Sz);
394 } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) {
395 // Ignore & and * operators -- they're no-ops.
396 // However, we try to figure out whether the expression is a pointer,
397 // so we can use . and -> appropriately in error messages.
398 if (UOE->getOpcode() == UO_Deref) {
399 if (NDeref) ++(*NDeref);
400 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
402 if (UOE->getOpcode() == UO_AddrOf) {
403 if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UOE->getSubExpr())) {
404 if (DRE->getDecl()->isCXXInstanceMember()) {
405 // This is a pointer-to-member expression, e.g. &MyClass::mu_.
406 // We interpret this syntax specially, as a wildcard.
407 unsigned Root = makeDot(DRE->getDecl(), false);
409 NodeVec[Root].setSize(2);
413 if (NDeref) --(*NDeref);
414 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref);
416 unsigned Root = makeUnary();
417 unsigned Sz = buildSExpr(UOE->getSubExpr(), CallCtx);
418 NodeVec[Root].setSize(Sz);
420 } else if (const ArraySubscriptExpr *ASE =
421 dyn_cast<ArraySubscriptExpr>(Exp)) {
422 unsigned Root = makeIndex();
423 unsigned Sz = buildSExpr(ASE->getBase(), CallCtx);
424 Sz += buildSExpr(ASE->getIdx(), CallCtx);
425 NodeVec[Root].setSize(Sz);
427 } else if (const AbstractConditionalOperator *CE =
428 dyn_cast<AbstractConditionalOperator>(Exp)) {
429 unsigned Root = makeUnknown(3);
430 unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
431 Sz += buildSExpr(CE->getTrueExpr(), CallCtx);
432 Sz += buildSExpr(CE->getFalseExpr(), CallCtx);
433 NodeVec[Root].setSize(Sz);
435 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) {
436 unsigned Root = makeUnknown(3);
437 unsigned Sz = buildSExpr(CE->getCond(), CallCtx);
438 Sz += buildSExpr(CE->getLHS(), CallCtx);
439 Sz += buildSExpr(CE->getRHS(), CallCtx);
440 NodeVec[Root].setSize(Sz);
442 } else if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
443 return buildSExpr(CE->getSubExpr(), CallCtx, NDeref);
444 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
445 return buildSExpr(PE->getSubExpr(), CallCtx, NDeref);
446 } else if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Exp)) {
447 return buildSExpr(EWC->getSubExpr(), CallCtx, NDeref);
448 } else if (const CXXBindTemporaryExpr *E = dyn_cast<CXXBindTemporaryExpr>(Exp)) {
449 return buildSExpr(E->getSubExpr(), CallCtx, NDeref);
450 } else if (isa<CharacterLiteral>(Exp) ||
451 isa<CXXNullPtrLiteralExpr>(Exp) ||
452 isa<GNUNullExpr>(Exp) ||
453 isa<CXXBoolLiteralExpr>(Exp) ||
454 isa<FloatingLiteral>(Exp) ||
455 isa<ImaginaryLiteral>(Exp) ||
456 isa<IntegerLiteral>(Exp) ||
457 isa<StringLiteral>(Exp) ||
458 isa<ObjCStringLiteral>(Exp)) {
460 return 1; // FIXME: Ignore literals for now
463 return 1; // Ignore. FIXME: mark as invalid expression?
467 /// \brief Construct a SExpr from an expression.
468 /// \param MutexExp The original mutex expression within an attribute
469 /// \param DeclExp An expression involving the Decl on which the attribute
471 /// \param D The declaration to which the lock/unlock attribute is attached.
472 void buildSExprFromExpr(const Expr *MutexExp, const Expr *DeclExp,
473 const NamedDecl *D, VarDecl *SelfDecl = 0) {
474 CallingContext CallCtx(D);
477 if (const StringLiteral* SLit = dyn_cast<StringLiteral>(MutexExp)) {
478 if (SLit->getString() == StringRef("*"))
479 // The "*" expr is a universal lock, which essentially turns off
480 // checks until it is removed from the lockset.
483 // Ignore other string literals for now.
489 // If we are processing a raw attribute expression, with no substitutions.
491 buildSExpr(MutexExp, 0);
495 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
496 // for formal parameters when we call buildMutexID later.
497 if (const MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
498 CallCtx.SelfArg = ME->getBase();
499 CallCtx.SelfArrow = ME->isArrow();
500 } else if (const CXXMemberCallExpr *CE =
501 dyn_cast<CXXMemberCallExpr>(DeclExp)) {
502 CallCtx.SelfArg = CE->getImplicitObjectArgument();
503 CallCtx.SelfArrow = isCalleeArrow(CE->getCallee());
504 CallCtx.NumArgs = CE->getNumArgs();
505 CallCtx.FunArgs = CE->getArgs();
506 } else if (const CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
507 CallCtx.NumArgs = CE->getNumArgs();
508 CallCtx.FunArgs = CE->getArgs();
509 } else if (const CXXConstructExpr *CE =
510 dyn_cast<CXXConstructExpr>(DeclExp)) {
511 CallCtx.SelfArg = 0; // Will be set below
512 CallCtx.NumArgs = CE->getNumArgs();
513 CallCtx.FunArgs = CE->getArgs();
514 } else if (D && isa<CXXDestructorDecl>(D)) {
515 // There's no such thing as a "destructor call" in the AST.
516 CallCtx.SelfArg = DeclExp;
519 // Hack to handle constructors, where self cannot be recovered from
521 if (SelfDecl && !CallCtx.SelfArg) {
522 DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue,
523 SelfDecl->getLocation());
524 CallCtx.SelfArg = &SelfDRE;
526 // If the attribute has no arguments, then assume the argument is "this".
528 buildSExpr(CallCtx.SelfArg, 0);
529 else // For most attributes.
530 buildSExpr(MutexExp, &CallCtx);
534 // If the attribute has no arguments, then assume the argument is "this".
536 buildSExpr(CallCtx.SelfArg, 0);
537 else // For most attributes.
538 buildSExpr(MutexExp, &CallCtx);
541 /// \brief Get index of next sibling of node i.
542 unsigned getNextSibling(unsigned i) const {
543 return i + NodeVec[i].size();
547 explicit SExpr(clang::Decl::EmptyShell e) { NodeVec.clear(); }
549 /// \param MutexExp The original mutex expression within an attribute
550 /// \param DeclExp An expression involving the Decl on which the attribute
552 /// \param D The declaration to which the lock/unlock attribute is attached.
553 /// Caller must check isValid() after construction.
554 SExpr(const Expr* MutexExp, const Expr *DeclExp, const NamedDecl* D,
555 VarDecl *SelfDecl=0) {
556 buildSExprFromExpr(MutexExp, DeclExp, D, SelfDecl);
559 /// Return true if this is a valid decl sequence.
560 /// Caller must call this by hand after construction to handle errors.
561 bool isValid() const {
562 return !NodeVec.empty();
565 bool shouldIgnore() const {
566 // Nop is a mutex that we have decided to deliberately ignore.
567 assert(NodeVec.size() > 0 && "Invalid Mutex");
568 return NodeVec[0].kind() == EOP_Nop;
571 bool isUniversal() const {
572 assert(NodeVec.size() > 0 && "Invalid Mutex");
573 return NodeVec[0].kind() == EOP_Universal;
576 /// Issue a warning about an invalid lock expression
577 static void warnInvalidLock(ThreadSafetyHandler &Handler,
578 const Expr *MutexExp,
579 const Expr *DeclExp, const NamedDecl* D) {
582 Loc = DeclExp->getExprLoc();
584 // FIXME: add a note about the attribute location in MutexExp or D
586 Handler.handleInvalidLockExp(Loc);
589 bool operator==(const SExpr &other) const {
590 return NodeVec == other.NodeVec;
593 bool operator!=(const SExpr &other) const {
594 return !(*this == other);
597 bool matches(const SExpr &Other, unsigned i = 0, unsigned j = 0) const {
598 if (NodeVec[i].matches(Other.NodeVec[j])) {
599 unsigned ni = NodeVec[i].arity();
600 unsigned nj = Other.NodeVec[j].arity();
601 unsigned n = (ni < nj) ? ni : nj;
603 unsigned ci = i+1; // first child of i
604 unsigned cj = j+1; // first child of j
605 for (unsigned k = 0; k < n;
606 ++k, ci=getNextSibling(ci), cj = Other.getNextSibling(cj)) {
607 Result = Result && matches(Other, ci, cj);
614 // A partial match between a.mu and b.mu returns true a and b have the same
615 // type (and thus mu refers to the same mutex declaration), regardless of
616 // whether a and b are different objects or not.
617 bool partiallyMatches(const SExpr &Other) const {
618 if (NodeVec[0].kind() == EOP_Dot)
619 return NodeVec[0].matches(Other.NodeVec[0]);
623 /// \brief Pretty print a lock expression for use in error messages.
624 std::string toString(unsigned i = 0) const {
626 if (i >= NodeVec.size())
629 const SExprNode* N = &NodeVec[i];
641 return N->getNamedDecl()->getNameAsString();
644 if (NodeVec[i+1].kind() == EOP_Wildcard) {
646 S += N->getNamedDecl()->getQualifiedNameAsString();
649 std::string FieldName = N->getNamedDecl()->getNameAsString();
650 if (NodeVec[i+1].kind() == EOP_This)
653 std::string S = toString(i+1);
655 return S + "->" + FieldName;
657 return S + "." + FieldName;
660 std::string S = toString(i+1) + "(";
661 unsigned NumArgs = N->arity()-1;
662 unsigned ci = getNextSibling(i+1);
663 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
665 if (k+1 < NumArgs) S += ",";
672 if (NodeVec[i+1].kind() != EOP_This)
673 S = toString(i+1) + ".";
674 if (const NamedDecl *D = N->getFunctionDecl())
675 S += D->getNameAsString() + "(";
678 unsigned NumArgs = N->arity()-1;
679 unsigned ci = getNextSibling(i+1);
680 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) {
682 if (k+1 < NumArgs) S += ",";
688 std::string S1 = toString(i+1);
689 std::string S2 = toString(i+1 + NodeVec[i+1].size());
690 return S1 + "[" + S2 + "]";
693 std::string S = toString(i+1);
697 std::string S1 = toString(i+1);
698 std::string S2 = toString(i+1 + NodeVec[i+1].size());
699 return "(" + S1 + "#" + S2 + ")";
702 unsigned NumChildren = N->arity();
703 if (NumChildren == 0)
707 for (unsigned j = 0; j < NumChildren; ++j, ci = getNextSibling(ci)) {
709 if (j+1 < NumChildren) S += "#";
721 /// \brief A short list of SExprs
722 class MutexIDList : public SmallVector<SExpr, 3> {
724 /// \brief Return true if the list contains the specified SExpr
725 /// Performs a linear search, because these lists are almost always very small.
726 bool contains(const SExpr& M) {
727 for (iterator I=begin(),E=end(); I != E; ++I)
728 if ((*I) == M) return true;
732 /// \brief Push M onto list, bud discard duplicates
733 void push_back_nodup(const SExpr& M) {
734 if (!contains(M)) push_back(M);
740 /// \brief This is a helper class that stores info about the most recent
741 /// accquire of a Lock.
743 /// The main body of the analysis maps MutexIDs to LockDatas.
745 SourceLocation AcquireLoc;
747 /// \brief LKind stores whether a lock is held shared or exclusively.
748 /// Note that this analysis does not currently support either re-entrant
749 /// locking or lock "upgrading" and "downgrading" between exclusive and
752 /// FIXME: add support for re-entrant locking and lock up/downgrading
754 bool Asserted; // for asserted locks
755 bool Managed; // for ScopedLockable objects
756 SExpr UnderlyingMutex; // for ScopedLockable objects
758 LockData(SourceLocation AcquireLoc, LockKind LKind, bool M=false,
760 : AcquireLoc(AcquireLoc), LKind(LKind), Asserted(Asrt), Managed(M),
761 UnderlyingMutex(Decl::EmptyShell())
764 LockData(SourceLocation AcquireLoc, LockKind LKind, const SExpr &Mu)
765 : AcquireLoc(AcquireLoc), LKind(LKind), Asserted(false), Managed(false),
769 bool operator==(const LockData &other) const {
770 return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
773 bool operator!=(const LockData &other) const {
774 return !(*this == other);
777 void Profile(llvm::FoldingSetNodeID &ID) const {
778 ID.AddInteger(AcquireLoc.getRawEncoding());
779 ID.AddInteger(LKind);
782 bool isAtLeast(LockKind LK) {
783 return (LK == LK_Shared) || (LKind == LK_Exclusive);
788 /// \brief A FactEntry stores a single fact that is known at a particular point
789 /// in the program execution. Currently, this is information regarding a lock
790 /// that is held at that point.
795 FactEntry(const SExpr& M, const LockData& L)
801 typedef unsigned short FactID;
803 /// \brief FactManager manages the memory for all facts that are created during
804 /// the analysis of a single routine.
807 std::vector<FactEntry> Facts;
810 FactID newLock(const SExpr& M, const LockData& L) {
811 Facts.push_back(FactEntry(M,L));
812 return static_cast<unsigned short>(Facts.size() - 1);
815 const FactEntry& operator[](FactID F) const { return Facts[F]; }
816 FactEntry& operator[](FactID F) { return Facts[F]; }
820 /// \brief A FactSet is the set of facts that are known to be true at a
821 /// particular program point. FactSets must be small, because they are
822 /// frequently copied, and are thus implemented as a set of indices into a
823 /// table maintained by a FactManager. A typical FactSet only holds 1 or 2
824 /// locks, so we can get away with doing a linear search for lookup. Note
825 /// that a hashtable or map is inappropriate in this case, because lookups
826 /// may involve partial pattern matches, rather than exact matches.
829 typedef SmallVector<FactID, 4> FactVec;
834 typedef FactVec::iterator iterator;
835 typedef FactVec::const_iterator const_iterator;
837 iterator begin() { return FactIDs.begin(); }
838 const_iterator begin() const { return FactIDs.begin(); }
840 iterator end() { return FactIDs.end(); }
841 const_iterator end() const { return FactIDs.end(); }
843 bool isEmpty() const { return FactIDs.size() == 0; }
845 FactID addLock(FactManager& FM, const SExpr& M, const LockData& L) {
846 FactID F = FM.newLock(M, L);
847 FactIDs.push_back(F);
851 bool removeLock(FactManager& FM, const SExpr& M) {
852 unsigned n = FactIDs.size();
856 for (unsigned i = 0; i < n-1; ++i) {
857 if (FM[FactIDs[i]].MutID.matches(M)) {
858 FactIDs[i] = FactIDs[n-1];
863 if (FM[FactIDs[n-1]].MutID.matches(M)) {
870 // Returns an iterator
871 iterator findLockIter(FactManager &FM, const SExpr &M) {
872 for (iterator I = begin(), E = end(); I != E; ++I) {
873 const SExpr &Exp = FM[*I].MutID;
880 LockData* findLock(FactManager &FM, const SExpr &M) const {
881 for (const_iterator I = begin(), E = end(); I != E; ++I) {
882 const SExpr &Exp = FM[*I].MutID;
889 LockData* findLockUniv(FactManager &FM, const SExpr &M) const {
890 for (const_iterator I = begin(), E = end(); I != E; ++I) {
891 const SExpr &Exp = FM[*I].MutID;
892 if (Exp.matches(M) || Exp.isUniversal())
898 FactEntry* findPartialMatch(FactManager &FM, const SExpr &M) const {
899 for (const_iterator I=begin(), E=end(); I != E; ++I) {
900 const SExpr& Exp = FM[*I].MutID;
901 if (Exp.partiallyMatches(M)) return &FM[*I];
909 /// A Lockset maps each SExpr (defined above) to information about how it has
911 typedef llvm::ImmutableMap<SExpr, LockData> Lockset;
912 typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
914 class LocalVariableMap;
916 /// A side (entry or exit) of a CFG node.
917 enum CFGBlockSide { CBS_Entry, CBS_Exit };
919 /// CFGBlockInfo is a struct which contains all the information that is
920 /// maintained for each block in the CFG. See LocalVariableMap for more
921 /// information about the contexts.
922 struct CFGBlockInfo {
923 FactSet EntrySet; // Lockset held at entry to block
924 FactSet ExitSet; // Lockset held at exit from block
925 LocalVarContext EntryContext; // Context held at entry to block
926 LocalVarContext ExitContext; // Context held at exit from block
927 SourceLocation EntryLoc; // Location of first statement in block
928 SourceLocation ExitLoc; // Location of last statement in block.
929 unsigned EntryIndex; // Used to replay contexts later
930 bool Reachable; // Is this block reachable?
932 const FactSet &getSet(CFGBlockSide Side) const {
933 return Side == CBS_Entry ? EntrySet : ExitSet;
935 SourceLocation getLocation(CFGBlockSide Side) const {
936 return Side == CBS_Entry ? EntryLoc : ExitLoc;
940 CFGBlockInfo(LocalVarContext EmptyCtx)
941 : EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false)
945 static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M);
950 // A LocalVariableMap maintains a map from local variables to their currently
951 // valid definitions. It provides SSA-like functionality when traversing the
952 // CFG. Like SSA, each definition or assignment to a variable is assigned a
953 // unique name (an integer), which acts as the SSA name for that definition.
954 // The total set of names is shared among all CFG basic blocks.
955 // Unlike SSA, we do not rewrite expressions to replace local variables declrefs
956 // with their SSA-names. Instead, we compute a Context for each point in the
957 // code, which maps local variables to the appropriate SSA-name. This map
958 // changes with each assignment.
960 // The map is computed in a single pass over the CFG. Subsequent analyses can
961 // then query the map to find the appropriate Context for a statement, and use
962 // that Context to look up the definitions of variables.
963 class LocalVariableMap {
965 typedef LocalVarContext Context;
967 /// A VarDefinition consists of an expression, representing the value of the
968 /// variable, along with the context in which that expression should be
969 /// interpreted. A reference VarDefinition does not itself contain this
970 /// information, but instead contains a pointer to a previous VarDefinition.
971 struct VarDefinition {
973 friend class LocalVariableMap;
975 const NamedDecl *Dec; // The original declaration for this variable.
976 const Expr *Exp; // The expression for this variable, OR
977 unsigned Ref; // Reference to another VarDefinition
978 Context Ctx; // The map with which Exp should be interpreted.
980 bool isReference() { return !Exp; }
983 // Create ordinary variable definition
984 VarDefinition(const NamedDecl *D, const Expr *E, Context C)
985 : Dec(D), Exp(E), Ref(0), Ctx(C)
988 // Create reference to previous definition
989 VarDefinition(const NamedDecl *D, unsigned R, Context C)
990 : Dec(D), Exp(0), Ref(R), Ctx(C)
995 Context::Factory ContextFactory;
996 std::vector<VarDefinition> VarDefinitions;
997 std::vector<unsigned> CtxIndices;
998 std::vector<std::pair<Stmt*, Context> > SavedContexts;
1001 LocalVariableMap() {
1002 // index 0 is a placeholder for undefined variables (aka phi-nodes).
1003 VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext()));
1006 /// Look up a definition, within the given context.
1007 const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
1008 const unsigned *i = Ctx.lookup(D);
1011 assert(*i < VarDefinitions.size());
1012 return &VarDefinitions[*i];
1015 /// Look up the definition for D within the given context. Returns
1016 /// NULL if the expression is not statically known. If successful, also
1017 /// modifies Ctx to hold the context of the return Expr.
1018 const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
1019 const unsigned *P = Ctx.lookup(D);
1025 if (VarDefinitions[i].Exp) {
1026 Ctx = VarDefinitions[i].Ctx;
1027 return VarDefinitions[i].Exp;
1029 i = VarDefinitions[i].Ref;
1034 Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
1036 /// Return the next context after processing S. This function is used by
1037 /// clients of the class to get the appropriate context when traversing the
1038 /// CFG. It must be called for every assignment or DeclStmt.
1039 Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
1040 if (SavedContexts[CtxIndex+1].first == S) {
1042 Context Result = SavedContexts[CtxIndex].second;
1048 void dumpVarDefinitionName(unsigned i) {
1050 llvm::errs() << "Undefined";
1053 const NamedDecl *Dec = VarDefinitions[i].Dec;
1055 llvm::errs() << "<<NULL>>";
1058 Dec->printName(llvm::errs());
1059 llvm::errs() << "." << i << " " << ((const void*) Dec);
1062 /// Dumps an ASCII representation of the variable map to llvm::errs()
1064 for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
1065 const Expr *Exp = VarDefinitions[i].Exp;
1066 unsigned Ref = VarDefinitions[i].Ref;
1068 dumpVarDefinitionName(i);
1069 llvm::errs() << " = ";
1070 if (Exp) Exp->dump();
1072 dumpVarDefinitionName(Ref);
1073 llvm::errs() << "\n";
1078 /// Dumps an ASCII representation of a Context to llvm::errs()
1079 void dumpContext(Context C) {
1080 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
1081 const NamedDecl *D = I.getKey();
1082 D->printName(llvm::errs());
1083 const unsigned *i = C.lookup(D);
1084 llvm::errs() << " -> ";
1085 dumpVarDefinitionName(*i);
1086 llvm::errs() << "\n";
1090 /// Builds the variable map.
1091 void traverseCFG(CFG *CFGraph, PostOrderCFGView *SortedGraph,
1092 std::vector<CFGBlockInfo> &BlockInfo);
1095 // Get the current context index
1096 unsigned getContextIndex() { return SavedContexts.size()-1; }
1098 // Save the current context for later replay
1099 void saveContext(Stmt *S, Context C) {
1100 SavedContexts.push_back(std::make_pair(S,C));
1103 // Adds a new definition to the given context, and returns a new context.
1104 // This method should be called when declaring a new variable.
1105 Context addDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
1106 assert(!Ctx.contains(D));
1107 unsigned newID = VarDefinitions.size();
1108 Context NewCtx = ContextFactory.add(Ctx, D, newID);
1109 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1113 // Add a new reference to an existing definition.
1114 Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
1115 unsigned newID = VarDefinitions.size();
1116 Context NewCtx = ContextFactory.add(Ctx, D, newID);
1117 VarDefinitions.push_back(VarDefinition(D, i, Ctx));
1121 // Updates a definition only if that definition is already in the map.
1122 // This method should be called when assigning to an existing variable.
1123 Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
1124 if (Ctx.contains(D)) {
1125 unsigned newID = VarDefinitions.size();
1126 Context NewCtx = ContextFactory.remove(Ctx, D);
1127 NewCtx = ContextFactory.add(NewCtx, D, newID);
1128 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
1134 // Removes a definition from the context, but keeps the variable name
1135 // as a valid variable. The index 0 is a placeholder for cleared definitions.
1136 Context clearDefinition(const NamedDecl *D, Context Ctx) {
1137 Context NewCtx = Ctx;
1138 if (NewCtx.contains(D)) {
1139 NewCtx = ContextFactory.remove(NewCtx, D);
1140 NewCtx = ContextFactory.add(NewCtx, D, 0);
1145 // Remove a definition entirely frmo the context.
1146 Context removeDefinition(const NamedDecl *D, Context Ctx) {
1147 Context NewCtx = Ctx;
1148 if (NewCtx.contains(D)) {
1149 NewCtx = ContextFactory.remove(NewCtx, D);
1154 Context intersectContexts(Context C1, Context C2);
1155 Context createReferenceContext(Context C);
1156 void intersectBackEdge(Context C1, Context C2);
1158 friend class VarMapBuilder;
1162 // This has to be defined after LocalVariableMap.
1163 CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) {
1164 return CFGBlockInfo(M.getEmptyContext());
1168 /// Visitor which builds a LocalVariableMap
1169 class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
1171 LocalVariableMap* VMap;
1172 LocalVariableMap::Context Ctx;
1174 VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
1175 : VMap(VM), Ctx(C) {}
1177 void VisitDeclStmt(DeclStmt *S);
1178 void VisitBinaryOperator(BinaryOperator *BO);
1182 // Add new local variables to the variable map
1183 void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
1184 bool modifiedCtx = false;
1185 DeclGroupRef DGrp = S->getDeclGroup();
1186 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
1187 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
1188 Expr *E = VD->getInit();
1190 // Add local variables with trivial type to the variable map
1191 QualType T = VD->getType();
1192 if (T.isTrivialType(VD->getASTContext())) {
1193 Ctx = VMap->addDefinition(VD, E, Ctx);
1199 VMap->saveContext(S, Ctx);
1202 // Update local variable definitions in variable map
1203 void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
1204 if (!BO->isAssignmentOp())
1207 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
1209 // Update the variable map and current context.
1210 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
1211 ValueDecl *VDec = DRE->getDecl();
1212 if (Ctx.lookup(VDec)) {
1213 if (BO->getOpcode() == BO_Assign)
1214 Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
1216 // FIXME -- handle compound assignment operators
1217 Ctx = VMap->clearDefinition(VDec, Ctx);
1218 VMap->saveContext(BO, Ctx);
1224 // Computes the intersection of two contexts. The intersection is the
1225 // set of variables which have the same definition in both contexts;
1226 // variables with different definitions are discarded.
1227 LocalVariableMap::Context
1228 LocalVariableMap::intersectContexts(Context C1, Context C2) {
1229 Context Result = C1;
1230 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
1231 const NamedDecl *Dec = I.getKey();
1232 unsigned i1 = I.getData();
1233 const unsigned *i2 = C2.lookup(Dec);
1234 if (!i2) // variable doesn't exist on second path
1235 Result = removeDefinition(Dec, Result);
1236 else if (*i2 != i1) // variable exists, but has different definition
1237 Result = clearDefinition(Dec, Result);
1242 // For every variable in C, create a new variable that refers to the
1243 // definition in C. Return a new context that contains these new variables.
1244 // (We use this for a naive implementation of SSA on loop back-edges.)
1245 LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
1246 Context Result = getEmptyContext();
1247 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
1248 const NamedDecl *Dec = I.getKey();
1249 unsigned i = I.getData();
1250 Result = addReference(Dec, i, Result);
1255 // This routine also takes the intersection of C1 and C2, but it does so by
1256 // altering the VarDefinitions. C1 must be the result of an earlier call to
1257 // createReferenceContext.
1258 void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
1259 for (Context::iterator I = C1.begin(), E = C1.end(); I != E; ++I) {
1260 const NamedDecl *Dec = I.getKey();
1261 unsigned i1 = I.getData();
1262 VarDefinition *VDef = &VarDefinitions[i1];
1263 assert(VDef->isReference());
1265 const unsigned *i2 = C2.lookup(Dec);
1266 if (!i2 || (*i2 != i1))
1267 VDef->Ref = 0; // Mark this variable as undefined
1272 // Traverse the CFG in topological order, so all predecessors of a block
1273 // (excluding back-edges) are visited before the block itself. At
1274 // each point in the code, we calculate a Context, which holds the set of
1275 // variable definitions which are visible at that point in execution.
1276 // Visible variables are mapped to their definitions using an array that
1277 // contains all definitions.
1279 // At join points in the CFG, the set is computed as the intersection of
1280 // the incoming sets along each edge, E.g.
1282 // { Context | VarDefinitions }
1283 // int x = 0; { x -> x1 | x1 = 0 }
1284 // int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
1285 // if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... }
1286 // else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... }
1287 // ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... }
1289 // This is essentially a simpler and more naive version of the standard SSA
1290 // algorithm. Those definitions that remain in the intersection are from blocks
1291 // that strictly dominate the current block. We do not bother to insert proper
1292 // phi nodes, because they are not used in our analysis; instead, wherever
1293 // a phi node would be required, we simply remove that definition from the
1294 // context (E.g. x above).
1296 // The initial traversal does not capture back-edges, so those need to be
1297 // handled on a separate pass. Whenever the first pass encounters an
1298 // incoming back edge, it duplicates the context, creating new definitions
1299 // that refer back to the originals. (These correspond to places where SSA
1300 // might have to insert a phi node.) On the second pass, these definitions are
1301 // set to NULL if the variable has changed on the back-edge (i.e. a phi
1302 // node was actually required.) E.g.
1304 // { Context | VarDefinitions }
1305 // int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
1306 // while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; }
1307 // x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... }
1308 // ... { y -> y1 | x3 = 2, x2 = 1, ... }
1310 void LocalVariableMap::traverseCFG(CFG *CFGraph,
1311 PostOrderCFGView *SortedGraph,
1312 std::vector<CFGBlockInfo> &BlockInfo) {
1313 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
1315 CtxIndices.resize(CFGraph->getNumBlockIDs());
1317 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1318 E = SortedGraph->end(); I!= E; ++I) {
1319 const CFGBlock *CurrBlock = *I;
1320 int CurrBlockID = CurrBlock->getBlockID();
1321 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
1323 VisitedBlocks.insert(CurrBlock);
1325 // Calculate the entry context for the current block
1326 bool HasBackEdges = false;
1327 bool CtxInit = true;
1328 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
1329 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
1330 // if *PI -> CurrBlock is a back edge, so skip it
1331 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) {
1332 HasBackEdges = true;
1336 int PrevBlockID = (*PI)->getBlockID();
1337 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
1340 CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
1344 CurrBlockInfo->EntryContext =
1345 intersectContexts(CurrBlockInfo->EntryContext,
1346 PrevBlockInfo->ExitContext);
1350 // Duplicate the context if we have back-edges, so we can call
1351 // intersectBackEdges later.
1353 CurrBlockInfo->EntryContext =
1354 createReferenceContext(CurrBlockInfo->EntryContext);
1356 // Create a starting context index for the current block
1357 saveContext(0, CurrBlockInfo->EntryContext);
1358 CurrBlockInfo->EntryIndex = getContextIndex();
1360 // Visit all the statements in the basic block.
1361 VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
1362 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1363 BE = CurrBlock->end(); BI != BE; ++BI) {
1364 switch (BI->getKind()) {
1365 case CFGElement::Statement: {
1366 CFGStmt CS = BI->castAs<CFGStmt>();
1367 VMapBuilder.Visit(const_cast<Stmt*>(CS.getStmt()));
1374 CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
1376 // Mark variables on back edges as "unknown" if they've been changed.
1377 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1378 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1379 // if CurrBlock -> *SI is *not* a back edge
1380 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
1383 CFGBlock *FirstLoopBlock = *SI;
1384 Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
1385 Context LoopEnd = CurrBlockInfo->ExitContext;
1386 intersectBackEdge(LoopBegin, LoopEnd);
1390 // Put an extra entry at the end of the indexed context array
1391 unsigned exitID = CFGraph->getExit().getBlockID();
1392 saveContext(0, BlockInfo[exitID].ExitContext);
1395 /// Find the appropriate source locations to use when producing diagnostics for
1396 /// each block in the CFG.
1397 static void findBlockLocations(CFG *CFGraph,
1398 PostOrderCFGView *SortedGraph,
1399 std::vector<CFGBlockInfo> &BlockInfo) {
1400 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1401 E = SortedGraph->end(); I!= E; ++I) {
1402 const CFGBlock *CurrBlock = *I;
1403 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
1405 // Find the source location of the last statement in the block, if the
1406 // block is not empty.
1407 if (const Stmt *S = CurrBlock->getTerminator()) {
1408 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
1410 for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
1411 BE = CurrBlock->rend(); BI != BE; ++BI) {
1412 // FIXME: Handle other CFGElement kinds.
1413 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) {
1414 CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
1420 if (!CurrBlockInfo->ExitLoc.isInvalid()) {
1421 // This block contains at least one statement. Find the source location
1422 // of the first statement in the block.
1423 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1424 BE = CurrBlock->end(); BI != BE; ++BI) {
1425 // FIXME: Handle other CFGElement kinds.
1426 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) {
1427 CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
1431 } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
1432 CurrBlock != &CFGraph->getExit()) {
1433 // The block is empty, and has a single predecessor. Use its exit
1435 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
1436 BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
1441 /// \brief Class which implements the core thread safety analysis routines.
1442 class ThreadSafetyAnalyzer {
1443 friend class BuildLockset;
1445 ThreadSafetyHandler &Handler;
1446 LocalVariableMap LocalVarMap;
1447 FactManager FactMan;
1448 std::vector<CFGBlockInfo> BlockInfo;
1451 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {}
1453 void addLock(FactSet &FSet, const SExpr &Mutex, const LockData &LDat);
1454 void removeLock(FactSet &FSet, const SExpr &Mutex,
1455 SourceLocation UnlockLoc, bool FullyRemove=false);
1457 template <typename AttrType>
1458 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1459 const NamedDecl *D, VarDecl *SelfDecl=0);
1461 template <class AttrType>
1462 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
1464 const CFGBlock *PredBlock, const CFGBlock *CurrBlock,
1465 Expr *BrE, bool Neg);
1467 const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
1470 void getEdgeLockset(FactSet &Result, const FactSet &ExitSet,
1471 const CFGBlock* PredBlock,
1472 const CFGBlock *CurrBlock);
1474 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1475 SourceLocation JoinLoc,
1476 LockErrorKind LEK1, LockErrorKind LEK2,
1479 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
1480 SourceLocation JoinLoc, LockErrorKind LEK1,
1482 intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify);
1485 void runAnalysis(AnalysisDeclContext &AC);
1489 /// \brief Add a new lock to the lockset, warning if the lock is already there.
1490 /// \param Mutex -- the Mutex expression for the lock
1491 /// \param LDat -- the LockData for the lock
1492 void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const SExpr &Mutex,
1493 const LockData &LDat) {
1494 // FIXME: deal with acquired before/after annotations.
1495 // FIXME: Don't always warn when we have support for reentrant locks.
1496 if (Mutex.shouldIgnore())
1499 if (FSet.findLock(FactMan, Mutex)) {
1501 Handler.handleDoubleLock(Mutex.toString(), LDat.AcquireLoc);
1503 FSet.addLock(FactMan, Mutex, LDat);
1508 /// \brief Remove a lock from the lockset, warning if the lock is not there.
1509 /// \param Mutex The lock expression corresponding to the lock to be removed
1510 /// \param UnlockLoc The source location of the unlock (only used in error msg)
1511 void ThreadSafetyAnalyzer::removeLock(FactSet &FSet,
1513 SourceLocation UnlockLoc,
1515 if (Mutex.shouldIgnore())
1518 const LockData *LDat = FSet.findLock(FactMan, Mutex);
1520 Handler.handleUnmatchedUnlock(Mutex.toString(), UnlockLoc);
1524 if (LDat->UnderlyingMutex.isValid()) {
1525 // This is scoped lockable object, which manages the real mutex.
1527 // We're destroying the managing object.
1528 // Remove the underlying mutex if it exists; but don't warn.
1529 if (FSet.findLock(FactMan, LDat->UnderlyingMutex))
1530 FSet.removeLock(FactMan, LDat->UnderlyingMutex);
1532 // We're releasing the underlying mutex, but not destroying the
1533 // managing object. Warn on dual release.
1534 if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) {
1535 Handler.handleUnmatchedUnlock(LDat->UnderlyingMutex.toString(),
1538 FSet.removeLock(FactMan, LDat->UnderlyingMutex);
1542 FSet.removeLock(FactMan, Mutex);
1546 /// \brief Extract the list of mutexIDs from the attribute on an expression,
1547 /// and push them onto Mtxs, discarding any duplicates.
1548 template <typename AttrType>
1549 void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1550 Expr *Exp, const NamedDecl *D,
1551 VarDecl *SelfDecl) {
1552 typedef typename AttrType::args_iterator iterator_type;
1554 if (Attr->args_size() == 0) {
1555 // The mutex held is the "this" object.
1556 SExpr Mu(0, Exp, D, SelfDecl);
1558 SExpr::warnInvalidLock(Handler, 0, Exp, D);
1560 Mtxs.push_back_nodup(Mu);
1564 for (iterator_type I=Attr->args_begin(), E=Attr->args_end(); I != E; ++I) {
1565 SExpr Mu(*I, Exp, D, SelfDecl);
1567 SExpr::warnInvalidLock(Handler, *I, Exp, D);
1569 Mtxs.push_back_nodup(Mu);
1574 /// \brief Extract the list of mutexIDs from a trylock attribute. If the
1575 /// trylock applies to the given edge, then push them onto Mtxs, discarding
1577 template <class AttrType>
1578 void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
1579 Expr *Exp, const NamedDecl *D,
1580 const CFGBlock *PredBlock,
1581 const CFGBlock *CurrBlock,
1582 Expr *BrE, bool Neg) {
1583 // Find out which branch has the lock
1585 if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) {
1586 branch = BLE->getValue();
1588 else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) {
1589 branch = ILE->getValue().getBoolValue();
1591 int branchnum = branch ? 0 : 1;
1592 if (Neg) branchnum = !branchnum;
1594 // If we've taken the trylock branch, then add the lock
1596 for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
1597 SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
1598 if (*SI == CurrBlock && i == branchnum) {
1599 getMutexIDs(Mtxs, Attr, Exp, D);
1605 bool getStaticBooleanValue(Expr* E, bool& TCond) {
1606 if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) {
1609 } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) {
1610 TCond = BLE->getValue();
1612 } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) {
1613 TCond = ILE->getValue().getBoolValue();
1615 } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
1616 return getStaticBooleanValue(CE->getSubExpr(), TCond);
1622 // If Cond can be traced back to a function call, return the call expression.
1623 // The negate variable should be called with false, and will be set to true
1624 // if the function call is negated, e.g. if (!mu.tryLock(...))
1625 const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
1631 if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
1634 else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) {
1635 return getTrylockCallExpr(PE->getSubExpr(), C, Negate);
1637 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
1638 return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
1640 else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) {
1641 return getTrylockCallExpr(EWC->getSubExpr(), C, Negate);
1643 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
1644 const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
1645 return getTrylockCallExpr(E, C, Negate);
1647 else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
1648 if (UOP->getOpcode() == UO_LNot) {
1650 return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
1654 else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) {
1655 if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) {
1656 if (BOP->getOpcode() == BO_NE)
1660 if (getStaticBooleanValue(BOP->getRHS(), TCond)) {
1661 if (!TCond) Negate = !Negate;
1662 return getTrylockCallExpr(BOP->getLHS(), C, Negate);
1665 if (getStaticBooleanValue(BOP->getLHS(), TCond)) {
1666 if (!TCond) Negate = !Negate;
1667 return getTrylockCallExpr(BOP->getRHS(), C, Negate);
1671 if (BOP->getOpcode() == BO_LAnd) {
1672 // LHS must have been evaluated in a different block.
1673 return getTrylockCallExpr(BOP->getRHS(), C, Negate);
1675 if (BOP->getOpcode() == BO_LOr) {
1676 return getTrylockCallExpr(BOP->getRHS(), C, Negate);
1684 /// \brief Find the lockset that holds on the edge between PredBlock
1685 /// and CurrBlock. The edge set is the exit set of PredBlock (passed
1686 /// as the ExitSet parameter) plus any trylocks, which are conditionally held.
1687 void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result,
1688 const FactSet &ExitSet,
1689 const CFGBlock *PredBlock,
1690 const CFGBlock *CurrBlock) {
1693 const Stmt *Cond = PredBlock->getTerminatorCondition();
1697 bool Negate = false;
1698 const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
1699 const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
1702 const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate));
1706 NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
1707 if(!FunDecl || !FunDecl->hasAttrs())
1710 MutexIDList ExclusiveLocksToAdd;
1711 MutexIDList SharedLocksToAdd;
1713 // If the condition is a call to a Trylock function, then grab the attributes
1714 AttrVec &ArgAttrs = FunDecl->getAttrs();
1715 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
1716 Attr *Attr = ArgAttrs[i];
1717 switch (Attr->getKind()) {
1718 case attr::ExclusiveTrylockFunction: {
1719 ExclusiveTrylockFunctionAttr *A =
1720 cast<ExclusiveTrylockFunctionAttr>(Attr);
1721 getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl,
1722 PredBlock, CurrBlock, A->getSuccessValue(), Negate);
1725 case attr::SharedTrylockFunction: {
1726 SharedTrylockFunctionAttr *A =
1727 cast<SharedTrylockFunctionAttr>(Attr);
1728 getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl,
1729 PredBlock, CurrBlock, A->getSuccessValue(), Negate);
1737 // Add and remove locks.
1738 SourceLocation Loc = Exp->getExprLoc();
1739 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
1740 addLock(Result, ExclusiveLocksToAdd[i],
1741 LockData(Loc, LK_Exclusive));
1743 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
1744 addLock(Result, SharedLocksToAdd[i],
1745 LockData(Loc, LK_Shared));
1750 /// \brief We use this class to visit different types of expressions in
1751 /// CFGBlocks, and build up the lockset.
1752 /// An expression may cause us to add or remove locks from the lockset, or else
1753 /// output error messages related to missing locks.
1754 /// FIXME: In future, we may be able to not inherit from a visitor.
1755 class BuildLockset : public StmtVisitor<BuildLockset> {
1756 friend class ThreadSafetyAnalyzer;
1758 ThreadSafetyAnalyzer *Analyzer;
1760 LocalVariableMap::Context LVarCtx;
1764 const ValueDecl *getValueDecl(const Expr *Exp);
1766 void warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, AccessKind AK,
1767 Expr *MutexExp, ProtectedOperationKind POK);
1768 void warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, Expr *MutexExp);
1770 void checkAccess(const Expr *Exp, AccessKind AK);
1771 void checkPtAccess(const Expr *Exp, AccessKind AK);
1773 void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = 0);
1776 BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
1777 : StmtVisitor<BuildLockset>(),
1779 FSet(Info.EntrySet),
1780 LVarCtx(Info.EntryContext),
1781 CtxIndex(Info.EntryIndex)
1784 void VisitUnaryOperator(UnaryOperator *UO);
1785 void VisitBinaryOperator(BinaryOperator *BO);
1786 void VisitCastExpr(CastExpr *CE);
1787 void VisitCallExpr(CallExpr *Exp);
1788 void VisitCXXConstructExpr(CXXConstructExpr *Exp);
1789 void VisitDeclStmt(DeclStmt *S);
1793 /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs
1794 const ValueDecl *BuildLockset::getValueDecl(const Expr *Exp) {
1795 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Exp))
1796 return getValueDecl(CE->getSubExpr());
1798 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Exp))
1799 return DR->getDecl();
1801 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp))
1802 return ME->getMemberDecl();
1807 /// \brief Warn if the LSet does not contain a lock sufficient to protect access
1808 /// of at least the passed in AccessKind.
1809 void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp,
1810 AccessKind AK, Expr *MutexExp,
1811 ProtectedOperationKind POK) {
1812 LockKind LK = getLockKindFromAccessKind(AK);
1814 SExpr Mutex(MutexExp, Exp, D);
1815 if (!Mutex.isValid()) {
1816 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
1818 } else if (Mutex.shouldIgnore()) {
1822 LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex);
1823 bool NoError = true;
1825 // No exact match found. Look for a partial match.
1826 FactEntry* FEntry = FSet.findPartialMatch(Analyzer->FactMan, Mutex);
1828 // Warn that there's no precise match.
1829 LDat = &FEntry->LDat;
1830 std::string PartMatchStr = FEntry->MutID.toString();
1831 StringRef PartMatchName(PartMatchStr);
1832 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1833 Exp->getExprLoc(), &PartMatchName);
1835 // Warn that there's no match at all.
1836 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1841 // Make sure the mutex we found is the right kind.
1842 if (NoError && LDat && !LDat->isAtLeast(LK))
1843 Analyzer->Handler.handleMutexNotHeld(D, POK, Mutex.toString(), LK,
1847 /// \brief Warn if the LSet contains the given lock.
1848 void BuildLockset::warnIfMutexHeld(const NamedDecl *D, const Expr* Exp,
1850 SExpr Mutex(MutexExp, Exp, D);
1851 if (!Mutex.isValid()) {
1852 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D);
1856 LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex);
1858 std::string DeclName = D->getNameAsString();
1859 StringRef DeclNameSR (DeclName);
1860 Analyzer->Handler.handleFunExcludesLock(DeclNameSR, Mutex.toString(),
1866 /// \brief Checks guarded_by and pt_guarded_by attributes.
1867 /// Whenever we identify an access (read or write) to a DeclRefExpr that is
1868 /// marked with guarded_by, we must ensure the appropriate mutexes are held.
1869 /// Similarly, we check if the access is to an expression that dereferences
1870 /// a pointer marked with pt_guarded_by.
1871 void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK) {
1872 Exp = Exp->IgnoreParenCasts();
1874 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp)) {
1876 if (UO->getOpcode() == clang::UO_Deref)
1877 checkPtAccess(UO->getSubExpr(), AK);
1881 if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(Exp)) {
1882 if (Analyzer->Handler.issueBetaWarnings()) {
1883 checkPtAccess(AE->getLHS(), AK);
1888 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
1890 checkPtAccess(ME->getBase(), AK);
1892 checkAccess(ME->getBase(), AK);
1895 const ValueDecl *D = getValueDecl(Exp);
1896 if (!D || !D->hasAttrs())
1899 if (D->getAttr<GuardedVarAttr>() && FSet.isEmpty())
1900 Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK,
1903 const AttrVec &ArgAttrs = D->getAttrs();
1904 for (unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1905 if (GuardedByAttr *GBAttr = dyn_cast<GuardedByAttr>(ArgAttrs[i]))
1906 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarAccess);
1909 /// \brief Checks pt_guarded_by and pt_guarded_var attributes.
1910 void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK) {
1911 if (Analyzer->Handler.issueBetaWarnings()) {
1913 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
1914 Exp = PE->getSubExpr();
1917 if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
1918 if (CE->getCastKind() == CK_ArrayToPointerDecay) {
1919 // If it's an actual array, and not a pointer, then it's elements
1920 // are protected by GUARDED_BY, not PT_GUARDED_BY;
1921 checkAccess(CE->getSubExpr(), AK);
1924 Exp = CE->getSubExpr();
1931 Exp = Exp->IgnoreParenCasts();
1933 const ValueDecl *D = getValueDecl(Exp);
1934 if (!D || !D->hasAttrs())
1937 if (D->getAttr<PtGuardedVarAttr>() && FSet.isEmpty())
1938 Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK,
1941 const AttrVec &ArgAttrs = D->getAttrs();
1942 for (unsigned i = 0, Size = ArgAttrs.size(); i < Size; ++i)
1943 if (PtGuardedByAttr *GBAttr = dyn_cast<PtGuardedByAttr>(ArgAttrs[i]))
1944 warnIfMutexNotHeld(D, Exp, AK, GBAttr->getArg(), POK_VarDereference);
1948 /// \brief Process a function call, method call, constructor call,
1949 /// or destructor call. This involves looking at the attributes on the
1950 /// corresponding function/method/constructor/destructor, issuing warnings,
1951 /// and updating the locksets accordingly.
1953 /// FIXME: For classes annotated with one of the guarded annotations, we need
1954 /// to treat const method calls as reads and non-const method calls as writes,
1955 /// and check that the appropriate locks are held. Non-const method calls with
1956 /// the same signature as const method calls can be also treated as reads.
1958 void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) {
1959 SourceLocation Loc = Exp->getExprLoc();
1960 const AttrVec &ArgAttrs = D->getAttrs();
1961 MutexIDList ExclusiveLocksToAdd;
1962 MutexIDList SharedLocksToAdd;
1963 MutexIDList LocksToRemove;
1965 for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
1966 Attr *At = const_cast<Attr*>(ArgAttrs[i]);
1967 switch (At->getKind()) {
1968 // When we encounter an exclusive lock function, we need to add the lock
1969 // to our lockset with kind exclusive.
1970 case attr::ExclusiveLockFunction: {
1971 ExclusiveLockFunctionAttr *A = cast<ExclusiveLockFunctionAttr>(At);
1972 Analyzer->getMutexIDs(ExclusiveLocksToAdd, A, Exp, D, VD);
1976 // When we encounter a shared lock function, we need to add the lock
1977 // to our lockset with kind shared.
1978 case attr::SharedLockFunction: {
1979 SharedLockFunctionAttr *A = cast<SharedLockFunctionAttr>(At);
1980 Analyzer->getMutexIDs(SharedLocksToAdd, A, Exp, D, VD);
1984 // An assert will add a lock to the lockset, but will not generate
1985 // a warning if it is already there, and will not generate a warning
1986 // if it is not removed.
1987 case attr::AssertExclusiveLock: {
1988 AssertExclusiveLockAttr *A = cast<AssertExclusiveLockAttr>(At);
1990 MutexIDList AssertLocks;
1991 Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
1992 for (unsigned i=0,n=AssertLocks.size(); i<n; ++i) {
1993 Analyzer->addLock(FSet, AssertLocks[i],
1994 LockData(Loc, LK_Exclusive, false, true));
1998 case attr::AssertSharedLock: {
1999 AssertSharedLockAttr *A = cast<AssertSharedLockAttr>(At);
2001 MutexIDList AssertLocks;
2002 Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
2003 for (unsigned i=0,n=AssertLocks.size(); i<n; ++i) {
2004 Analyzer->addLock(FSet, AssertLocks[i],
2005 LockData(Loc, LK_Shared, false, true));
2010 // When we encounter an unlock function, we need to remove unlocked
2011 // mutexes from the lockset, and flag a warning if they are not there.
2012 case attr::UnlockFunction: {
2013 UnlockFunctionAttr *A = cast<UnlockFunctionAttr>(At);
2014 Analyzer->getMutexIDs(LocksToRemove, A, Exp, D, VD);
2018 case attr::ExclusiveLocksRequired: {
2019 ExclusiveLocksRequiredAttr *A = cast<ExclusiveLocksRequiredAttr>(At);
2021 for (ExclusiveLocksRequiredAttr::args_iterator
2022 I = A->args_begin(), E = A->args_end(); I != E; ++I)
2023 warnIfMutexNotHeld(D, Exp, AK_Written, *I, POK_FunctionCall);
2027 case attr::SharedLocksRequired: {
2028 SharedLocksRequiredAttr *A = cast<SharedLocksRequiredAttr>(At);
2030 for (SharedLocksRequiredAttr::args_iterator I = A->args_begin(),
2031 E = A->args_end(); I != E; ++I)
2032 warnIfMutexNotHeld(D, Exp, AK_Read, *I, POK_FunctionCall);
2036 case attr::LocksExcluded: {
2037 LocksExcludedAttr *A = cast<LocksExcludedAttr>(At);
2039 for (LocksExcludedAttr::args_iterator I = A->args_begin(),
2040 E = A->args_end(); I != E; ++I) {
2041 warnIfMutexHeld(D, Exp, *I);
2046 // Ignore other (non thread-safety) attributes
2052 // Figure out if we're calling the constructor of scoped lockable class
2053 bool isScopedVar = false;
2055 if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) {
2056 const CXXRecordDecl* PD = CD->getParent();
2057 if (PD && PD->getAttr<ScopedLockableAttr>())
2063 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
2064 Analyzer->addLock(FSet, ExclusiveLocksToAdd[i],
2065 LockData(Loc, LK_Exclusive, isScopedVar));
2067 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
2068 Analyzer->addLock(FSet, SharedLocksToAdd[i],
2069 LockData(Loc, LK_Shared, isScopedVar));
2072 // Add the managing object as a dummy mutex, mapped to the underlying mutex.
2073 // FIXME -- this doesn't work if we acquire multiple locks.
2075 SourceLocation MLoc = VD->getLocation();
2076 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
2077 SExpr SMutex(&DRE, 0, 0);
2079 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
2080 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive,
2081 ExclusiveLocksToAdd[i]));
2083 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
2084 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared,
2085 SharedLocksToAdd[i]));
2090 // FIXME -- should only fully remove if the attribute refers to 'this'.
2091 bool Dtor = isa<CXXDestructorDecl>(D);
2092 for (unsigned i=0,n=LocksToRemove.size(); i<n; ++i) {
2093 Analyzer->removeLock(FSet, LocksToRemove[i], Loc, Dtor);
2098 /// \brief For unary operations which read and write a variable, we need to
2099 /// check whether we hold any required mutexes. Reads are checked in
2101 void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
2102 switch (UO->getOpcode()) {
2103 case clang::UO_PostDec:
2104 case clang::UO_PostInc:
2105 case clang::UO_PreDec:
2106 case clang::UO_PreInc: {
2107 checkAccess(UO->getSubExpr(), AK_Written);
2115 /// For binary operations which assign to a variable (writes), we need to check
2116 /// whether we hold any required mutexes.
2117 /// FIXME: Deal with non-primitive types.
2118 void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
2119 if (!BO->isAssignmentOp())
2122 // adjust the context
2123 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
2125 checkAccess(BO->getLHS(), AK_Written);
2129 /// Whenever we do an LValue to Rvalue cast, we are reading a variable and
2130 /// need to ensure we hold any required mutexes.
2131 /// FIXME: Deal with non-primitive types.
2132 void BuildLockset::VisitCastExpr(CastExpr *CE) {
2133 if (CE->getCastKind() != CK_LValueToRValue)
2135 checkAccess(CE->getSubExpr(), AK_Read);
2139 void BuildLockset::VisitCallExpr(CallExpr *Exp) {
2140 if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Exp)) {
2141 MemberExpr *ME = dyn_cast<MemberExpr>(CE->getCallee());
2142 // ME can be null when calling a method pointer
2143 CXXMethodDecl *MD = CE->getMethodDecl();
2146 if (ME->isArrow()) {
2147 if (MD->isConst()) {
2148 checkPtAccess(CE->getImplicitObjectArgument(), AK_Read);
2149 } else { // FIXME -- should be AK_Written
2150 checkPtAccess(CE->getImplicitObjectArgument(), AK_Read);
2154 checkAccess(CE->getImplicitObjectArgument(), AK_Read);
2155 else // FIXME -- should be AK_Written
2156 checkAccess(CE->getImplicitObjectArgument(), AK_Read);
2159 } else if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(Exp)) {
2160 switch (OE->getOperator()) {
2162 const Expr *Target = OE->getArg(0);
2163 const Expr *Source = OE->getArg(1);
2164 checkAccess(Target, AK_Written);
2165 checkAccess(Source, AK_Read);
2170 case OO_Subscript: {
2171 if (Analyzer->Handler.issueBetaWarnings()) {
2172 const Expr *Obj = OE->getArg(0);
2173 checkAccess(Obj, AK_Read);
2174 checkPtAccess(Obj, AK_Read);
2179 const Expr *Obj = OE->getArg(0);
2180 checkAccess(Obj, AK_Read);
2185 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
2186 if(!D || !D->hasAttrs())
2191 void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
2192 const CXXConstructorDecl *D = Exp->getConstructor();
2193 if (D && D->isCopyConstructor()) {
2194 const Expr* Source = Exp->getArg(0);
2195 checkAccess(Source, AK_Read);
2197 // FIXME -- only handles constructors in DeclStmt below.
2200 void BuildLockset::VisitDeclStmt(DeclStmt *S) {
2201 // adjust the context
2202 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
2204 DeclGroupRef DGrp = S->getDeclGroup();
2205 for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
2207 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
2208 Expr *E = VD->getInit();
2209 // handle constructors that involve temporaries
2210 if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E))
2211 E = EWC->getSubExpr();
2213 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
2214 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
2215 if (!CtorD || !CtorD->hasAttrs())
2217 handleCall(CE, CtorD, VD);
2225 /// \brief Compute the intersection of two locksets and issue warnings for any
2226 /// locks in the symmetric difference.
2228 /// This function is used at a merge point in the CFG when comparing the lockset
2229 /// of each branch being merged. For example, given the following sequence:
2230 /// A; if () then B; else C; D; we need to check that the lockset after B and C
2231 /// are the same. In the event of a difference, we use the intersection of these
2232 /// two locksets at the start of D.
2234 /// \param FSet1 The first lockset.
2235 /// \param FSet2 The second lockset.
2236 /// \param JoinLoc The location of the join point for error reporting
2237 /// \param LEK1 The error message to report if a mutex is missing from LSet1
2238 /// \param LEK2 The error message to report if a mutex is missing from Lset2
2239 void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1,
2240 const FactSet &FSet2,
2241 SourceLocation JoinLoc,
2245 FactSet FSet1Orig = FSet1;
2247 // Find locks in FSet2 that conflict or are not in FSet1, and warn.
2248 for (FactSet::const_iterator I = FSet2.begin(), E = FSet2.end();
2250 const SExpr &FSet2Mutex = FactMan[*I].MutID;
2251 const LockData &LDat2 = FactMan[*I].LDat;
2252 FactSet::iterator I1 = FSet1.findLockIter(FactMan, FSet2Mutex);
2254 if (I1 != FSet1.end()) {
2255 const LockData* LDat1 = &FactMan[*I1].LDat;
2256 if (LDat1->LKind != LDat2.LKind) {
2257 Handler.handleExclusiveAndShared(FSet2Mutex.toString(),
2260 if (Modify && LDat1->LKind != LK_Exclusive) {
2261 // Take the exclusive lock, which is the one in FSet2.
2265 else if (LDat1->Asserted && !LDat2.Asserted) {
2266 // The non-asserted lock in FSet2 is the one we want to track.
2270 if (LDat2.UnderlyingMutex.isValid()) {
2271 if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) {
2272 // If this is a scoped lock that manages another mutex, and if the
2273 // underlying mutex is still held, then warn about the underlying
2275 Handler.handleMutexHeldEndOfScope(LDat2.UnderlyingMutex.toString(),
2280 else if (!LDat2.Managed && !FSet2Mutex.isUniversal() && !LDat2.Asserted)
2281 Handler.handleMutexHeldEndOfScope(FSet2Mutex.toString(),
2287 // Find locks in FSet1 that are not in FSet2, and remove them.
2288 for (FactSet::const_iterator I = FSet1Orig.begin(), E = FSet1Orig.end();
2290 const SExpr &FSet1Mutex = FactMan[*I].MutID;
2291 const LockData &LDat1 = FactMan[*I].LDat;
2293 if (!FSet2.findLock(FactMan, FSet1Mutex)) {
2294 if (LDat1.UnderlyingMutex.isValid()) {
2295 if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) {
2296 // If this is a scoped lock that manages another mutex, and if the
2297 // underlying mutex is still held, then warn about the underlying
2299 Handler.handleMutexHeldEndOfScope(LDat1.UnderlyingMutex.toString(),
2304 else if (!LDat1.Managed && !FSet1Mutex.isUniversal() && !LDat1.Asserted)
2305 Handler.handleMutexHeldEndOfScope(FSet1Mutex.toString(),
2309 FSet1.removeLock(FactMan, FSet1Mutex);
2315 // Return true if block B never continues to its successors.
2316 inline bool neverReturns(const CFGBlock* B) {
2317 if (B->hasNoReturnElement())
2322 CFGElement Last = B->back();
2323 if (Optional<CFGStmt> S = Last.getAs<CFGStmt>()) {
2324 if (isa<CXXThrowExpr>(S->getStmt()))
2331 /// \brief Check a function's CFG for thread-safety violations.
2333 /// We traverse the blocks in the CFG, compute the set of mutexes that are held
2334 /// at the end of each block, and issue warnings for thread safety violations.
2335 /// Each block in the CFG is traversed exactly once.
2336 void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
2337 CFG *CFGraph = AC.getCFG();
2338 if (!CFGraph) return;
2339 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(AC.getDecl());
2341 // AC.dumpCFG(true);
2344 return; // Ignore anonymous functions for now.
2345 if (D->getAttr<NoThreadSafetyAnalysisAttr>())
2347 // FIXME: Do something a bit more intelligent inside constructor and
2348 // destructor code. Constructors and destructors must assume unique access
2349 // to 'this', so checks on member variable access is disabled, but we should
2350 // still enable checks on other objects.
2351 if (isa<CXXConstructorDecl>(D))
2352 return; // Don't check inside constructors.
2353 if (isa<CXXDestructorDecl>(D))
2354 return; // Don't check inside destructors.
2356 BlockInfo.resize(CFGraph->getNumBlockIDs(),
2357 CFGBlockInfo::getEmptyBlockInfo(LocalVarMap));
2359 // We need to explore the CFG via a "topological" ordering.
2360 // That way, we will be guaranteed to have information about required
2361 // predecessor locksets when exploring a new block.
2362 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
2363 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
2365 // Mark entry block as reachable
2366 BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true;
2368 // Compute SSA names for local variables
2369 LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
2371 // Fill in source locations for all CFGBlocks.
2372 findBlockLocations(CFGraph, SortedGraph, BlockInfo);
2374 MutexIDList ExclusiveLocksAcquired;
2375 MutexIDList SharedLocksAcquired;
2376 MutexIDList LocksReleased;
2378 // Add locks from exclusive_locks_required and shared_locks_required
2379 // to initial lockset. Also turn off checking for lock and unlock functions.
2380 // FIXME: is there a more intelligent way to check lock/unlock functions?
2381 if (!SortedGraph->empty() && D->hasAttrs()) {
2382 const CFGBlock *FirstBlock = *SortedGraph->begin();
2383 FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
2384 const AttrVec &ArgAttrs = D->getAttrs();
2386 MutexIDList ExclusiveLocksToAdd;
2387 MutexIDList SharedLocksToAdd;
2389 SourceLocation Loc = D->getLocation();
2390 for (unsigned i = 0; i < ArgAttrs.size(); ++i) {
2391 Attr *Attr = ArgAttrs[i];
2392 Loc = Attr->getLocation();
2393 if (ExclusiveLocksRequiredAttr *A
2394 = dyn_cast<ExclusiveLocksRequiredAttr>(Attr)) {
2395 getMutexIDs(ExclusiveLocksToAdd, A, (Expr*) 0, D);
2396 } else if (SharedLocksRequiredAttr *A
2397 = dyn_cast<SharedLocksRequiredAttr>(Attr)) {
2398 getMutexIDs(SharedLocksToAdd, A, (Expr*) 0, D);
2399 } else if (UnlockFunctionAttr *A = dyn_cast<UnlockFunctionAttr>(Attr)) {
2400 // UNLOCK_FUNCTION() is used to hide the underlying lock implementation.
2401 // We must ignore such methods.
2402 if (A->args_size() == 0)
2404 // FIXME -- deal with exclusive vs. shared unlock functions?
2405 getMutexIDs(ExclusiveLocksToAdd, A, (Expr*) 0, D);
2406 getMutexIDs(LocksReleased, A, (Expr*) 0, D);
2407 } else if (ExclusiveLockFunctionAttr *A
2408 = dyn_cast<ExclusiveLockFunctionAttr>(Attr)) {
2409 if (A->args_size() == 0)
2411 getMutexIDs(ExclusiveLocksAcquired, A, (Expr*) 0, D);
2412 } else if (SharedLockFunctionAttr *A
2413 = dyn_cast<SharedLockFunctionAttr>(Attr)) {
2414 if (A->args_size() == 0)
2416 getMutexIDs(SharedLocksAcquired, A, (Expr*) 0, D);
2417 } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) {
2418 // Don't try to check trylock functions for now
2420 } else if (isa<SharedTrylockFunctionAttr>(Attr)) {
2421 // Don't try to check trylock functions for now
2426 // FIXME -- Loc can be wrong here.
2427 for (unsigned i=0,n=ExclusiveLocksToAdd.size(); i<n; ++i) {
2428 addLock(InitialLockset, ExclusiveLocksToAdd[i],
2429 LockData(Loc, LK_Exclusive));
2431 for (unsigned i=0,n=SharedLocksToAdd.size(); i<n; ++i) {
2432 addLock(InitialLockset, SharedLocksToAdd[i],
2433 LockData(Loc, LK_Shared));
2437 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
2438 E = SortedGraph->end(); I!= E; ++I) {
2439 const CFGBlock *CurrBlock = *I;
2440 int CurrBlockID = CurrBlock->getBlockID();
2441 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
2443 // Use the default initial lockset in case there are no predecessors.
2444 VisitedBlocks.insert(CurrBlock);
2446 // Iterate through the predecessor blocks and warn if the lockset for all
2447 // predecessors is not the same. We take the entry lockset of the current
2448 // block to be the intersection of all previous locksets.
2449 // FIXME: By keeping the intersection, we may output more errors in future
2450 // for a lock which is not in the intersection, but was in the union. We
2451 // may want to also keep the union in future. As an example, let's say
2452 // the intersection contains Mutex L, and the union contains L and M.
2453 // Later we unlock M. At this point, we would output an error because we
2454 // never locked M; although the real error is probably that we forgot to
2455 // lock M on all code paths. Conversely, let's say that later we lock M.
2456 // In this case, we should compare against the intersection instead of the
2457 // union because the real error is probably that we forgot to unlock M on
2459 bool LocksetInitialized = false;
2460 SmallVector<CFGBlock *, 8> SpecialBlocks;
2461 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
2462 PE = CurrBlock->pred_end(); PI != PE; ++PI) {
2464 // if *PI -> CurrBlock is a back edge
2465 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI))
2468 int PrevBlockID = (*PI)->getBlockID();
2469 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2471 // Ignore edges from blocks that can't return.
2472 if (neverReturns(*PI) || !PrevBlockInfo->Reachable)
2475 // Okay, we can reach this block from the entry.
2476 CurrBlockInfo->Reachable = true;
2478 // If the previous block ended in a 'continue' or 'break' statement, then
2479 // a difference in locksets is probably due to a bug in that block, rather
2480 // than in some other predecessor. In that case, keep the other
2481 // predecessor's lockset.
2482 if (const Stmt *Terminator = (*PI)->getTerminator()) {
2483 if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
2484 SpecialBlocks.push_back(*PI);
2489 FactSet PrevLockset;
2490 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock);
2492 if (!LocksetInitialized) {
2493 CurrBlockInfo->EntrySet = PrevLockset;
2494 LocksetInitialized = true;
2496 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2497 CurrBlockInfo->EntryLoc,
2498 LEK_LockedSomePredecessors);
2502 // Skip rest of block if it's not reachable.
2503 if (!CurrBlockInfo->Reachable)
2506 // Process continue and break blocks. Assume that the lockset for the
2507 // resulting block is unaffected by any discrepancies in them.
2508 for (unsigned SpecialI = 0, SpecialN = SpecialBlocks.size();
2509 SpecialI < SpecialN; ++SpecialI) {
2510 CFGBlock *PrevBlock = SpecialBlocks[SpecialI];
2511 int PrevBlockID = PrevBlock->getBlockID();
2512 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
2514 if (!LocksetInitialized) {
2515 CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
2516 LocksetInitialized = true;
2518 // Determine whether this edge is a loop terminator for diagnostic
2519 // purposes. FIXME: A 'break' statement might be a loop terminator, but
2520 // it might also be part of a switch. Also, a subsequent destructor
2521 // might add to the lockset, in which case the real issue might be a
2522 // double lock on the other path.
2523 const Stmt *Terminator = PrevBlock->getTerminator();
2524 bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
2526 FactSet PrevLockset;
2527 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet,
2528 PrevBlock, CurrBlock);
2530 // Do not update EntrySet.
2531 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
2532 PrevBlockInfo->ExitLoc,
2533 IsLoop ? LEK_LockedSomeLoopIterations
2534 : LEK_LockedSomePredecessors,
2539 BuildLockset LocksetBuilder(this, *CurrBlockInfo);
2541 // Visit all the statements in the basic block.
2542 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
2543 BE = CurrBlock->end(); BI != BE; ++BI) {
2544 switch (BI->getKind()) {
2545 case CFGElement::Statement: {
2546 CFGStmt CS = BI->castAs<CFGStmt>();
2547 LocksetBuilder.Visit(const_cast<Stmt*>(CS.getStmt()));
2550 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
2551 case CFGElement::AutomaticObjectDtor: {
2552 CFGAutomaticObjDtor AD = BI->castAs<CFGAutomaticObjDtor>();
2553 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl *>(
2554 AD.getDestructorDecl(AC.getASTContext()));
2555 if (!DD->hasAttrs())
2558 // Create a dummy expression,
2559 VarDecl *VD = const_cast<VarDecl*>(AD.getVarDecl());
2560 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
2561 AD.getTriggerStmt()->getLocEnd());
2562 LocksetBuilder.handleCall(&DRE, DD);
2569 CurrBlockInfo->ExitSet = LocksetBuilder.FSet;
2571 // For every back edge from CurrBlock (the end of the loop) to another block
2572 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to
2573 // the one held at the beginning of FirstLoopBlock. We can look up the
2574 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
2575 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
2576 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
2578 // if CurrBlock -> *SI is *not* a back edge
2579 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI))
2582 CFGBlock *FirstLoopBlock = *SI;
2583 CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
2584 CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
2585 intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
2587 LEK_LockedSomeLoopIterations,
2592 CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
2593 CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()];
2595 // Skip the final check if the exit block is unreachable.
2596 if (!Final->Reachable)
2599 // By default, we expect all locks held on entry to be held on exit.
2600 FactSet ExpectedExitSet = Initial->EntrySet;
2602 // Adjust the expected exit set by adding or removing locks, as declared
2603 // by *-LOCK_FUNCTION and UNLOCK_FUNCTION. The intersect below will then
2604 // issue the appropriate warning.
2605 // FIXME: the location here is not quite right.
2606 for (unsigned i=0,n=ExclusiveLocksAcquired.size(); i<n; ++i) {
2607 ExpectedExitSet.addLock(FactMan, ExclusiveLocksAcquired[i],
2608 LockData(D->getLocation(), LK_Exclusive));
2610 for (unsigned i=0,n=SharedLocksAcquired.size(); i<n; ++i) {
2611 ExpectedExitSet.addLock(FactMan, SharedLocksAcquired[i],
2612 LockData(D->getLocation(), LK_Shared));
2614 for (unsigned i=0,n=LocksReleased.size(); i<n; ++i) {
2615 ExpectedExitSet.removeLock(FactMan, LocksReleased[i]);
2618 // FIXME: Should we call this function for all blocks which exit the function?
2619 intersectAndWarn(ExpectedExitSet, Final->ExitSet,
2621 LEK_LockedAtEndOfFunction,
2622 LEK_NotLockedAtEndOfFunction,
2626 } // end anonymous namespace
2630 namespace thread_safety {
2632 /// \brief Check a function's CFG for thread-safety violations.
2634 /// We traverse the blocks in the CFG, compute the set of mutexes that are held
2635 /// at the end of each block, and issue warnings for thread safety violations.
2636 /// Each block in the CFG is traversed exactly once.
2637 void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
2638 ThreadSafetyHandler &Handler) {
2639 ThreadSafetyAnalyzer Analyzer(Handler);
2640 Analyzer.runAnalysis(AC);
2643 /// \brief Helper function that returns a LockKind required for the given level
2645 LockKind getLockKindFromAccessKind(AccessKind AK) {
2650 return LK_Exclusive;
2652 llvm_unreachable("Unknown AccessKind");
2655 }} // end namespace clang::thread_safety