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