blob: 014ea8255e680b997aa51c70ead4f7e71e1eb2b6 [file] [log] [blame]
Nick Lewycky5d796aa2008-08-16 17:46:53 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek13ed7fe2007-09-14 22:49:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Uninitialized Values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenek11de5cb2007-09-20 21:42:55 +000015#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenekcd5860c2007-09-17 19:59:27 +000016#include "clang/Analysis/LocalCheckers.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Analysis/AnalysisDiagnostic.h"
Ted Kremenekcd5860c2007-09-17 19:59:27 +000018#include "clang/AST/ASTContext.h"
Ted Kremenek1de632b2007-09-25 21:00:24 +000019#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000020#include "llvm/Support/Compiler.h"
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000021
Ted Kremenekcd5860c2007-09-17 19:59:27 +000022#include "llvm/ADT/SmallPtrSet.h"
23
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000024using namespace clang;
25
Ted Kremenekcd5860c2007-09-17 19:59:27 +000026//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000027// Dataflow initialization logic.
Ted Kremenekcd5860c2007-09-17 19:59:27 +000028//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000029
30namespace {
31
Ted Kremenekc2b51d82008-01-08 18:19:08 +000032class VISIBILITY_HIDDEN RegisterDecls
33 : public CFGRecStmtDeclVisitor<RegisterDecls> {
34
Ted Kremenek56d516d2007-09-17 17:14:52 +000035 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000036public:
Ted Kremenek11e72182007-10-01 20:33:52 +000037 RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000038
Ted Kremenek606ceee2008-04-15 23:02:18 +000039 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000040 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000041};
42
43} // end anonymous namespace
44
45void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek11e72182007-10-01 20:33:52 +000046 RegisterDecls R(getAnalysisData());
Ted Kremeneka90b0d12007-09-18 20:59:00 +000047 cfg.VisitBlockStmts(R);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000048}
49
Ted Kremenekcd5860c2007-09-17 19:59:27 +000050//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000051// Transfer functions.
Ted Kremenekcd5860c2007-09-17 19:59:27 +000052//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000053
54namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +000055class VISIBILITY_HIDDEN TransferFuncs
56 : public CFGStmtVisitor<TransferFuncs,bool> {
57
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000058 UninitializedValues::ValTy V;
Ted Kremenek56d516d2007-09-17 17:14:52 +000059 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000060public:
Ted Kremenek7deed0c2008-04-15 18:35:30 +000061 TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000062
63 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000064 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek56d516d2007-09-17 17:14:52 +000065
Ted Kremenek7deed0c2008-04-15 18:35:30 +000066 void SetTopValue(UninitializedValues::ValTy& X) {
Ted Kremeneke219b8a2008-11-11 19:41:42 +000067 X.setDeclValues(AD);
Ted Kremenek8d798c72008-11-14 01:14:18 +000068 X.resetBlkExprValues(AD);
Ted Kremenek7deed0c2008-04-15 18:35:30 +000069 }
70
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000071 bool VisitDeclRefExpr(DeclRefExpr* DR);
72 bool VisitBinaryOperator(BinaryOperator* B);
73 bool VisitUnaryOperator(UnaryOperator* U);
74 bool VisitStmt(Stmt* S);
75 bool VisitCallExpr(CallExpr* C);
Ted Kremenekcd5860c2007-09-17 19:59:27 +000076 bool VisitDeclStmt(DeclStmt* D);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000077 bool VisitConditionalOperator(ConditionalOperator* C);
Ted Kremenekbfcb7122008-11-12 21:58:46 +000078 bool BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000079
80 bool Visit(Stmt *S);
81 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek37622082008-04-15 04:39:08 +000082
Ted Kremenek411cdee2008-04-16 21:10:48 +000083 void VisitTerminator(CFGBlock* B) { }
Ted Kremenek13ed7fe2007-09-14 22:49:21 +000084};
Ted Kremenekaead1532007-09-27 18:20:22 +000085
Ted Kremeneke219b8a2008-11-11 19:41:42 +000086static const bool Initialized = false;
87static const bool Uninitialized = true;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000088
Ted Kremenek20ee4fb2007-09-17 18:31:23 +000089bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek2f868c02008-04-16 02:59:55 +000090
91 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
92 if (VD->isBlockVarDecl()) {
93
94 if (AD.Observer)
95 AD.Observer->ObserveDeclRefExpr(V, AD, DR, VD);
Ted Kremeneka1de8c72007-09-28 00:09:38 +000096
Ted Kremenek2f868c02008-04-16 02:59:55 +000097 // Pseudo-hack to prevent cascade of warnings. If an accessed variable
98 // is uninitialized, then we are already going to flag a warning for
99 // this variable, which a "source" of uninitialized values.
100 // We can otherwise do a full "taint" of uninitialized values. The
101 // client has both options by toggling AD.FullUninitTaint.
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000102
Ted Kremenek2f868c02008-04-16 02:59:55 +0000103 if (AD.FullUninitTaint)
104 return V(VD,AD);
105 }
106
107 return Initialized;
Ted Kremenek43a16982007-09-18 21:43:18 +0000108}
109
Ted Kremenek2f868c02008-04-16 02:59:55 +0000110static VarDecl* FindBlockVarDecl(Expr* E) {
111
112 // Blast through casts and parentheses to find any DeclRefExprs that
113 // refer to a block VarDecl.
114
115 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
116 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
117 if (VD->isBlockVarDecl()) return VD;
118
119 return NULL;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000120}
121
122bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek2f868c02008-04-16 02:59:55 +0000123
124 if (VarDecl* VD = FindBlockVarDecl(B->getLHS()))
Ted Kremenek6ce2b632007-09-28 21:08:51 +0000125 if (B->isAssignmentOp()) {
Ted Kremenekff7c5382007-11-24 20:07:36 +0000126 if (B->getOpcode() == BinaryOperator::Assign)
127 return V(VD,AD) = Visit(B->getRHS());
128 else // Handle +=, -=, *=, etc. We do want '&', not '&&'.
129 return V(VD,AD) = Visit(B->getLHS()) & Visit(B->getRHS());
Ted Kremenek6ce2b632007-09-28 21:08:51 +0000130 }
131
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000132 return VisitStmt(B);
133}
134
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000135bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000136 for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) {
137 VarDecl *VD = dyn_cast<VarDecl>(*I);
Steve Naroff248a7532008-04-15 22:42:06 +0000138 if (VD && VD->isBlockVarDecl()) {
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000139 if (Stmt* I = VD->getInit())
140 V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
Ted Kremenek81a56ec2007-12-13 05:14:22 +0000141 else {
142 // Special case for declarations of array types. For things like:
143 //
144 // char x[10];
145 //
146 // we should treat "x" as being initialized, because the variable
147 // "x" really refers to the memory block. Clearly x[1] is
148 // uninitialized, but expressions like "(char *) x" really do refer to
149 // an initialized value. This simple dataflow analysis does not reason
150 // about the contents of arrays, although it could be potentially
151 // extended to do so if the array were of constant size.
152 if (VD->getType()->isArrayType())
153 V(VD,AD) = Initialized;
154 else
155 V(VD,AD) = Uninitialized;
156 }
Ted Kremenekaead1532007-09-27 18:20:22 +0000157 }
Steve Naroff248a7532008-04-15 22:42:06 +0000158 }
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000159 return Uninitialized; // Value is never consumed.
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000160}
Steve Naroff248a7532008-04-15 22:42:06 +0000161
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000162bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremenek59d18272007-09-18 21:47:41 +0000163 VisitChildren(C);
Ted Kremenekaead1532007-09-27 18:20:22 +0000164 return Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000165}
166
167bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000168 switch (U->getOpcode()) {
Argyrios Kyrtzidis5da6b252008-04-17 13:52:22 +0000169 case UnaryOperator::AddrOf: {
Steve Naroff248a7532008-04-15 22:42:06 +0000170 VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
171 if (VD && VD->isBlockVarDecl())
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000172 return V(VD,AD) = Initialized;
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000173 break;
Argyrios Kyrtzidis5da6b252008-04-17 13:52:22 +0000174 }
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000175
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000176 default:
177 break;
178 }
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000179
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000180 return Visit(U->getSubExpr());
181}
182
Ted Kremenekbfcb7122008-11-12 21:58:46 +0000183bool
184TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
Ted Kremeneke219b8a2008-11-11 19:41:42 +0000185 // This represents a use of the 'collection'
186 bool x = Visit(S->getCollection());
187
188 if (x == Uninitialized)
189 return Uninitialized;
190
191 // This represents an initialization of the 'element' value.
192 Stmt* Element = S->getElement();
193 VarDecl* VD = 0;
194
195 if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
Chris Lattner7e24e822009-03-28 06:33:19 +0000196 VD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekc2813f72008-11-14 18:21:25 +0000197 else {
198 Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
Ted Kremeneke219b8a2008-11-11 19:41:42 +0000199
Ted Kremenekc2813f72008-11-14 18:21:25 +0000200 // Initialize the value of the reference variable.
201 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ElemExpr))
202 VD = cast<VarDecl>(DR->getDecl());
203 else
204 return Visit(ElemExpr);
205 }
206
Ted Kremeneke219b8a2008-11-11 19:41:42 +0000207 V(VD,AD) = Initialized;
208 return Initialized;
209}
210
211
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000212bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
213 Visit(C->getCond());
Anders Carlsson39073232007-11-30 19:04:31 +0000214
215 bool rhsResult = Visit(C->getRHS());
216 // Handle the GNU extension for missing LHS.
217 if (Expr *lhs = C->getLHS())
218 return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
219 else
220 return rhsResult;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000221}
222
223bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekaead1532007-09-27 18:20:22 +0000224 bool x = Initialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000225
226 // We don't stop at the first subexpression that is Uninitialized because
227 // evaluating some subexpressions may result in propogating "Uninitialized"
228 // or "Initialized" to variables referenced in the other subexpressions.
229 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000230 if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000231
232 return x;
233}
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000234
235bool TransferFuncs::Visit(Stmt *S) {
236 if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
237 else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
238}
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000239
240bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +0000241 bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
242 if (AD.isTracked(E)) V(E,AD) = x;
243 return x;
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000244}
245
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000246} // end anonymous namespace
247
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000248//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000249// Merge operator.
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000250//
251// In our transfer functions we take the approach that any
Nick Lewycky5d796aa2008-08-16 17:46:53 +0000252// combination of uninitialized values, e.g.
253// Uninitialized + ___ = Uninitialized.
Ted Kremenek20ee4fb2007-09-17 18:31:23 +0000254//
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000255// Merges take the same approach, preferring soundness. At a confluence point,
256// if any predecessor has a variable marked uninitialized, the value is
257// uninitialized at the confluence point.
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000258//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000259
260namespace {
Ted Kremenek8d798c72008-11-14 01:14:18 +0000261 typedef StmtDeclBitVector_Types::Union Merge;
Ted Kremenekaead1532007-09-27 18:20:22 +0000262 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
263}
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000264
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000265//===----------------------------------------------------------------------===//
Nick Lewycky5d796aa2008-08-16 17:46:53 +0000266// Uninitialized values checker. Scan an AST and flag variable uses
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000267//===----------------------------------------------------------------------===//
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000268
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000269UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
270
271namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +0000272class VISIBILITY_HIDDEN UninitializedValuesChecker
273 : public UninitializedValues::ObserverTy {
274
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000275 ASTContext &Ctx;
276 Diagnostic &Diags;
Steve Naroff248a7532008-04-15 22:42:06 +0000277 llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000278
279public:
280 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
281 : Ctx(ctx), Diags(diags) {}
282
283 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
284 UninitializedValues::AnalysisDataTy& AD,
Steve Naroff248a7532008-04-15 22:42:06 +0000285 DeclRefExpr* DR, VarDecl* VD) {
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000286
Ted Kremenek43a16982007-09-18 21:43:18 +0000287 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
288
Ted Kremenekaead1532007-09-27 18:20:22 +0000289 if (V(VD,AD) == Uninitialized)
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000290 if (AlreadyWarned.insert(VD))
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000291 Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
292 diag::warn_uninit_val);
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000293 }
294};
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000295} // end anonymous namespace
296
Ted Kremenek2bf55142007-09-17 20:49:30 +0000297namespace clang {
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000298void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
299 bool FullUninitTaint) {
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000300
Nick Lewycky5d796aa2008-08-16 17:46:53 +0000301 // Compute the uninitialized values information.
Ted Kremenek11e72182007-10-01 20:33:52 +0000302 UninitializedValues U(cfg);
Ted Kremeneka1de8c72007-09-28 00:09:38 +0000303 U.getAnalysisData().FullUninitTaint = FullUninitTaint;
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000304 Solver S(U);
Ted Kremenekcd5860c2007-09-17 19:59:27 +0000305 S.runOnCFG(cfg);
306
307 // Scan for DeclRefExprs that use uninitialized values.
308 UninitializedValuesChecker Observer(Ctx,Diags);
309 U.getAnalysisData().Observer = &Observer;
Ted Kremenek294a7c92007-09-18 21:08:21 +0000310 S.runOnAllBlocks(cfg);
Ted Kremenek13ed7fe2007-09-14 22:49:21 +0000311}
Ted Kremenek294a7c92007-09-18 21:08:21 +0000312} // end namespace clang