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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/AnalysisDiagnostic.h" |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 1de632b | 2007-09-25 21:00:24 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/FlowSensitive/DataflowSolver.h" |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 19 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 25 | // Dataflow initialization logic. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 30 | class RegisterDecls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | : public CFGRecStmtDeclVisitor<RegisterDecls> { |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 56d516d | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 33 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 34 | public: |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 35 | RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 606ceee | 2008-04-15 23:02:18 +0000 | [diff] [blame] | 37 | void VisitVarDecl(VarDecl* VD) { AD.Register(VD); } |
Ted Kremenek | 9f9141c | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 38 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 39 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 41 | } // end anonymous namespace |
| 42 | |
| 43 | void UninitializedValues::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 44 | RegisterDecls R(getAnalysisData()); |
Ted Kremenek | a90b0d1 | 2007-09-18 20:59:00 +0000 | [diff] [blame] | 45 | cfg.VisitBlockStmts(R); |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 49 | // Transfer functions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 51 | |
| 52 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 53 | class TransferFuncs |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 54 | : public CFGStmtVisitor<TransferFuncs,bool> { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 56 | UninitializedValues::ValTy V; |
Ted Kremenek | 56d516d | 2007-09-17 17:14:52 +0000 | [diff] [blame] | 57 | UninitializedValues::AnalysisDataTy& AD; |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 58 | public: |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 59 | TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 61 | UninitializedValues::ValTy& getVal() { return V; } |
Ted Kremenek | 9f9141c | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 62 | CFG& getCFG() { return AD.getCFG(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 64 | void SetTopValue(UninitializedValues::ValTy& X) { |
Ted Kremenek | e219b8a | 2008-11-11 19:41:42 +0000 | [diff] [blame] | 65 | X.setDeclValues(AD); |
Ted Kremenek | 8d798c7 | 2008-11-14 01:14:18 +0000 | [diff] [blame] | 66 | X.resetBlkExprValues(AD); |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 67 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 69 | bool VisitDeclRefExpr(DeclRefExpr* DR); |
| 70 | bool VisitBinaryOperator(BinaryOperator* B); |
| 71 | bool VisitUnaryOperator(UnaryOperator* U); |
| 72 | bool VisitStmt(Stmt* S); |
| 73 | bool VisitCallExpr(CallExpr* C); |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 74 | bool VisitDeclStmt(DeclStmt* D); |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 75 | bool VisitConditionalOperator(ConditionalOperator* C); |
Ted Kremenek | bfcb712 | 2008-11-12 21:58:46 +0000 | [diff] [blame] | 76 | bool BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 78 | bool Visit(Stmt *S); |
| 79 | bool BlockStmt_VisitExpr(Expr* E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | e219b8a | 2008-11-11 19:41:42 +0000 | [diff] [blame] | 84 | static const bool Initialized = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | static const bool Uninitialized = true; |
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) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 89 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 90 | if (VD->isBlockVarDecl()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 92 | if (AD.Observer) |
| 93 | AD.Observer->ObserveDeclRefExpr(V, AD, DR, VD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 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) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 110 | // Blast through casts and parentheses to find any DeclRefExprs that |
| 111 | // refer to a block VarDecl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 113 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | 2f868c0 | 2008-04-16 02:59:55 +0000 | [diff] [blame] | 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 | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 137 | if (Stmt* I = VD->getInit()) { |
| 138 | // Visit the subexpression to check for uses of uninitialized values, |
| 139 | // even if we don't propagate that value. |
| 140 | bool isSubExprUninit = Visit(I); |
| 141 | V(VD,AD) = AD.FullUninitTaint ? isSubExprUninit : Initialized; |
| 142 | } |
Ted Kremenek | 81a56ec | 2007-12-13 05:14:22 +0000 | [diff] [blame] | 143 | else { |
| 144 | // Special case for declarations of array types. For things like: |
| 145 | // |
| 146 | // char x[10]; |
| 147 | // |
| 148 | // we should treat "x" as being initialized, because the variable |
| 149 | // "x" really refers to the memory block. Clearly x[1] is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | // uninitialized, but expressions like "(char *) x" really do refer to |
| 151 | // an initialized value. This simple dataflow analysis does not reason |
Ted Kremenek | 81a56ec | 2007-12-13 05:14:22 +0000 | [diff] [blame] | 152 | // about the contents of arrays, although it could be potentially |
| 153 | // extended to do so if the array were of constant size. |
| 154 | if (VD->getType()->isArrayType()) |
| 155 | V(VD,AD) = Initialized; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | else |
Ted Kremenek | 81a56ec | 2007-12-13 05:14:22 +0000 | [diff] [blame] | 157 | V(VD,AD) = Uninitialized; |
| 158 | } |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 159 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 161 | return Uninitialized; // Value is never consumed. |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 164 | bool TransferFuncs::VisitCallExpr(CallExpr* C) { |
Ted Kremenek | 59d1827 | 2007-09-18 21:47:41 +0000 | [diff] [blame] | 165 | VisitChildren(C); |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 166 | return Initialized; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 170 | switch (U->getOpcode()) { |
Argyrios Kyrtzidis | 5da6b25 | 2008-04-17 13:52:22 +0000 | [diff] [blame] | 171 | case UnaryOperator::AddrOf: { |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 172 | VarDecl* VD = FindBlockVarDecl(U->getSubExpr()); |
| 173 | if (VD && VD->isBlockVarDecl()) |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 174 | return V(VD,AD) = Initialized; |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 175 | break; |
Argyrios Kyrtzidis | 5da6b25 | 2008-04-17 13:52:22 +0000 | [diff] [blame] | 176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 178 | default: |
| 179 | break; |
| 180 | } |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 182 | return Visit(U->getSubExpr()); |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | bfcb712 | 2008-11-12 21:58:46 +0000 | [diff] [blame] | 185 | bool |
| 186 | TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { |
Ted Kremenek | e219b8a | 2008-11-11 19:41:42 +0000 | [diff] [blame] | 187 | // This represents a use of the 'collection' |
| 188 | bool x = Visit(S->getCollection()); |
| 189 | |
| 190 | if (x == Uninitialized) |
| 191 | return Uninitialized; |
| 192 | |
| 193 | // This represents an initialization of the 'element' value. |
| 194 | Stmt* Element = S->getElement(); |
| 195 | VarDecl* VD = 0; |
| 196 | |
| 197 | if (DeclStmt* DS = dyn_cast<DeclStmt>(Element)) |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 198 | VD = cast<VarDecl>(DS->getSingleDecl()); |
Ted Kremenek | c2813f7 | 2008-11-14 18:21:25 +0000 | [diff] [blame] | 199 | else { |
| 200 | Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens(); |
Ted Kremenek | e219b8a | 2008-11-11 19:41:42 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | c2813f7 | 2008-11-14 18:21:25 +0000 | [diff] [blame] | 202 | // Initialize the value of the reference variable. |
| 203 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ElemExpr)) |
| 204 | VD = cast<VarDecl>(DR->getDecl()); |
| 205 | else |
| 206 | return Visit(ElemExpr); |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | e219b8a | 2008-11-11 19:41:42 +0000 | [diff] [blame] | 209 | V(VD,AD) = Initialized; |
| 210 | return Initialized; |
| 211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
| 213 | |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 214 | bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) { |
| 215 | Visit(C->getCond()); |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 216 | |
| 217 | bool rhsResult = Visit(C->getRHS()); |
| 218 | // Handle the GNU extension for missing LHS. |
| 219 | if (Expr *lhs = C->getLHS()) |
| 220 | return Visit(lhs) & rhsResult; // Yes: we want &, not &&. |
| 221 | else |
| 222 | return rhsResult; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool TransferFuncs::VisitStmt(Stmt* S) { |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 226 | bool x = Initialized; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 227 | |
| 228 | // We don't stop at the first subexpression that is Uninitialized because |
| 229 | // evaluating some subexpressions may result in propogating "Uninitialized" |
| 230 | // or "Initialized" to variables referenced in the other subexpressions. |
| 231 | 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] | 232 | if (*I && Visit(*I) == Uninitialized) x = Uninitialized; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 234 | return x; |
| 235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 237 | bool TransferFuncs::Visit(Stmt *S) { |
| 238 | if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD); |
| 239 | else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S); |
| 240 | } |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 241 | |
| 242 | bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E); |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 244 | if (AD.isTracked(E)) V(E,AD) = x; |
| 245 | return x; |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 248 | } // end anonymous namespace |
| 249 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 251 | // Merge operator. |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 252 | // |
| 253 | // In our transfer functions we take the approach that any |
Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 254 | // combination of uninitialized values, e.g. |
| 255 | // Uninitialized + ___ = Uninitialized. |
Ted Kremenek | 20ee4fb | 2007-09-17 18:31:23 +0000 | [diff] [blame] | 256 | // |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 257 | // Merges take the same approach, preferring soundness. At a confluence point, |
| 258 | // if any predecessor has a variable marked uninitialized, the value is |
| 259 | // uninitialized at the confluence point. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 261 | |
| 262 | namespace { |
Ted Kremenek | 8d798c7 | 2008-11-14 01:14:18 +0000 | [diff] [blame] | 263 | typedef StmtDeclBitVector_Types::Union Merge; |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 264 | typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver; |
| 265 | } |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// |
Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 268 | // Uninitialized values checker. Scan an AST and flag variable uses |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 271 | UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {} |
| 272 | |
| 273 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 274 | class UninitializedValuesChecker |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 275 | : public UninitializedValues::ObserverTy { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 277 | ASTContext &Ctx; |
| 278 | Diagnostic &Diags; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 279 | llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 281 | public: |
| 282 | UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags) |
| 283 | : Ctx(ctx), Diags(diags) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 285 | virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V, |
| 286 | UninitializedValues::AnalysisDataTy& AD, |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 287 | DeclRefExpr* DR, VarDecl* VD) { |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 43a1698 | 2007-09-18 21:43:18 +0000 | [diff] [blame] | 289 | assert ( AD.isTracked(VD) && "Unknown VarDecl."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | aead153 | 2007-09-27 18:20:22 +0000 | [diff] [blame] | 291 | if (V(VD,AD) == Uninitialized) |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 292 | if (AlreadyWarned.insert(VD)) |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 293 | Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()), |
| 294 | diag::warn_uninit_val); |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 295 | } |
| 296 | }; |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 297 | } // end anonymous namespace |
| 298 | |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 299 | namespace clang { |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 300 | void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags, |
| 301 | bool FullUninitTaint) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Nick Lewycky | 5d796aa | 2008-08-16 17:46:53 +0000 | [diff] [blame] | 303 | // Compute the uninitialized values information. |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 304 | UninitializedValues U(cfg); |
Ted Kremenek | a1de8c7 | 2007-09-28 00:09:38 +0000 | [diff] [blame] | 305 | U.getAnalysisData().FullUninitTaint = FullUninitTaint; |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 306 | Solver S(U); |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 307 | S.runOnCFG(cfg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | cd5860c | 2007-09-17 19:59:27 +0000 | [diff] [blame] | 309 | // Scan for DeclRefExprs that use uninitialized values. |
| 310 | UninitializedValuesChecker Observer(Ctx,Diags); |
| 311 | U.getAnalysisData().Observer = &Observer; |
Ted Kremenek | 294a7c9 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 312 | S.runOnAllBlocks(cfg); |
Ted Kremenek | 13ed7fe | 2007-09-14 22:49:21 +0000 | [diff] [blame] | 313 | } |
Ted Kremenek | 294a7c9 | 2007-09-18 21:08:21 +0000 | [diff] [blame] | 314 | } // end namespace clang |