Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 1 | //==- UninitializedValues.cpp - Find Unintialized Values --------*- C++ --*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source 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/Analysis/UninitializedValues.h" |
| 15 | #include "clang/Analysis/CFGVarDeclVisitor.h" |
| 16 | #include "clang/Analysis/CFGStmtVisitor.h" |
| 17 | #include "DataflowSolver.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | //===--------------------------------------------------------------------===// |
| 22 | // Dataflow initialization logic. |
| 23 | //===--------------------------------------------------------------------===// |
| 24 | |
| 25 | namespace { |
| 26 | |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 27 | class RegisterDeclsAndExprs : public CFGVarDeclVisitor<RegisterDeclsAndExprs> { |
| 28 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 29 | public: |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 30 | RegisterDeclsAndExprs(const CFG& cfg, UninitializedValues::AnalysisDataTy& ad) |
| 31 | : CFGVarDeclVisitor<RegisterDeclsAndExprs>(cfg), AD(ad) |
| 32 | {} |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 33 | |
| 34 | void VisitVarDecl(VarDecl* D) { |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 35 | if (AD.VMap.find(D) == AD.VMap.end()) |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 36 | AD.VMap[D] = AD.NumDecls++; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void BlockStmt_VisitExpr(Expr* E) { |
| 40 | if (AD.EMap.find(E) == AD.EMap.end()) |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 41 | AD.EMap[E] = AD.NumBlockExprs++; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 42 | } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | } // end anonymous namespace |
| 46 | |
| 47 | void UninitializedValues::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 48 | RegisterDeclsAndExprs R(cfg,this->getAnalysisData()); |
| 49 | R.VisitAllDecls(); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 50 | UninitializedValues::ValTy& V = getBlockDataMap()[&cfg.getEntry()]; |
| 51 | V.DeclBV.resize(getAnalysisData().NumDecls); |
| 52 | V.ExprBV.resize(getAnalysisData().NumBlockExprs); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | //===--------------------------------------------------------------------===// |
| 56 | // Transfer functions. |
| 57 | //===--------------------------------------------------------------------===// |
| 58 | |
| 59 | namespace { |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 60 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 61 | class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> { |
| 62 | UninitializedValues::ValTy V; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 63 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 64 | public: |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 65 | TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) { |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 66 | V.DeclBV.resize(AD.NumDecls); |
| 67 | V.ExprBV.resize(AD.NumBlockExprs); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | UninitializedValues::ValTy& getVal() { return V; } |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 72 | bool VisitDeclRefExpr(DeclRefExpr* DR); |
| 73 | bool VisitBinaryOperator(BinaryOperator* B); |
| 74 | bool VisitUnaryOperator(UnaryOperator* U); |
| 75 | bool VisitStmt(Stmt* S); |
| 76 | bool VisitCallExpr(CallExpr* C); |
| 77 | bool BlockStmt_VisitExpr(Expr* E); |
| 78 | |
| 79 | static inline bool Initialized() { return true; } |
| 80 | static inline bool Unintialized() { return false; } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 81 | }; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 82 | |
| 83 | |
| 84 | bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 85 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 86 | assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); |
| 87 | return V.DeclBV[ AD.VMap[VD] ]; |
| 88 | } |
| 89 | else |
| 90 | return Initialized(); |
| 91 | } |
| 92 | |
| 93 | bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
| 94 | if (CFG::hasImplicitControlFlow(B)) { |
| 95 | assert ( AD.EMap.find(B) != AD.EMap.end() && "Unknown block-level expr."); |
| 96 | return V.ExprBV[ AD.EMap[B] ]; |
| 97 | } |
| 98 | |
| 99 | return VisitStmt(B); |
| 100 | } |
| 101 | |
| 102 | bool TransferFuncs::VisitCallExpr(CallExpr* C) { |
| 103 | VisitStmt(C); |
| 104 | return Initialized(); |
| 105 | } |
| 106 | |
| 107 | bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
| 108 | switch (U->getOpcode()) { |
| 109 | case UnaryOperator::AddrOf: { |
| 110 | // Blast through parentheses and find the decl (if any). Treat it |
| 111 | // as initialized from this point forward. |
| 112 | for (Stmt* S = U->getSubExpr() ;; ) |
| 113 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) |
| 114 | S = P->getSubExpr(); |
| 115 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) { |
| 116 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 117 | assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl."); |
| 118 | V.DeclBV[ AD.VMap[VD] ] = Initialized(); |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | else { |
| 123 | // Evaluate the transfer function for subexpressions, even |
| 124 | // if we cannot reason more deeply about the &-expression. |
| 125 | return Visit(U->getSubExpr()); |
| 126 | } |
| 127 | |
| 128 | return Initialized(); |
| 129 | } |
| 130 | |
| 131 | default: |
| 132 | return Visit(U->getSubExpr()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool TransferFuncs::VisitStmt(Stmt* S) { |
| 137 | bool x = Initialized(); |
| 138 | |
| 139 | // We don't stop at the first subexpression that is Uninitialized because |
| 140 | // evaluating some subexpressions may result in propogating "Uninitialized" |
| 141 | // or "Initialized" to variables referenced in the other subexpressions. |
| 142 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
| 143 | if (Visit(*I) == Unintialized()) |
| 144 | x = Unintialized(); |
| 145 | |
| 146 | return x; |
| 147 | } |
| 148 | |
| 149 | bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
| 150 | assert ( AD.EMap.find(E) != AD.EMap.end() ); |
| 151 | return V.ExprBV[ AD.EMap[E] ] = Visit(E); |
| 152 | } |
| 153 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 154 | } // end anonymous namespace |
| 155 | |
| 156 | //===--------------------------------------------------------------------===// |
| 157 | // Merge operator. |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 158 | // |
| 159 | // In our transfer functions we take the approach that any |
| 160 | // combination of unintialized values, e.g. Unitialized + ___ = Unitialized. |
| 161 | // |
| 162 | // Merges take the opposite approach. |
| 163 | // |
| 164 | // In the merge of dataflow values (for Decls) we prefer unsoundness, and |
| 165 | // prefer false negatives to false positives. At merges, if a value for a |
| 166 | // tracked Decl is EVER initialized in any of the predecessors we treat it as |
| 167 | // initialized at the confluence point. |
| 168 | // |
| 169 | // For tracked CFGBlock-level expressions (such as the result of |
| 170 | // short-circuit), we do the opposite merge: if a value is EVER uninitialized |
| 171 | // in a predecessor we treat it as uninitalized at the confluence point. |
| 172 | // The reason we do this is because dataflow values for tracked Exprs are |
| 173 | // not as control-dependent as dataflow values for tracked Decls. |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 174 | //===--------------------------------------------------------------------===// |
| 175 | |
| 176 | namespace { |
| 177 | struct Merge { |
| 178 | void operator()(UninitializedValues::ValTy& Dst, |
| 179 | UninitializedValues::ValTy& Src) { |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame^] | 180 | assert (Dst.DeclBV.size() == Src.DeclBV.size() |
| 181 | && "Bitvector sizes do not match."); |
| 182 | |
| 183 | Dst.DeclBV |= Src.DeclBV; |
| 184 | |
| 185 | assert (Dst.ExprBV.size() == Src.ExprBV.size() |
| 186 | && "Bitvector sizes do not match."); |
| 187 | |
| 188 | Dst.ExprBV &= Src.ExprBV; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 189 | } |
| 190 | }; |
| 191 | } // end anonymous namespace |
| 192 | |
| 193 | //===--------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 194 | // External interface (driver logic). |
| 195 | //===--------------------------------------------------------------------===// |
| 196 | |
| 197 | void UninitializedValues::CheckUninitializedValues(const CFG& cfg) { |
| 198 | |
| 199 | typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver; |
| 200 | |
| 201 | UninitializedValues U; |
| 202 | |
| 203 | { // Compute the unitialized values information. |
| 204 | Solver S(U); |
| 205 | S.runOnCFG(cfg); |
| 206 | } |
| 207 | |
| 208 | // WarnObserver O; |
| 209 | Solver S(U); |
| 210 | |
| 211 | for (CFG::const_iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I) |
| 212 | S.runOnBlock(&*I); |
| 213 | } |