]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Analysis/UninitializedValues.cpp
Merge ACPICA 20170929.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Analysis / UninitializedValues.cpp
1 //==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements uninitialized values analysis for source-level CFGs.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/StmtVisitor.h"
19 #include "clang/Analysis/Analyses/PostOrderCFGView.h"
20 #include "clang/Analysis/Analyses/UninitializedValues.h"
21 #include "clang/Analysis/AnalysisContext.h"
22 #include "clang/Analysis/CFG.h"
23 #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/PackedVector.h"
27 #include "llvm/ADT/SmallBitVector.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Support/SaveAndRestore.h"
30 #include <utility>
31
32 using namespace clang;
33
34 #define DEBUG_LOGGING 0
35
36 static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
37   if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
38       !vd->isExceptionVariable() && !vd->isInitCapture() &&
39       !vd->isImplicit() && vd->getDeclContext() == dc) {
40     QualType ty = vd->getType();
41     return ty->isScalarType() || ty->isVectorType() || ty->isRecordType();
42   }
43   return false;
44 }
45
46 //------------------------------------------------------------------------====//
47 // DeclToIndex: a mapping from Decls we track to value indices.
48 //====------------------------------------------------------------------------//
49
50 namespace {
51 class DeclToIndex {
52   llvm::DenseMap<const VarDecl *, unsigned> map;
53 public:
54   DeclToIndex() {}
55   
56   /// Compute the actual mapping from declarations to bits.
57   void computeMap(const DeclContext &dc);
58   
59   /// Return the number of declarations in the map.
60   unsigned size() const { return map.size(); }
61   
62   /// Returns the bit vector index for a given declaration.
63   Optional<unsigned> getValueIndex(const VarDecl *d) const;
64 };
65 }
66
67 void DeclToIndex::computeMap(const DeclContext &dc) {
68   unsigned count = 0;
69   DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
70                                                E(dc.decls_end());
71   for ( ; I != E; ++I) {
72     const VarDecl *vd = *I;
73     if (isTrackedVar(vd, &dc))
74       map[vd] = count++;
75   }
76 }
77
78 Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
79   llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
80   if (I == map.end())
81     return None;
82   return I->second;
83 }
84
85 //------------------------------------------------------------------------====//
86 // CFGBlockValues: dataflow values for CFG blocks.
87 //====------------------------------------------------------------------------//
88
89 // These values are defined in such a way that a merge can be done using
90 // a bitwise OR.
91 enum Value { Unknown = 0x0,         /* 00 */
92              Initialized = 0x1,     /* 01 */
93              Uninitialized = 0x2,   /* 10 */
94              MayUninitialized = 0x3 /* 11 */ };
95
96 static bool isUninitialized(const Value v) {
97   return v >= Uninitialized;
98 }
99 static bool isAlwaysUninit(const Value v) {
100   return v == Uninitialized;
101 }
102
103 namespace {
104
105 typedef llvm::PackedVector<Value, 2, llvm::SmallBitVector> ValueVector;
106
107 class CFGBlockValues {
108   const CFG &cfg;
109   SmallVector<ValueVector, 8> vals;
110   ValueVector scratch;
111   DeclToIndex declToIndex;
112 public:
113   CFGBlockValues(const CFG &cfg);
114
115   unsigned getNumEntries() const { return declToIndex.size(); }
116   
117   void computeSetOfDeclarations(const DeclContext &dc);  
118   ValueVector &getValueVector(const CFGBlock *block) {
119     return vals[block->getBlockID()];
120   }
121
122   void setAllScratchValues(Value V);
123   void mergeIntoScratch(ValueVector const &source, bool isFirst);
124   bool updateValueVectorWithScratch(const CFGBlock *block);
125   
126   bool hasNoDeclarations() const {
127     return declToIndex.size() == 0;
128   }
129
130   void resetScratch();
131   
132   ValueVector::reference operator[](const VarDecl *vd);
133
134   Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
135                  const VarDecl *vd) {
136     const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
137     assert(idx.hasValue());
138     return getValueVector(block)[idx.getValue()];
139   }
140 };  
141 } // end anonymous namespace
142
143 CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {}
144
145 void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
146   declToIndex.computeMap(dc);
147   unsigned decls = declToIndex.size();
148   scratch.resize(decls);
149   unsigned n = cfg.getNumBlockIDs();
150   if (!n)
151     return;
152   vals.resize(n);
153   for (unsigned i = 0; i < n; ++i)
154     vals[i].resize(decls);
155 }
156
157 #if DEBUG_LOGGING
158 static void printVector(const CFGBlock *block, ValueVector &bv,
159                         unsigned num) {
160   llvm::errs() << block->getBlockID() << " :";
161   for (unsigned i = 0; i < bv.size(); ++i) {
162     llvm::errs() << ' ' << bv[i];
163   }
164   llvm::errs() << " : " << num << '\n';
165 }
166 #endif
167
168 void CFGBlockValues::setAllScratchValues(Value V) {
169   for (unsigned I = 0, E = scratch.size(); I != E; ++I)
170     scratch[I] = V;
171 }
172
173 void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
174                                       bool isFirst) {
175   if (isFirst)
176     scratch = source;
177   else
178     scratch |= source;
179 }
180
181 bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
182   ValueVector &dst = getValueVector(block);
183   bool changed = (dst != scratch);
184   if (changed)
185     dst = scratch;
186 #if DEBUG_LOGGING
187   printVector(block, scratch, 0);
188 #endif
189   return changed;
190 }
191
192 void CFGBlockValues::resetScratch() {
193   scratch.reset();
194 }
195
196 ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
197   const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
198   assert(idx.hasValue());
199   return scratch[idx.getValue()];
200 }
201
202 //------------------------------------------------------------------------====//
203 // Worklist: worklist for dataflow analysis.
204 //====------------------------------------------------------------------------//
205
206 namespace {
207 class DataflowWorklist {
208   PostOrderCFGView::iterator PO_I, PO_E;
209   SmallVector<const CFGBlock *, 20> worklist;
210   llvm::BitVector enqueuedBlocks;
211 public:
212   DataflowWorklist(const CFG &cfg, PostOrderCFGView &view)
213     : PO_I(view.begin()), PO_E(view.end()),
214       enqueuedBlocks(cfg.getNumBlockIDs(), true) {
215         // Treat the first block as already analyzed.
216         if (PO_I != PO_E) {
217           assert(*PO_I == &cfg.getEntry());
218           enqueuedBlocks[(*PO_I)->getBlockID()] = false;
219           ++PO_I;
220         }
221       }
222   
223   void enqueueSuccessors(const CFGBlock *block);
224   const CFGBlock *dequeue();
225 };
226 }
227
228 void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
229   for (CFGBlock::const_succ_iterator I = block->succ_begin(),
230        E = block->succ_end(); I != E; ++I) {
231     const CFGBlock *Successor = *I;
232     if (!Successor || enqueuedBlocks[Successor->getBlockID()])
233       continue;
234     worklist.push_back(Successor);
235     enqueuedBlocks[Successor->getBlockID()] = true;
236   }
237 }
238
239 const CFGBlock *DataflowWorklist::dequeue() {
240   const CFGBlock *B = nullptr;
241
242   // First dequeue from the worklist.  This can represent
243   // updates along backedges that we want propagated as quickly as possible.
244   if (!worklist.empty())
245     B = worklist.pop_back_val();
246
247   // Next dequeue from the initial reverse post order.  This is the
248   // theoretical ideal in the presence of no back edges.
249   else if (PO_I != PO_E) {
250     B = *PO_I;
251     ++PO_I;
252   }
253   else {
254     return nullptr;
255   }
256
257   assert(enqueuedBlocks[B->getBlockID()] == true);
258   enqueuedBlocks[B->getBlockID()] = false;
259   return B;
260 }
261
262 //------------------------------------------------------------------------====//
263 // Classification of DeclRefExprs as use or initialization.
264 //====------------------------------------------------------------------------//
265
266 namespace {
267 class FindVarResult {
268   const VarDecl *vd;
269   const DeclRefExpr *dr;
270 public:
271   FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {}
272
273   const DeclRefExpr *getDeclRefExpr() const { return dr; }
274   const VarDecl *getDecl() const { return vd; }
275 };
276
277 static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
278   while (Ex) {
279     Ex = Ex->IgnoreParenNoopCasts(C);
280     if (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
281       if (CE->getCastKind() == CK_LValueBitCast) {
282         Ex = CE->getSubExpr();
283         continue;
284       }
285     }
286     break;
287   }
288   return Ex;
289 }
290
291 /// If E is an expression comprising a reference to a single variable, find that
292 /// variable.
293 static FindVarResult findVar(const Expr *E, const DeclContext *DC) {
294   if (const DeclRefExpr *DRE =
295         dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E)))
296     if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
297       if (isTrackedVar(VD, DC))
298         return FindVarResult(VD, DRE);
299   return FindVarResult(nullptr, nullptr);
300 }
301
302 /// \brief Classify each DeclRefExpr as an initialization or a use. Any
303 /// DeclRefExpr which isn't explicitly classified will be assumed to have
304 /// escaped the analysis and will be treated as an initialization.
305 class ClassifyRefs : public StmtVisitor<ClassifyRefs> {
306 public:
307   enum Class {
308     Init,
309     Use,
310     SelfInit,
311     Ignore
312   };
313
314 private:
315   const DeclContext *DC;
316   llvm::DenseMap<const DeclRefExpr*, Class> Classification;
317
318   bool isTrackedVar(const VarDecl *VD) const {
319     return ::isTrackedVar(VD, DC);
320   }
321
322   void classify(const Expr *E, Class C);
323
324 public:
325   ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {}
326
327   void VisitDeclStmt(DeclStmt *DS);
328   void VisitUnaryOperator(UnaryOperator *UO);
329   void VisitBinaryOperator(BinaryOperator *BO);
330   void VisitCallExpr(CallExpr *CE);
331   void VisitCastExpr(CastExpr *CE);
332
333   void operator()(Stmt *S) { Visit(S); }
334
335   Class get(const DeclRefExpr *DRE) const {
336     llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I
337         = Classification.find(DRE);
338     if (I != Classification.end())
339       return I->second;
340
341     const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
342     if (!VD || !isTrackedVar(VD))
343       return Ignore;
344
345     return Init;
346   }
347 };
348 }
349
350 static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) {
351   if (VD->getType()->isRecordType())
352     return nullptr;
353   if (Expr *Init = VD->getInit()) {
354     const DeclRefExpr *DRE
355       = dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init));
356     if (DRE && DRE->getDecl() == VD)
357       return DRE;
358   }
359   return nullptr;
360 }
361
362 void ClassifyRefs::classify(const Expr *E, Class C) {
363   // The result of a ?: could also be an lvalue.
364   E = E->IgnoreParens();
365   if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
366     classify(CO->getTrueExpr(), C);
367     classify(CO->getFalseExpr(), C);
368     return;
369   }
370
371   if (const BinaryConditionalOperator *BCO =
372           dyn_cast<BinaryConditionalOperator>(E)) {
373     classify(BCO->getFalseExpr(), C);
374     return;
375   }
376
377   if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) {
378     classify(OVE->getSourceExpr(), C);
379     return;
380   }
381
382   if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
383     if (VarDecl *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
384       if (!VD->isStaticDataMember())
385         classify(ME->getBase(), C);
386     }
387     return;
388   }
389
390   if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
391     switch (BO->getOpcode()) {
392     case BO_PtrMemD:
393     case BO_PtrMemI:
394       classify(BO->getLHS(), C);
395       return;
396     case BO_Comma:
397       classify(BO->getRHS(), C);
398       return;
399     default:
400       return;
401     }
402   }
403
404   FindVarResult Var = findVar(E, DC);
405   if (const DeclRefExpr *DRE = Var.getDeclRefExpr())
406     Classification[DRE] = std::max(Classification[DRE], C);
407 }
408
409 void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
410   for (auto *DI : DS->decls()) {
411     VarDecl *VD = dyn_cast<VarDecl>(DI);
412     if (VD && isTrackedVar(VD))
413       if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
414         Classification[DRE] = SelfInit;
415   }
416 }
417
418 void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
419   // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this
420   // is not a compound-assignment, we will treat it as initializing the variable
421   // when TransferFunctions visits it. A compound-assignment does not affect
422   // whether a variable is uninitialized, and there's no point counting it as a
423   // use.
424   if (BO->isCompoundAssignmentOp())
425     classify(BO->getLHS(), Use);
426   else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma)
427     classify(BO->getLHS(), Ignore);
428 }
429
430 void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) {
431   // Increment and decrement are uses despite there being no lvalue-to-rvalue
432   // conversion.
433   if (UO->isIncrementDecrementOp())
434     classify(UO->getSubExpr(), Use);
435 }
436
437 static bool isPointerToConst(const QualType &QT) {
438   return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified();
439 }
440
441 void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
442   // Classify arguments to std::move as used.
443   if (CE->getNumArgs() == 1) {
444     if (FunctionDecl *FD = CE->getDirectCallee()) {
445       if (FD->isInStdNamespace() && FD->getIdentifier() &&
446           FD->getIdentifier()->isStr("move")) {
447         // RecordTypes are handled in SemaDeclCXX.cpp.
448         if (!CE->getArg(0)->getType()->isRecordType())
449           classify(CE->getArg(0), Use);
450         return;
451       }
452     }
453   }
454
455   // If a value is passed by const pointer or by const reference to a function,
456   // we should not assume that it is initialized by the call, and we
457   // conservatively do not assume that it is used.
458   for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
459        I != E; ++I) {
460     if ((*I)->isGLValue()) {
461       if ((*I)->getType().isConstQualified())
462         classify((*I), Ignore);
463     } else if (isPointerToConst((*I)->getType())) {
464       const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
465       const UnaryOperator *UO = dyn_cast<UnaryOperator>(Ex);
466       if (UO && UO->getOpcode() == UO_AddrOf)
467         Ex = UO->getSubExpr();
468       classify(Ex, Ignore);
469     }
470   }
471 }
472
473 void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
474   if (CE->getCastKind() == CK_LValueToRValue)
475     classify(CE->getSubExpr(), Use);
476   else if (CStyleCastExpr *CSE = dyn_cast<CStyleCastExpr>(CE)) {
477     if (CSE->getType()->isVoidType()) {
478       // Squelch any detected load of an uninitialized value if
479       // we cast it to void.
480       // e.g. (void) x;
481       classify(CSE->getSubExpr(), Ignore);
482     }
483   }
484 }
485
486 //------------------------------------------------------------------------====//
487 // Transfer function for uninitialized values analysis.
488 //====------------------------------------------------------------------------//
489
490 namespace {
491 class TransferFunctions : public StmtVisitor<TransferFunctions> {
492   CFGBlockValues &vals;
493   const CFG &cfg;
494   const CFGBlock *block;
495   AnalysisDeclContext &ac;
496   const ClassifyRefs &classification;
497   ObjCNoReturn objCNoRet;
498   UninitVariablesHandler &handler;
499
500 public:
501   TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
502                     const CFGBlock *block, AnalysisDeclContext &ac,
503                     const ClassifyRefs &classification,
504                     UninitVariablesHandler &handler)
505     : vals(vals), cfg(cfg), block(block), ac(ac),
506       classification(classification), objCNoRet(ac.getASTContext()),
507       handler(handler) {}
508
509   void reportUse(const Expr *ex, const VarDecl *vd);
510
511   void VisitBinaryOperator(BinaryOperator *bo);
512   void VisitBlockExpr(BlockExpr *be);
513   void VisitCallExpr(CallExpr *ce);
514   void VisitDeclRefExpr(DeclRefExpr *dr);
515   void VisitDeclStmt(DeclStmt *ds);
516   void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
517   void VisitObjCMessageExpr(ObjCMessageExpr *ME);
518
519   bool isTrackedVar(const VarDecl *vd) {
520     return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
521   }
522
523   FindVarResult findVar(const Expr *ex) {
524     return ::findVar(ex, cast<DeclContext>(ac.getDecl()));
525   }
526
527   UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
528     UninitUse Use(ex, isAlwaysUninit(v));
529
530     assert(isUninitialized(v));
531     if (Use.getKind() == UninitUse::Always)
532       return Use;
533
534     // If an edge which leads unconditionally to this use did not initialize
535     // the variable, we can say something stronger than 'may be uninitialized':
536     // we can say 'either it's used uninitialized or you have dead code'.
537     //
538     // We track the number of successors of a node which have been visited, and
539     // visit a node once we have visited all of its successors. Only edges where
540     // the variable might still be uninitialized are followed. Since a variable
541     // can't transfer from being initialized to being uninitialized, this will
542     // trace out the subgraph which inevitably leads to the use and does not
543     // initialize the variable. We do not want to skip past loops, since their
544     // non-termination might be correlated with the initialization condition.
545     //
546     // For example:
547     //
548     //         void f(bool a, bool b) {
549     // block1:   int n;
550     //           if (a) {
551     // block2:     if (b)
552     // block3:       n = 1;
553     // block4:   } else if (b) {
554     // block5:     while (!a) {
555     // block6:       do_work(&a);
556     //               n = 2;
557     //             }
558     //           }
559     // block7:   if (a)
560     // block8:     g();
561     // block9:   return n;
562     //         }
563     //
564     // Starting from the maybe-uninitialized use in block 9:
565     //  * Block 7 is not visited because we have only visited one of its two
566     //    successors.
567     //  * Block 8 is visited because we've visited its only successor.
568     // From block 8:
569     //  * Block 7 is visited because we've now visited both of its successors.
570     // From block 7:
571     //  * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
572     //    of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
573     //  * Block 3 is not visited because it initializes 'n'.
574     // Now the algorithm terminates, having visited blocks 7 and 8, and having
575     // found the frontier is blocks 2, 4, and 5.
576     //
577     // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
578     // and 4), so we report that any time either of those edges is taken (in
579     // each case when 'b == false'), 'n' is used uninitialized.
580     SmallVector<const CFGBlock*, 32> Queue;
581     SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
582     Queue.push_back(block);
583     // Specify that we've already visited all successors of the starting block.
584     // This has the dual purpose of ensuring we never add it to the queue, and
585     // of marking it as not being a candidate element of the frontier.
586     SuccsVisited[block->getBlockID()] = block->succ_size();
587     while (!Queue.empty()) {
588       const CFGBlock *B = Queue.pop_back_val();
589
590       // If the use is always reached from the entry block, make a note of that.
591       if (B == &cfg.getEntry())
592         Use.setUninitAfterCall();
593
594       for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
595            I != E; ++I) {
596         const CFGBlock *Pred = *I;
597         if (!Pred)
598           continue;
599         
600         Value AtPredExit = vals.getValue(Pred, B, vd);
601         if (AtPredExit == Initialized)
602           // This block initializes the variable.
603           continue;
604         if (AtPredExit == MayUninitialized &&
605             vals.getValue(B, nullptr, vd) == Uninitialized) {
606           // This block declares the variable (uninitialized), and is reachable
607           // from a block that initializes the variable. We can't guarantee to
608           // give an earlier location for the diagnostic (and it appears that
609           // this code is intended to be reachable) so give a diagnostic here
610           // and go no further down this path.
611           Use.setUninitAfterDecl();
612           continue;
613         }
614
615         unsigned &SV = SuccsVisited[Pred->getBlockID()];
616         if (!SV) {
617           // When visiting the first successor of a block, mark all NULL
618           // successors as having been visited.
619           for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(),
620                                              SE = Pred->succ_end();
621                SI != SE; ++SI)
622             if (!*SI)
623               ++SV;
624         }
625
626         if (++SV == Pred->succ_size())
627           // All paths from this block lead to the use and don't initialize the
628           // variable.
629           Queue.push_back(Pred);
630       }
631     }
632
633     // Scan the frontier, looking for blocks where the variable was
634     // uninitialized.
635     for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
636       const CFGBlock *Block = *BI;
637       unsigned BlockID = Block->getBlockID();
638       const Stmt *Term = Block->getTerminator();
639       if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
640           Term) {
641         // This block inevitably leads to the use. If we have an edge from here
642         // to a post-dominator block, and the variable is uninitialized on that
643         // edge, we have found a bug.
644         for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
645              E = Block->succ_end(); I != E; ++I) {
646           const CFGBlock *Succ = *I;
647           if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
648               vals.getValue(Block, Succ, vd) == Uninitialized) {
649             // Switch cases are a special case: report the label to the caller
650             // as the 'terminator', not the switch statement itself. Suppress
651             // situations where no label matched: we can't be sure that's
652             // possible.
653             if (isa<SwitchStmt>(Term)) {
654               const Stmt *Label = Succ->getLabel();
655               if (!Label || !isa<SwitchCase>(Label))
656                 // Might not be possible.
657                 continue;
658               UninitUse::Branch Branch;
659               Branch.Terminator = Label;
660               Branch.Output = 0; // Ignored.
661               Use.addUninitBranch(Branch);
662             } else {
663               UninitUse::Branch Branch;
664               Branch.Terminator = Term;
665               Branch.Output = I - Block->succ_begin();
666               Use.addUninitBranch(Branch);
667             }
668           }
669         }
670       }
671     }
672
673     return Use;
674   }
675 };
676 }
677
678 void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
679   Value v = vals[vd];
680   if (isUninitialized(v))
681     handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
682 }
683
684 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) {
685   // This represents an initialization of the 'element' value.
686   if (DeclStmt *DS = dyn_cast<DeclStmt>(FS->getElement())) {
687     const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
688     if (isTrackedVar(VD))
689       vals[VD] = Initialized;
690   }
691 }
692
693 void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
694   const BlockDecl *bd = be->getBlockDecl();
695   for (const auto &I : bd->captures()) {
696     const VarDecl *vd = I.getVariable();
697     if (!isTrackedVar(vd))
698       continue;
699     if (I.isByRef()) {
700       vals[vd] = Initialized;
701       continue;
702     }
703     reportUse(be, vd);
704   }
705 }
706
707 void TransferFunctions::VisitCallExpr(CallExpr *ce) {
708   if (Decl *Callee = ce->getCalleeDecl()) {
709     if (Callee->hasAttr<ReturnsTwiceAttr>()) {
710       // After a call to a function like setjmp or vfork, any variable which is
711       // initialized anywhere within this function may now be initialized. For
712       // now, just assume such a call initializes all variables.  FIXME: Only
713       // mark variables as initialized if they have an initializer which is
714       // reachable from here.
715       vals.setAllScratchValues(Initialized);
716     }
717     else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) {
718       // Functions labeled like "analyzer_noreturn" are often used to denote
719       // "panic" functions that in special debug situations can still return,
720       // but for the most part should not be treated as returning.  This is a
721       // useful annotation borrowed from the static analyzer that is useful for
722       // suppressing branch-specific false positives when we call one of these
723       // functions but keep pretending the path continues (when in reality the
724       // user doesn't care).
725       vals.setAllScratchValues(Unknown);
726     }
727   }
728 }
729
730 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
731   switch (classification.get(dr)) {
732   case ClassifyRefs::Ignore:
733     break;
734   case ClassifyRefs::Use:
735     reportUse(dr, cast<VarDecl>(dr->getDecl()));
736     break;
737   case ClassifyRefs::Init:
738     vals[cast<VarDecl>(dr->getDecl())] = Initialized;
739     break;
740   case ClassifyRefs::SelfInit:
741       handler.handleSelfInit(cast<VarDecl>(dr->getDecl()));
742     break;
743   }
744 }
745
746 void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
747   if (BO->getOpcode() == BO_Assign) {
748     FindVarResult Var = findVar(BO->getLHS());
749     if (const VarDecl *VD = Var.getDecl())
750       vals[VD] = Initialized;
751   }
752 }
753
754 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
755   for (auto *DI : DS->decls()) {
756     VarDecl *VD = dyn_cast<VarDecl>(DI);
757     if (VD && isTrackedVar(VD)) {
758       if (getSelfInitExpr(VD)) {
759         // If the initializer consists solely of a reference to itself, we
760         // explicitly mark the variable as uninitialized. This allows code
761         // like the following:
762         //
763         //   int x = x;
764         //
765         // to deliberately leave a variable uninitialized. Different analysis
766         // clients can detect this pattern and adjust their reporting
767         // appropriately, but we need to continue to analyze subsequent uses
768         // of the variable.
769         vals[VD] = Uninitialized;
770       } else if (VD->getInit()) {
771         // Treat the new variable as initialized.
772         vals[VD] = Initialized;
773       } else {
774         // No initializer: the variable is now uninitialized. This matters
775         // for cases like:
776         //   while (...) {
777         //     int n;
778         //     use(n);
779         //     n = 0;
780         //   }
781         // FIXME: Mark the variable as uninitialized whenever its scope is
782         // left, since its scope could be re-entered by a jump over the
783         // declaration.
784         vals[VD] = Uninitialized;
785       }
786     }
787   }
788 }
789
790 void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
791   // If the Objective-C message expression is an implicit no-return that
792   // is not modeled in the CFG, set the tracked dataflow values to Unknown.
793   if (objCNoRet.isImplicitNoReturn(ME)) {
794     vals.setAllScratchValues(Unknown);
795   }
796 }
797
798 //------------------------------------------------------------------------====//
799 // High-level "driver" logic for uninitialized values analysis.
800 //====------------------------------------------------------------------------//
801
802 static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
803                        AnalysisDeclContext &ac, CFGBlockValues &vals,
804                        const ClassifyRefs &classification,
805                        llvm::BitVector &wasAnalyzed,
806                        UninitVariablesHandler &handler) {
807   wasAnalyzed[block->getBlockID()] = true;
808   vals.resetScratch();
809   // Merge in values of predecessor blocks.
810   bool isFirst = true;
811   for (CFGBlock::const_pred_iterator I = block->pred_begin(),
812        E = block->pred_end(); I != E; ++I) {
813     const CFGBlock *pred = *I;
814     if (!pred)
815       continue;
816     if (wasAnalyzed[pred->getBlockID()]) {
817       vals.mergeIntoScratch(vals.getValueVector(pred), isFirst);
818       isFirst = false;
819     }
820   }
821   // Apply the transfer function.
822   TransferFunctions tf(vals, cfg, block, ac, classification, handler);
823   for (CFGBlock::const_iterator I = block->begin(), E = block->end(); 
824        I != E; ++I) {
825     if (Optional<CFGStmt> cs = I->getAs<CFGStmt>())
826       tf.Visit(const_cast<Stmt*>(cs->getStmt()));
827   }
828   return vals.updateValueVectorWithScratch(block);
829 }
830
831 /// PruneBlocksHandler is a special UninitVariablesHandler that is used
832 /// to detect when a CFGBlock has any *potential* use of an uninitialized
833 /// variable.  It is mainly used to prune out work during the final
834 /// reporting pass.
835 namespace {
836 struct PruneBlocksHandler : public UninitVariablesHandler {
837   PruneBlocksHandler(unsigned numBlocks)
838     : hadUse(numBlocks, false), hadAnyUse(false),
839       currentBlock(0) {}
840
841   ~PruneBlocksHandler() override {}
842
843   /// Records if a CFGBlock had a potential use of an uninitialized variable.
844   llvm::BitVector hadUse;
845
846   /// Records if any CFGBlock had a potential use of an uninitialized variable.
847   bool hadAnyUse;
848
849   /// The current block to scribble use information.
850   unsigned currentBlock;
851
852   void handleUseOfUninitVariable(const VarDecl *vd,
853                                  const UninitUse &use) override {
854     hadUse[currentBlock] = true;
855     hadAnyUse = true;
856   }
857
858   /// Called when the uninitialized variable analysis detects the
859   /// idiom 'int x = x'.  All other uses of 'x' within the initializer
860   /// are handled by handleUseOfUninitVariable.
861   void handleSelfInit(const VarDecl *vd) override {
862     hadUse[currentBlock] = true;
863     hadAnyUse = true;
864   }
865 };
866 }
867
868 void clang::runUninitializedVariablesAnalysis(
869     const DeclContext &dc,
870     const CFG &cfg,
871     AnalysisDeclContext &ac,
872     UninitVariablesHandler &handler,
873     UninitVariablesAnalysisStats &stats) {
874   CFGBlockValues vals(cfg);
875   vals.computeSetOfDeclarations(dc);
876   if (vals.hasNoDeclarations())
877     return;
878
879   stats.NumVariablesAnalyzed = vals.getNumEntries();
880
881   // Precompute which expressions are uses and which are initializations.
882   ClassifyRefs classification(ac);
883   cfg.VisitBlockStmts(classification);
884
885   // Mark all variables uninitialized at the entry.
886   const CFGBlock &entry = cfg.getEntry();
887   ValueVector &vec = vals.getValueVector(&entry);
888   const unsigned n = vals.getNumEntries();
889   for (unsigned j = 0; j < n ; ++j) {
890     vec[j] = Uninitialized;
891   }
892
893   // Proceed with the workist.
894   DataflowWorklist worklist(cfg, *ac.getAnalysis<PostOrderCFGView>());
895   llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
896   worklist.enqueueSuccessors(&cfg.getEntry());
897   llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
898   wasAnalyzed[cfg.getEntry().getBlockID()] = true;
899   PruneBlocksHandler PBH(cfg.getNumBlockIDs());
900
901   while (const CFGBlock *block = worklist.dequeue()) {
902     PBH.currentBlock = block->getBlockID();
903
904     // Did the block change?
905     bool changed = runOnBlock(block, cfg, ac, vals,
906                               classification, wasAnalyzed, PBH);
907     ++stats.NumBlockVisits;
908     if (changed || !previouslyVisited[block->getBlockID()])
909       worklist.enqueueSuccessors(block);    
910     previouslyVisited[block->getBlockID()] = true;
911   }
912
913   if (!PBH.hadAnyUse)
914     return;
915
916   // Run through the blocks one more time, and report uninitialized variables.
917   for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
918     const CFGBlock *block = *BI;
919     if (PBH.hadUse[block->getBlockID()]) {
920       runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler);
921       ++stats.NumBlockVisits;
922     }
923   }
924 }
925
926 UninitVariablesHandler::~UninitVariablesHandler() {}