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" |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/LocalCheckers.h" |
| 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 19 | #include "DataflowSolver.h" |
| 20 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 26 | // Dataflow initialization logic. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 31 | class RegisterDeclsExprs : public CFGRecStmtDeclVisitor<RegisterDeclsExprs> { |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 32 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 33 | public: |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 34 | RegisterDeclsExprs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 36 | void VisitBlockVarDecl(BlockVarDecl* VD) { |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 37 | if (!AD.isTracked(VD)) AD[VD] = AD.NumDecls++; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Ted Kremenek | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 40 | void BlockStmt_VisitExpr(Expr* E) { |
| 41 | if (!AD.isTracked(E)) AD[E] = AD.NumBlockExprs++; |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +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 | 1ff4115 | 2007-09-19 18:00:03 +0000 | [diff] [blame] | 48 | RegisterDeclsExprs R(this->getAnalysisData()); |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 49 | cfg.VisitBlockStmts(R); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 53 | // Transfer functions. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 55 | |
| 56 | namespace { |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 58 | class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> { |
| 59 | UninitializedValues::ValTy V; |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 60 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | e6148f3 | 2007-09-17 21:59:08 +0000 | [diff] [blame] | 61 | bool InitWithAssigns; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 62 | public: |
Ted Kremenek | e6148f3 | 2007-09-17 21:59:08 +0000 | [diff] [blame] | 63 | TransferFuncs(UninitializedValues::AnalysisDataTy& ad, |
| 64 | bool init_with_assigns=true) : |
| 65 | AD(ad), InitWithAssigns(init_with_assigns) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 66 | V.resetValues(AD); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | UninitializedValues::ValTy& getVal() { return V; } |
Ted Kremenek | 3e03975 | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 71 | bool VisitDeclRefExpr(DeclRefExpr* DR); |
| 72 | bool VisitBinaryOperator(BinaryOperator* B); |
| 73 | bool VisitUnaryOperator(UnaryOperator* U); |
| 74 | bool VisitStmt(Stmt* S); |
| 75 | bool VisitCallExpr(CallExpr* C); |
| 76 | bool BlockStmt_VisitExpr(Expr* E); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 77 | bool VisitDeclStmt(DeclStmt* D); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 79 | BlockVarDecl* FindBlockVarDecl(Stmt* S); |
| 80 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 81 | static inline bool Initialized() { return true; } |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 82 | static inline bool Uninitialized() { return false; } |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 83 | }; |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 87 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 88 | if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 90 | return V.getBitRef(VD,AD); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 91 | } |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 92 | else return Initialized(); |
| 93 | } |
| 94 | |
| 95 | BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) { |
| 96 | for (;;) { |
| 97 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { |
| 98 | S = P->getSubExpr(); |
| 99 | continue; |
| 100 | } |
| 101 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 102 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) |
| 103 | return VD; |
| 104 | |
| 105 | return NULL; |
| 106 | } |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | e6148f3 | 2007-09-17 21:59:08 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 111 | if (CFG::hasImplicitControlFlow(B)) |
| 112 | return V.getBitRef(B,AD); |
| 113 | |
| 114 | if (B->isAssignmentOp()) |
| 115 | // Get the Decl for the LHS (if any). |
| 116 | if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS())) |
| 117 | if(InitWithAssigns) { |
| 118 | // Pseudo-hack to prevent cascade of warnings. If the RHS uses |
| 119 | // an uninitialized value, then we are already going to flag a warning |
| 120 | // for the RHS, or for the root "source" of the unintialized values. |
| 121 | // Thus, propogating uninitialized doesn't make sense, since we are |
| 122 | // just adding extra messages that don't |
| 123 | // contribute to diagnosing the bug. In InitWithAssigns mode |
| 124 | // we unconditionally set the assigned variable to Initialized to |
| 125 | // prevent Uninitialized propogation. |
| 126 | return V.getBitRef(VD,AD) = Initialized(); |
| 127 | } |
| 128 | else return V.getBitRef(VD,AD) = Visit(B->getRHS()); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 130 | return VisitStmt(B); |
| 131 | } |
| 132 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 133 | bool TransferFuncs::VisitDeclStmt(DeclStmt* S) { |
| 134 | bool x = Initialized(); |
| 135 | |
| 136 | for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator()) |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 137 | if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D)) |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 138 | if (Stmt* I = VD->getInit()) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 139 | x = V.getBitRef(cast<Expr>(I),AD); |
| 140 | V.getBitRef(VD,AD) = x; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return x; |
| 144 | } |
| 145 | |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 146 | bool TransferFuncs::VisitCallExpr(CallExpr* C) { |
Ted Kremenek | a1d3586 | 2007-09-18 21:47:41 +0000 | [diff] [blame] | 147 | VisitChildren(C); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 148 | return Initialized(); |
| 149 | } |
| 150 | |
| 151 | bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
| 152 | switch (U->getOpcode()) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 153 | case UnaryOperator::AddrOf: |
| 154 | // For "&x", treat "x" as now being initialized. |
| 155 | if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr())) |
| 156 | V.getBitRef(VD,AD) = Initialized(); |
| 157 | else |
| 158 | return Visit(U->getSubExpr()); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 159 | |
| 160 | default: |
| 161 | return Visit(U->getSubExpr()); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | bool TransferFuncs::VisitStmt(Stmt* S) { |
| 166 | bool x = Initialized(); |
| 167 | |
| 168 | // We don't stop at the first subexpression that is Uninitialized because |
| 169 | // evaluating some subexpressions may result in propogating "Uninitialized" |
| 170 | // or "Initialized" to variables referenced in the other subexpressions. |
| 171 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 172 | if (Visit(*I) == Uninitialized()) x = Uninitialized(); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 173 | |
| 174 | return x; |
| 175 | } |
| 176 | |
| 177 | bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 178 | assert (AD.isTracked(E)); |
| 179 | return V.getBitRef(E,AD) = Visit(E); |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 182 | } // end anonymous namespace |
| 183 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 185 | // Merge operator. |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 186 | // |
| 187 | // In our transfer functions we take the approach that any |
| 188 | // combination of unintialized values, e.g. Unitialized + ___ = Unitialized. |
| 189 | // |
| 190 | // Merges take the opposite approach. |
| 191 | // |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 192 | // In the merge of dataflow values we prefer unsoundness, and |
Ted Kremenek | 334b30a | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 193 | // prefer false negatives to false positives. At merges, if a value for a |
| 194 | // tracked Decl is EVER initialized in any of the predecessors we treat it as |
| 195 | // initialized at the confluence point. |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 197 | |
| 198 | namespace { |
| 199 | struct Merge { |
| 200 | void operator()(UninitializedValues::ValTy& Dst, |
| 201 | UninitializedValues::ValTy& Src) { |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 202 | assert (Src.sizesEqual(Dst) && "BV sizes do not match."); |
Ted Kremenek | 68447a6 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 203 | Dst.DeclBV |= Src.DeclBV; |
| 204 | Dst.ExprBV |= Src.ExprBV; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 205 | } |
| 206 | }; |
| 207 | } // end anonymous namespace |
| 208 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 209 | //===----------------------------------------------------------------------===// |
| 210 | // Unitialized values checker. Scan an AST and flag variable uses |
| 211 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 213 | UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {} |
| 214 | |
| 215 | namespace { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 216 | class UninitializedValuesChecker : public UninitializedValues::ObserverTy { |
| 217 | ASTContext &Ctx; |
| 218 | Diagnostic &Diags; |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 219 | llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 220 | |
| 221 | public: |
| 222 | UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags) |
| 223 | : Ctx(ctx), Diags(diags) {} |
| 224 | |
| 225 | virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V, |
| 226 | UninitializedValues::AnalysisDataTy& AD, |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 227 | DeclRefExpr* DR, BlockVarDecl* VD) { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | f92ba51 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 229 | assert ( AD.isTracked(VD) && "Unknown VarDecl."); |
| 230 | |
| 231 | if (V.getBitRef(VD,AD) == TransferFuncs::Uninitialized()) |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 232 | if (AlreadyWarned.insert(VD)) |
| 233 | Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val); |
| 234 | } |
| 235 | }; |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 236 | } // end anonymous namespace |
| 237 | |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 238 | namespace clang { |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 239 | void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 240 | |
| 241 | typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 243 | // Compute the unitialized values information. |
| 244 | UninitializedValues U; |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 245 | Solver S(U); |
Ted Kremenek | 3871d8e | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 246 | S.runOnCFG(cfg); |
| 247 | |
| 248 | // Scan for DeclRefExprs that use uninitialized values. |
| 249 | UninitializedValuesChecker Observer(Ctx,Diags); |
| 250 | U.getAnalysisData().Observer = &Observer; |
Ted Kremenek | 3fa5e09 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 251 | S.runOnAllBlocks(cfg); |
Ted Kremenek | 7f49f50 | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 252 | } |
Ted Kremenek | 3fa5e09 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 253 | } // end namespace clang |