1 //===- UninitializedValues.cpp - Find Uninitialized Values ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements uninitialized values analysis for source-level CFGs.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Analysis/Analyses/UninitializedValues.h"
14 #include "clang/AST/Attr.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclBase.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/OperationKinds.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/AST/StmtObjC.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Analysis/Analyses/PostOrderCFGView.h"
24 #include "clang/Analysis/AnalysisDeclContext.h"
25 #include "clang/Analysis/CFG.h"
26 #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
27 #include "clang/Analysis/FlowSensitive/DataflowWorklist.h"
28 #include "clang/Basic/LLVM.h"
29 #include "llvm/ADT/BitVector.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/None.h"
32 #include "llvm/ADT/Optional.h"
33 #include "llvm/ADT/PackedVector.h"
34 #include "llvm/ADT/SmallBitVector.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/Support/Casting.h"
40 using namespace clang;
42 #define DEBUG_LOGGING 0
44 static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
45 if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
46 !vd->isExceptionVariable() && !vd->isInitCapture() &&
47 !vd->isImplicit() && vd->getDeclContext() == dc) {
48 QualType ty = vd->getType();
49 return ty->isScalarType() || ty->isVectorType() || ty->isRecordType();
54 //------------------------------------------------------------------------====//
55 // DeclToIndex: a mapping from Decls we track to value indices.
56 //====------------------------------------------------------------------------//
61 llvm::DenseMap<const VarDecl *, unsigned> map;
64 DeclToIndex() = default;
66 /// Compute the actual mapping from declarations to bits.
67 void computeMap(const DeclContext &dc);
69 /// Return the number of declarations in the map.
70 unsigned size() const { return map.size(); }
72 /// Returns the bit vector index for a given declaration.
73 Optional<unsigned> getValueIndex(const VarDecl *d) const;
78 void DeclToIndex::computeMap(const DeclContext &dc) {
80 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
82 for ( ; I != E; ++I) {
83 const VarDecl *vd = *I;
84 if (isTrackedVar(vd, &dc))
89 Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
90 llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
96 //------------------------------------------------------------------------====//
97 // CFGBlockValues: dataflow values for CFG blocks.
98 //====------------------------------------------------------------------------//
100 // These values are defined in such a way that a merge can be done using
102 enum Value { Unknown = 0x0, /* 00 */
103 Initialized = 0x1, /* 01 */
104 Uninitialized = 0x2, /* 10 */
105 MayUninitialized = 0x3 /* 11 */ };
107 static bool isUninitialized(const Value v) {
108 return v >= Uninitialized;
111 static bool isAlwaysUninit(const Value v) {
112 return v == Uninitialized;
117 using ValueVector = llvm::PackedVector<Value, 2, llvm::SmallBitVector>;
119 class CFGBlockValues {
121 SmallVector<ValueVector, 8> vals;
123 DeclToIndex declToIndex;
126 CFGBlockValues(const CFG &cfg);
128 unsigned getNumEntries() const { return declToIndex.size(); }
130 void computeSetOfDeclarations(const DeclContext &dc);
132 ValueVector &getValueVector(const CFGBlock *block) {
133 return vals[block->getBlockID()];
136 void setAllScratchValues(Value V);
137 void mergeIntoScratch(ValueVector const &source, bool isFirst);
138 bool updateValueVectorWithScratch(const CFGBlock *block);
140 bool hasNoDeclarations() const {
141 return declToIndex.size() == 0;
146 ValueVector::reference operator[](const VarDecl *vd);
148 Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
150 const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
151 assert(idx.hasValue());
152 return getValueVector(block)[idx.getValue()];
158 CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {}
160 void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
161 declToIndex.computeMap(dc);
162 unsigned decls = declToIndex.size();
163 scratch.resize(decls);
164 unsigned n = cfg.getNumBlockIDs();
168 for (auto &val : vals)
173 static void printVector(const CFGBlock *block, ValueVector &bv,
175 llvm::errs() << block->getBlockID() << " :";
176 for (const auto &i : bv)
177 llvm::errs() << ' ' << i;
178 llvm::errs() << " : " << num << '\n';
182 void CFGBlockValues::setAllScratchValues(Value V) {
183 for (unsigned I = 0, E = scratch.size(); I != E; ++I)
187 void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
195 bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
196 ValueVector &dst = getValueVector(block);
197 bool changed = (dst != scratch);
201 printVector(block, scratch, 0);
206 void CFGBlockValues::resetScratch() {
210 ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
211 const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
212 assert(idx.hasValue());
213 return scratch[idx.getValue()];
216 //------------------------------------------------------------------------====//
217 // Classification of DeclRefExprs as use or initialization.
218 //====------------------------------------------------------------------------//
222 class FindVarResult {
224 const DeclRefExpr *dr;
227 FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {}
229 const DeclRefExpr *getDeclRefExpr() const { return dr; }
230 const VarDecl *getDecl() const { return vd; }
235 static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
237 Ex = Ex->IgnoreParenNoopCasts(C);
238 if (const auto *CE = dyn_cast<CastExpr>(Ex)) {
239 if (CE->getCastKind() == CK_LValueBitCast) {
240 Ex = CE->getSubExpr();
249 /// If E is an expression comprising a reference to a single variable, find that
251 static FindVarResult findVar(const Expr *E, const DeclContext *DC) {
252 if (const auto *DRE =
253 dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E)))
254 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
255 if (isTrackedVar(VD, DC))
256 return FindVarResult(VD, DRE);
257 return FindVarResult(nullptr, nullptr);
262 /// Classify each DeclRefExpr as an initialization or a use. Any
263 /// DeclRefExpr which isn't explicitly classified will be assumed to have
264 /// escaped the analysis and will be treated as an initialization.
265 class ClassifyRefs : public StmtVisitor<ClassifyRefs> {
276 const DeclContext *DC;
277 llvm::DenseMap<const DeclRefExpr *, Class> Classification;
279 bool isTrackedVar(const VarDecl *VD) const {
280 return ::isTrackedVar(VD, DC);
283 void classify(const Expr *E, Class C);
286 ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {}
288 void VisitDeclStmt(DeclStmt *DS);
289 void VisitUnaryOperator(UnaryOperator *UO);
290 void VisitBinaryOperator(BinaryOperator *BO);
291 void VisitCallExpr(CallExpr *CE);
292 void VisitCastExpr(CastExpr *CE);
293 void VisitOMPExecutableDirective(OMPExecutableDirective *ED);
295 void operator()(Stmt *S) { Visit(S); }
297 Class get(const DeclRefExpr *DRE) const {
298 llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I
299 = Classification.find(DRE);
300 if (I != Classification.end())
303 const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
304 if (!VD || !isTrackedVar(VD))
313 static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) {
314 if (VD->getType()->isRecordType())
316 if (Expr *Init = VD->getInit()) {
318 dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init));
319 if (DRE && DRE->getDecl() == VD)
325 void ClassifyRefs::classify(const Expr *E, Class C) {
326 // The result of a ?: could also be an lvalue.
327 E = E->IgnoreParens();
328 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
329 classify(CO->getTrueExpr(), C);
330 classify(CO->getFalseExpr(), C);
334 if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) {
335 classify(BCO->getFalseExpr(), C);
339 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) {
340 classify(OVE->getSourceExpr(), C);
344 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
345 if (const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
346 if (!VD->isStaticDataMember())
347 classify(ME->getBase(), C);
352 if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
353 switch (BO->getOpcode()) {
356 classify(BO->getLHS(), C);
359 classify(BO->getRHS(), C);
366 FindVarResult Var = findVar(E, DC);
367 if (const DeclRefExpr *DRE = Var.getDeclRefExpr())
368 Classification[DRE] = std::max(Classification[DRE], C);
371 void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
372 for (auto *DI : DS->decls()) {
373 auto *VD = dyn_cast<VarDecl>(DI);
374 if (VD && isTrackedVar(VD))
375 if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
376 Classification[DRE] = SelfInit;
380 void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
381 // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this
382 // is not a compound-assignment, we will treat it as initializing the variable
383 // when TransferFunctions visits it. A compound-assignment does not affect
384 // whether a variable is uninitialized, and there's no point counting it as a
386 if (BO->isCompoundAssignmentOp())
387 classify(BO->getLHS(), Use);
388 else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma)
389 classify(BO->getLHS(), Ignore);
392 void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) {
393 // Increment and decrement are uses despite there being no lvalue-to-rvalue
395 if (UO->isIncrementDecrementOp())
396 classify(UO->getSubExpr(), Use);
399 void ClassifyRefs::VisitOMPExecutableDirective(OMPExecutableDirective *ED) {
400 for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses()))
401 classify(cast<Expr>(S), Use);
404 static bool isPointerToConst(const QualType &QT) {
405 return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified();
408 static bool hasTrivialBody(CallExpr *CE) {
409 if (FunctionDecl *FD = CE->getDirectCallee()) {
410 if (FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
411 return FTD->getTemplatedDecl()->hasTrivialBody();
412 return FD->hasTrivialBody();
417 void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
418 // Classify arguments to std::move as used.
419 if (CE->isCallToStdMove()) {
420 // RecordTypes are handled in SemaDeclCXX.cpp.
421 if (!CE->getArg(0)->getType()->isRecordType())
422 classify(CE->getArg(0), Use);
425 bool isTrivialBody = hasTrivialBody(CE);
426 // If a value is passed by const pointer to a function,
427 // we should not assume that it is initialized by the call, and we
428 // conservatively do not assume that it is used.
429 // If a value is passed by const reference to a function,
430 // it should already be initialized.
431 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
433 if ((*I)->isGLValue()) {
434 if ((*I)->getType().isConstQualified())
435 classify((*I), isTrivialBody ? Ignore : ConstRefUse);
436 } else if (isPointerToConst((*I)->getType())) {
437 const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
438 const auto *UO = dyn_cast<UnaryOperator>(Ex);
439 if (UO && UO->getOpcode() == UO_AddrOf)
440 Ex = UO->getSubExpr();
441 classify(Ex, Ignore);
446 void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
447 if (CE->getCastKind() == CK_LValueToRValue)
448 classify(CE->getSubExpr(), Use);
449 else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) {
450 if (CSE->getType()->isVoidType()) {
451 // Squelch any detected load of an uninitialized value if
452 // we cast it to void.
454 classify(CSE->getSubExpr(), Ignore);
459 //------------------------------------------------------------------------====//
460 // Transfer function for uninitialized values analysis.
461 //====------------------------------------------------------------------------//
465 class TransferFunctions : public StmtVisitor<TransferFunctions> {
466 CFGBlockValues &vals;
468 const CFGBlock *block;
469 AnalysisDeclContext ∾
470 const ClassifyRefs &classification;
471 ObjCNoReturn objCNoRet;
472 UninitVariablesHandler &handler;
475 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
476 const CFGBlock *block, AnalysisDeclContext &ac,
477 const ClassifyRefs &classification,
478 UninitVariablesHandler &handler)
479 : vals(vals), cfg(cfg), block(block), ac(ac),
480 classification(classification), objCNoRet(ac.getASTContext()),
483 void reportUse(const Expr *ex, const VarDecl *vd);
484 void reportConstRefUse(const Expr *ex, const VarDecl *vd);
486 void VisitBinaryOperator(BinaryOperator *bo);
487 void VisitBlockExpr(BlockExpr *be);
488 void VisitCallExpr(CallExpr *ce);
489 void VisitDeclRefExpr(DeclRefExpr *dr);
490 void VisitDeclStmt(DeclStmt *ds);
491 void VisitGCCAsmStmt(GCCAsmStmt *as);
492 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
493 void VisitObjCMessageExpr(ObjCMessageExpr *ME);
494 void VisitOMPExecutableDirective(OMPExecutableDirective *ED);
496 bool isTrackedVar(const VarDecl *vd) {
497 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
500 FindVarResult findVar(const Expr *ex) {
501 return ::findVar(ex, cast<DeclContext>(ac.getDecl()));
504 UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
505 UninitUse Use(ex, isAlwaysUninit(v));
507 assert(isUninitialized(v));
508 if (Use.getKind() == UninitUse::Always)
511 // If an edge which leads unconditionally to this use did not initialize
512 // the variable, we can say something stronger than 'may be uninitialized':
513 // we can say 'either it's used uninitialized or you have dead code'.
515 // We track the number of successors of a node which have been visited, and
516 // visit a node once we have visited all of its successors. Only edges where
517 // the variable might still be uninitialized are followed. Since a variable
518 // can't transfer from being initialized to being uninitialized, this will
519 // trace out the subgraph which inevitably leads to the use and does not
520 // initialize the variable. We do not want to skip past loops, since their
521 // non-termination might be correlated with the initialization condition.
525 // void f(bool a, bool b) {
530 // block4: } else if (b) {
531 // block5: while (!a) {
532 // block6: do_work(&a);
541 // Starting from the maybe-uninitialized use in block 9:
542 // * Block 7 is not visited because we have only visited one of its two
544 // * Block 8 is visited because we've visited its only successor.
546 // * Block 7 is visited because we've now visited both of its successors.
548 // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
549 // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
550 // * Block 3 is not visited because it initializes 'n'.
551 // Now the algorithm terminates, having visited blocks 7 and 8, and having
552 // found the frontier is blocks 2, 4, and 5.
554 // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
555 // and 4), so we report that any time either of those edges is taken (in
556 // each case when 'b == false'), 'n' is used uninitialized.
557 SmallVector<const CFGBlock*, 32> Queue;
558 SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
559 Queue.push_back(block);
560 // Specify that we've already visited all successors of the starting block.
561 // This has the dual purpose of ensuring we never add it to the queue, and
562 // of marking it as not being a candidate element of the frontier.
563 SuccsVisited[block->getBlockID()] = block->succ_size();
564 while (!Queue.empty()) {
565 const CFGBlock *B = Queue.pop_back_val();
567 // If the use is always reached from the entry block, make a note of that.
568 if (B == &cfg.getEntry())
569 Use.setUninitAfterCall();
571 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
573 const CFGBlock *Pred = *I;
577 Value AtPredExit = vals.getValue(Pred, B, vd);
578 if (AtPredExit == Initialized)
579 // This block initializes the variable.
581 if (AtPredExit == MayUninitialized &&
582 vals.getValue(B, nullptr, vd) == Uninitialized) {
583 // This block declares the variable (uninitialized), and is reachable
584 // from a block that initializes the variable. We can't guarantee to
585 // give an earlier location for the diagnostic (and it appears that
586 // this code is intended to be reachable) so give a diagnostic here
587 // and go no further down this path.
588 Use.setUninitAfterDecl();
592 if (AtPredExit == MayUninitialized) {
593 // If the predecessor's terminator is an "asm goto" that initializes
594 // the variable, then it won't be counted as "initialized" on the
595 // non-fallthrough paths.
596 CFGTerminator term = Pred->getTerminator();
597 if (const auto *as = dyn_cast_or_null<GCCAsmStmt>(term.getStmt())) {
598 const CFGBlock *fallthrough = *Pred->succ_begin();
599 if (as->isAsmGoto() &&
600 llvm::any_of(as->outputs(), [&](const Expr *output) {
601 return vd == findVar(output).getDecl() &&
602 llvm::any_of(as->labels(),
603 [&](const AddrLabelExpr *label) {
604 return label->getLabel()->getStmt() == B->Label &&
608 Use.setUninitAfterDecl();
614 unsigned &SV = SuccsVisited[Pred->getBlockID()];
616 // When visiting the first successor of a block, mark all NULL
617 // successors as having been visited.
618 for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(),
619 SE = Pred->succ_end();
625 if (++SV == Pred->succ_size())
626 // All paths from this block lead to the use and don't initialize the
628 Queue.push_back(Pred);
632 // Scan the frontier, looking for blocks where the variable was
634 for (const auto *Block : cfg) {
635 unsigned BlockID = Block->getBlockID();
636 const Stmt *Term = Block->getTerminatorStmt();
637 if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
639 // This block inevitably leads to the use. If we have an edge from here
640 // to a post-dominator block, and the variable is uninitialized on that
641 // edge, we have found a bug.
642 for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
643 E = Block->succ_end(); I != E; ++I) {
644 const CFGBlock *Succ = *I;
645 if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
646 vals.getValue(Block, Succ, vd) == Uninitialized) {
647 // Switch cases are a special case: report the label to the caller
648 // as the 'terminator', not the switch statement itself. Suppress
649 // situations where no label matched: we can't be sure that's
651 if (isa<SwitchStmt>(Term)) {
652 const Stmt *Label = Succ->getLabel();
653 if (!Label || !isa<SwitchCase>(Label))
654 // Might not be possible.
656 UninitUse::Branch Branch;
657 Branch.Terminator = Label;
658 Branch.Output = 0; // Ignored.
659 Use.addUninitBranch(Branch);
661 UninitUse::Branch Branch;
662 Branch.Terminator = Term;
663 Branch.Output = I - Block->succ_begin();
664 Use.addUninitBranch(Branch);
677 void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
679 if (isUninitialized(v))
680 handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
683 void TransferFunctions::reportConstRefUse(const Expr *ex, const VarDecl *vd) {
685 if (isAlwaysUninit(v))
686 handler.handleConstRefUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
689 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) {
690 // This represents an initialization of the 'element' value.
691 if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) {
692 const auto *VD = cast<VarDecl>(DS->getSingleDecl());
693 if (isTrackedVar(VD))
694 vals[VD] = Initialized;
698 void TransferFunctions::VisitOMPExecutableDirective(
699 OMPExecutableDirective *ED) {
700 for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) {
701 assert(S && "Expected non-null used-in-clause child.");
704 if (!ED->isStandaloneDirective())
705 Visit(ED->getStructuredBlock());
708 void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
709 const BlockDecl *bd = be->getBlockDecl();
710 for (const auto &I : bd->captures()) {
711 const VarDecl *vd = I.getVariable();
712 if (!isTrackedVar(vd))
715 vals[vd] = Initialized;
722 void TransferFunctions::VisitCallExpr(CallExpr *ce) {
723 if (Decl *Callee = ce->getCalleeDecl()) {
724 if (Callee->hasAttr<ReturnsTwiceAttr>()) {
725 // After a call to a function like setjmp or vfork, any variable which is
726 // initialized anywhere within this function may now be initialized. For
727 // now, just assume such a call initializes all variables. FIXME: Only
728 // mark variables as initialized if they have an initializer which is
729 // reachable from here.
730 vals.setAllScratchValues(Initialized);
732 else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) {
733 // Functions labeled like "analyzer_noreturn" are often used to denote
734 // "panic" functions that in special debug situations can still return,
735 // but for the most part should not be treated as returning. This is a
736 // useful annotation borrowed from the static analyzer that is useful for
737 // suppressing branch-specific false positives when we call one of these
738 // functions but keep pretending the path continues (when in reality the
739 // user doesn't care).
740 vals.setAllScratchValues(Unknown);
745 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
746 switch (classification.get(dr)) {
747 case ClassifyRefs::Ignore:
749 case ClassifyRefs::Use:
750 reportUse(dr, cast<VarDecl>(dr->getDecl()));
752 case ClassifyRefs::Init:
753 vals[cast<VarDecl>(dr->getDecl())] = Initialized;
755 case ClassifyRefs::SelfInit:
756 handler.handleSelfInit(cast<VarDecl>(dr->getDecl()));
758 case ClassifyRefs::ConstRefUse:
759 reportConstRefUse(dr, cast<VarDecl>(dr->getDecl()));
764 void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
765 if (BO->getOpcode() == BO_Assign) {
766 FindVarResult Var = findVar(BO->getLHS());
767 if (const VarDecl *VD = Var.getDecl())
768 vals[VD] = Initialized;
772 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
773 for (auto *DI : DS->decls()) {
774 auto *VD = dyn_cast<VarDecl>(DI);
775 if (VD && isTrackedVar(VD)) {
776 if (getSelfInitExpr(VD)) {
777 // If the initializer consists solely of a reference to itself, we
778 // explicitly mark the variable as uninitialized. This allows code
779 // like the following:
783 // to deliberately leave a variable uninitialized. Different analysis
784 // clients can detect this pattern and adjust their reporting
785 // appropriately, but we need to continue to analyze subsequent uses
787 vals[VD] = Uninitialized;
788 } else if (VD->getInit()) {
789 // Treat the new variable as initialized.
790 vals[VD] = Initialized;
792 // No initializer: the variable is now uninitialized. This matters
799 // FIXME: Mark the variable as uninitialized whenever its scope is
800 // left, since its scope could be re-entered by a jump over the
802 vals[VD] = Uninitialized;
808 void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) {
809 // An "asm goto" statement is a terminator that may initialize some variables.
810 if (!as->isAsmGoto())
813 for (const Expr *o : as->outputs())
814 if (const VarDecl *VD = findVar(o).getDecl())
815 if (vals[VD] != Initialized)
816 // If the variable isn't initialized by the time we get here, then we
817 // mark it as potentially uninitialized for those cases where it's used
818 // on an indirect path, where it's not guaranteed to be defined.
819 vals[VD] = MayUninitialized;
822 void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
823 // If the Objective-C message expression is an implicit no-return that
824 // is not modeled in the CFG, set the tracked dataflow values to Unknown.
825 if (objCNoRet.isImplicitNoReturn(ME)) {
826 vals.setAllScratchValues(Unknown);
830 //------------------------------------------------------------------------====//
831 // High-level "driver" logic for uninitialized values analysis.
832 //====------------------------------------------------------------------------//
834 static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
835 AnalysisDeclContext &ac, CFGBlockValues &vals,
836 const ClassifyRefs &classification,
837 llvm::BitVector &wasAnalyzed,
838 UninitVariablesHandler &handler) {
839 wasAnalyzed[block->getBlockID()] = true;
841 // Merge in values of predecessor blocks.
843 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
844 E = block->pred_end(); I != E; ++I) {
845 const CFGBlock *pred = *I;
848 if (wasAnalyzed[pred->getBlockID()]) {
849 vals.mergeIntoScratch(vals.getValueVector(pred), isFirst);
853 // Apply the transfer function.
854 TransferFunctions tf(vals, cfg, block, ac, classification, handler);
855 for (const auto &I : *block) {
856 if (Optional<CFGStmt> cs = I.getAs<CFGStmt>())
857 tf.Visit(const_cast<Stmt *>(cs->getStmt()));
859 CFGTerminator terminator = block->getTerminator();
860 if (auto *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt()))
863 return vals.updateValueVectorWithScratch(block);
868 /// PruneBlocksHandler is a special UninitVariablesHandler that is used
869 /// to detect when a CFGBlock has any *potential* use of an uninitialized
870 /// variable. It is mainly used to prune out work during the final
872 struct PruneBlocksHandler : public UninitVariablesHandler {
873 /// Records if a CFGBlock had a potential use of an uninitialized variable.
874 llvm::BitVector hadUse;
876 /// Records if any CFGBlock had a potential use of an uninitialized variable.
877 bool hadAnyUse = false;
879 /// The current block to scribble use information.
880 unsigned currentBlock = 0;
882 PruneBlocksHandler(unsigned numBlocks) : hadUse(numBlocks, false) {}
884 ~PruneBlocksHandler() override = default;
886 void handleUseOfUninitVariable(const VarDecl *vd,
887 const UninitUse &use) override {
888 hadUse[currentBlock] = true;
892 void handleConstRefUseOfUninitVariable(const VarDecl *vd,
893 const UninitUse &use) override {
894 hadUse[currentBlock] = true;
898 /// Called when the uninitialized variable analysis detects the
899 /// idiom 'int x = x'. All other uses of 'x' within the initializer
900 /// are handled by handleUseOfUninitVariable.
901 void handleSelfInit(const VarDecl *vd) override {
902 hadUse[currentBlock] = true;
909 void clang::runUninitializedVariablesAnalysis(
910 const DeclContext &dc,
912 AnalysisDeclContext &ac,
913 UninitVariablesHandler &handler,
914 UninitVariablesAnalysisStats &stats) {
915 CFGBlockValues vals(cfg);
916 vals.computeSetOfDeclarations(dc);
917 if (vals.hasNoDeclarations())
920 stats.NumVariablesAnalyzed = vals.getNumEntries();
922 // Precompute which expressions are uses and which are initializations.
923 ClassifyRefs classification(ac);
924 cfg.VisitBlockStmts(classification);
926 // Mark all variables uninitialized at the entry.
927 const CFGBlock &entry = cfg.getEntry();
928 ValueVector &vec = vals.getValueVector(&entry);
929 const unsigned n = vals.getNumEntries();
930 for (unsigned j = 0; j < n; ++j) {
931 vec[j] = Uninitialized;
934 // Proceed with the workist.
935 ForwardDataflowWorklist worklist(cfg, ac);
936 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
937 worklist.enqueueSuccessors(&cfg.getEntry());
938 llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
939 wasAnalyzed[cfg.getEntry().getBlockID()] = true;
940 PruneBlocksHandler PBH(cfg.getNumBlockIDs());
942 while (const CFGBlock *block = worklist.dequeue()) {
943 PBH.currentBlock = block->getBlockID();
945 // Did the block change?
946 bool changed = runOnBlock(block, cfg, ac, vals,
947 classification, wasAnalyzed, PBH);
948 ++stats.NumBlockVisits;
949 if (changed || !previouslyVisited[block->getBlockID()])
950 worklist.enqueueSuccessors(block);
951 previouslyVisited[block->getBlockID()] = true;
957 // Run through the blocks one more time, and report uninitialized variables.
958 for (const auto *block : cfg)
959 if (PBH.hadUse[block->getBlockID()]) {
960 runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler);
961 ++stats.NumBlockVisits;
965 UninitVariablesHandler::~UninitVariablesHandler() = default;