Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 1 | //==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Uninitialized Values analysis for source-level CFGs. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | cf6e41b | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Ted Kremenek | 11de5cb | 2007-09-20 21:42:55 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | cd5860c | 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 | 1de632b | 2007-09-25 21:00:24 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/FlowSensitive/DataflowSolver.h" |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
| 23 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 27 | // Dataflow initialization logic. |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 32 | class VISIBILITY_HIDDEN RegisterDecls |
| 33 | : public CFGRecStmtDeclVisitor<RegisterDecls> { |
| 34 | |
Ted Kremenek | 56d516d | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 35 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 36 | public: |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 37 | RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 606ceee | 2008-04-15 23:02:18 +0000 | [diff] [blame] | 39 | void VisitVarDecl(VarDecl* VD) { AD.Register(VD); } |
Ted Kremenek | 9f9141c | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 40 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | } // end anonymous namespace |
| 44 | |
| 45 | void UninitializedValues::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 46 | RegisterDecls R(getAnalysisData()); |
Ted Kremenek | a90b0d1 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 47 | cfg.VisitBlockStmts(R); |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 51 | // Transfer functions. |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 53 | |
| 54 | namespace { |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 55 | class VISIBILITY_HIDDEN TransferFuncs |
| 56 | : public CFGStmtVisitor<TransferFuncs,bool> { |
| 57 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 58 | UninitializedValues::ValTy V; |
Ted Kremenek | 56d516d | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 59 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 60 | public: |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 61 | TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 62 | |
| 63 | UninitializedValues::ValTy& getVal() { return V; } |
Ted Kremenek | 9f9141c | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 64 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | 56d516d | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 66 | void SetTopValue(UninitializedValues::ValTy& X) { |
| 67 | X.resetValues(AD); |
| 68 | } |
| 69 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 70 | bool VisitDeclRefExpr(DeclRefExpr* DR); |
| 71 | bool VisitBinaryOperator(BinaryOperator* B); |
| 72 | bool VisitUnaryOperator(UnaryOperator* U); |
| 73 | bool VisitStmt(Stmt* S); |
| 74 | bool VisitCallExpr(CallExpr* C); |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 75 | bool VisitDeclStmt(DeclStmt* D); |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 76 | bool VisitConditionalOperator(ConditionalOperator* C); |
| 77 | |
| 78 | bool Visit(Stmt *S); |
| 79 | bool BlockStmt_VisitExpr(Expr* E); |
Ted Kremenek | 3762208 | 2008-04-15 04:39:08 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 81 | void VisitTerminator(CFGBlock* B) { } |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 82 | }; |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 83 | |
| 84 | static const bool Initialized = true; |
| 85 | static const bool Uninitialized = false; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 87 | bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 88 | |
| 89 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 90 | if (VD->isBlockVarDecl()) { |
| 91 | |
| 92 | if (AD.Observer) |
| 93 | AD.Observer->ObserveDeclRefExpr(V, AD, DR, VD); |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 95 | // Pseudo-hack to prevent cascade of warnings. If an accessed variable |
| 96 | // is uninitialized, then we are already going to flag a warning for |
| 97 | // this variable, which a "source" of uninitialized values. |
| 98 | // We can otherwise do a full "taint" of uninitialized values. The |
| 99 | // client has both options by toggling AD.FullUninitTaint. |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 101 | if (AD.FullUninitTaint) |
| 102 | return V(VD,AD); |
| 103 | } |
| 104 | |
| 105 | return Initialized; |
Ted Kremenek | 43a1698 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 108 | static VarDecl* FindBlockVarDecl(Expr* E) { |
| 109 | |
| 110 | // Blast through casts and parentheses to find any DeclRefExprs that |
| 111 | // refer to a block VarDecl. |
| 112 | |
| 113 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
| 114 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 115 | if (VD->isBlockVarDecl()) return VD; |
| 116 | |
| 117 | return NULL; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 121 | |
| 122 | if (VarDecl* VD = FindBlockVarDecl(B->getLHS())) |
Ted Kremenek | 6ce2b63 | 2007-09-28 21:08:51 +0000 | [diff] [blame] | 123 | if (B->isAssignmentOp()) { |
Ted Kremenek | ff7c538 | 2007-11-24 20:07:36 +0000 | [diff] [blame] | 124 | if (B->getOpcode() == BinaryOperator::Assign) |
| 125 | return V(VD,AD) = Visit(B->getRHS()); |
| 126 | else // Handle +=, -=, *=, etc. We do want '&', not '&&'. |
| 127 | return V(VD,AD) = Visit(B->getLHS()) & Visit(B->getRHS()); |
Ted Kremenek | 6ce2b63 | 2007-09-28 21:08:51 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 130 | return VisitStmt(B); |
| 131 | } |
| 132 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 133 | bool TransferFuncs::VisitDeclStmt(DeclStmt* S) { |
Ted Kremenek | 14f8b4f | 2008-08-05 20:46:55 +0000 | [diff] [blame] | 134 | for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) { |
| 135 | VarDecl *VD = dyn_cast<VarDecl>(*I); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 136 | if (VD && VD->isBlockVarDecl()) { |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 137 | if (Stmt* I = VD->getInit()) |
| 138 | V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized; |
Ted Kremenek | 81a56ec | 2007-12-13 05:14:22 +0000 | [diff] [blame] | 139 | else { |
| 140 | // Special case for declarations of array types. For things like: |
| 141 | // |
| 142 | // char x[10]; |
| 143 | // |
| 144 | // we should treat "x" as being initialized, because the variable |
| 145 | // "x" really refers to the memory block. Clearly x[1] is |
| 146 | // uninitialized, but expressions like "(char *) x" really do refer to |
| 147 | // an initialized value. This simple dataflow analysis does not reason |
| 148 | // about the contents of arrays, although it could be potentially |
| 149 | // extended to do so if the array were of constant size. |
| 150 | if (VD->getType()->isArrayType()) |
| 151 | V(VD,AD) = Initialized; |
| 152 | else |
| 153 | V(VD,AD) = Uninitialized; |
| 154 | } |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 155 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 156 | } |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 157 | return Uninitialized; // Value is never consumed. |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 158 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 160 | bool TransferFuncs::VisitCallExpr(CallExpr* C) { |
Ted Kremenek | 59d1827 | 2007-09-18 21:47:41 +0000 | [diff] [blame] | 161 | VisitChildren(C); |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 162 | return Initialized; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 166 | switch (U->getOpcode()) { |
Argyrios Kyrtzidis | 5da6b25 | 2008-04-17 13:52:22 +0000 | [diff] [blame] | 167 | case UnaryOperator::AddrOf: { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 168 | VarDecl* VD = FindBlockVarDecl(U->getSubExpr()); |
| 169 | if (VD && VD->isBlockVarDecl()) |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 170 | return V(VD,AD) = Initialized; |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 171 | break; |
Argyrios Kyrtzidis | 5da6b25 | 2008-04-17 13:52:22 +0000 | [diff] [blame] | 172 | } |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 173 | |
| 174 | case UnaryOperator::SizeOf: |
| 175 | return Initialized; |
| 176 | |
| 177 | default: |
| 178 | break; |
| 179 | } |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 181 | return Visit(U->getSubExpr()); |
| 182 | } |
| 183 | |
| 184 | bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) { |
| 185 | Visit(C->getCond()); |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 186 | |
| 187 | bool rhsResult = Visit(C->getRHS()); |
| 188 | // Handle the GNU extension for missing LHS. |
| 189 | if (Expr *lhs = C->getLHS()) |
| 190 | return Visit(lhs) & rhsResult; // Yes: we want &, not &&. |
| 191 | else |
| 192 | return rhsResult; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool TransferFuncs::VisitStmt(Stmt* S) { |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 196 | bool x = Initialized; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 197 | |
| 198 | // We don't stop at the first subexpression that is Uninitialized because |
| 199 | // evaluating some subexpressions may result in propogating "Uninitialized" |
| 200 | // or "Initialized" to variables referenced in the other subexpressions. |
| 201 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 202 | if (*I && Visit(*I) == Uninitialized) x = Uninitialized; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 203 | |
| 204 | return x; |
| 205 | } |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 206 | |
| 207 | bool TransferFuncs::Visit(Stmt *S) { |
| 208 | if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD); |
| 209 | else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S); |
| 210 | } |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 211 | |
| 212 | bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 213 | bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E); |
| 214 | if (AD.isTracked(E)) V(E,AD) = x; |
| 215 | return x; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 218 | } // end anonymous namespace |
| 219 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 220 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 221 | // Merge operator. |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 222 | // |
| 223 | // In our transfer functions we take the approach that any |
Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 224 | // combination of uninitialized values, e.g. |
| 225 | // Uninitialized + ___ = Uninitialized. |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 226 | // |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 227 | // Merges take the same approach, preferring soundness. At a confluence point, |
| 228 | // if any predecessor has a variable marked uninitialized, the value is |
| 229 | // uninitialized at the confluence point. |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 231 | |
| 232 | namespace { |
Ted Kremenek | 5fb5c6a | 2008-03-22 20:11:00 +0000 | [diff] [blame] | 233 | typedef ExprDeclBitVector_Types::Intersect Merge; |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 234 | typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver; |
| 235 | } |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 238 | // Uninitialized values checker. Scan an AST and flag variable uses |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 239 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 241 | UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {} |
| 242 | |
| 243 | namespace { |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 244 | class VISIBILITY_HIDDEN UninitializedValuesChecker |
| 245 | : public UninitializedValues::ObserverTy { |
| 246 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 247 | ASTContext &Ctx; |
| 248 | Diagnostic &Diags; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 249 | llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned; |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 250 | |
| 251 | public: |
| 252 | UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags) |
| 253 | : Ctx(ctx), Diags(diags) {} |
| 254 | |
| 255 | virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V, |
| 256 | UninitializedValues::AnalysisDataTy& AD, |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 257 | DeclRefExpr* DR, VarDecl* VD) { |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 258 | |
Ted Kremenek | 43a1698 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 259 | assert ( AD.isTracked(VD) && "Unknown VarDecl."); |
| 260 | |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 261 | if (V(VD,AD) == Uninitialized) |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 262 | if (AlreadyWarned.insert(VD)) |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 263 | Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()), |
| 264 | diag::warn_uninit_val); |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 265 | } |
| 266 | }; |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 267 | } // end anonymous namespace |
| 268 | |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 269 | namespace clang { |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 270 | void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags, |
| 271 | bool FullUninitTaint) { |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 272 | |
Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 273 | // Compute the uninitialized values information. |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 274 | UninitializedValues U(cfg); |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 275 | U.getAnalysisData().FullUninitTaint = FullUninitTaint; |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 276 | Solver S(U); |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 277 | S.runOnCFG(cfg); |
| 278 | |
| 279 | // Scan for DeclRefExprs that use uninitialized values. |
| 280 | UninitializedValuesChecker Observer(Ctx,Diags); |
| 281 | U.getAnalysisData().Observer = &Observer; |
Ted Kremenek | 294a7c9 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 282 | S.runOnAllBlocks(cfg); |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 283 | } |
Ted Kremenek | 294a7c9 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 284 | } // end namespace clang |