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