blob: 0e4a387620d4c691fb2997eed49d03b56ab3f4da [file] [log] [blame]
Nick Lewycky5e94eea2008-08-16 17:46:53 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenek7f49f502007-09-14 22:49:21 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Kremenek7f49f502007-09-14 22:49:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Uninitialized Values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekcdf8e842007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenek26e47462007-09-20 21:42:55 +000015#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek3871d8e2007-09-17 19:59:27 +000016#include "clang/Analysis/LocalCheckers.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/AST/ASTContext.h"
Ted Kremenek10d80462007-09-25 21:00:24 +000019#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekec818352008-01-08 18:19:08 +000020#include "llvm/Support/Compiler.h"
Ted Kremenek7f49f502007-09-14 22:49:21 +000021
Ted Kremenek3871d8e2007-09-17 19:59:27 +000022#include "llvm/ADT/SmallPtrSet.h"
23
Ted Kremenek7f49f502007-09-14 22:49:21 +000024using namespace clang;
25
Ted Kremenek3871d8e2007-09-17 19:59:27 +000026//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000027// Dataflow initialization logic.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000028//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000029
30namespace {
31
Ted Kremenekec818352008-01-08 18:19:08 +000032class VISIBILITY_HIDDEN RegisterDecls
33 : public CFGRecStmtDeclVisitor<RegisterDecls> {
34
Ted Kremenek3e039752007-09-17 17:14:52 +000035 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek7f49f502007-09-14 22:49:21 +000036public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000037 RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek7f49f502007-09-14 22:49:21 +000038
Ted Kremenekda67f2f2008-04-15 23:02:18 +000039 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek705386b2007-11-20 03:01:58 +000040 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek7f49f502007-09-14 22:49:21 +000041};
42
43} // end anonymous namespace
44
45void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +000046 RegisterDecls R(getAnalysisData());
Ted Kremenek68447a62007-09-18 20:59:00 +000047 cfg.VisitBlockStmts(R);
Ted Kremenek7f49f502007-09-14 22:49:21 +000048}
49
Ted Kremenek3871d8e2007-09-17 19:59:27 +000050//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000051// Transfer functions.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000052//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000053
54namespace {
Ted Kremenekec818352008-01-08 18:19:08 +000055class VISIBILITY_HIDDEN TransferFuncs
56 : public CFGStmtVisitor<TransferFuncs,bool> {
57
Ted Kremenek7f49f502007-09-14 22:49:21 +000058 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000059 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek7f49f502007-09-14 22:49:21 +000060public:
Ted Kremenek9ea943f2008-04-15 18:35:30 +000061 TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek7f49f502007-09-14 22:49:21 +000062
63 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek705386b2007-11-20 03:01:58 +000064 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek3e039752007-09-17 17:14:52 +000065
Ted Kremenek9ea943f2008-04-15 18:35:30 +000066 void SetTopValue(UninitializedValues::ValTy& X) {
Ted Kremenek24b81b52008-11-11 19:41:42 +000067 X.setDeclValues(AD);
68 X.resetExprValues(AD);
Ted Kremenek9ea943f2008-04-15 18:35:30 +000069 }
70
Ted Kremenek334b30a2007-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 Kremenek3871d8e2007-09-17 19:59:27 +000076 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek6b576492007-09-28 00:09:38 +000077 bool VisitConditionalOperator(ConditionalOperator* C);
Ted Kremeneke71b3c32008-11-12 21:58:46 +000078 bool BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
Ted Kremenek6b576492007-09-28 00:09:38 +000079
80 bool Visit(Stmt *S);
81 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremeneka0aa0b12008-04-15 04:39:08 +000082
Ted Kremenek79f0a632008-04-16 21:10:48 +000083 void VisitTerminator(CFGBlock* B) { }
Ted Kremenek7f49f502007-09-14 22:49:21 +000084};
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +000085
Ted Kremenek24b81b52008-11-11 19:41:42 +000086static const bool Initialized = false;
87static const bool Uninitialized = true;
Ted Kremenek334b30a2007-09-17 18:31:23 +000088
Ted Kremenek334b30a2007-09-17 18:31:23 +000089bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek947fdb82008-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 Kremenek6b576492007-09-28 00:09:38 +000096
Ted Kremenek947fdb82008-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 Kremenek6b576492007-09-28 00:09:38 +0000102
Ted Kremenek947fdb82008-04-16 02:59:55 +0000103 if (AD.FullUninitTaint)
104 return V(VD,AD);
105 }
106
107 return Initialized;
Ted Kremenekf92ba512007-09-18 21:43:18 +0000108}
109
Ted Kremenek947fdb82008-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 Kremenek334b30a2007-09-17 18:31:23 +0000120}
121
122bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek947fdb82008-04-16 02:59:55 +0000123
124 if (VarDecl* VD = FindBlockVarDecl(B->getLHS()))
Ted Kremenekf87111b2007-09-28 21:08:51 +0000125 if (B->isAssignmentOp()) {
Ted Kremenekbf80ca02007-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 Kremenekf87111b2007-09-28 21:08:51 +0000130 }
131
Ted Kremenek334b30a2007-09-17 18:31:23 +0000132 return VisitStmt(B);
133}
134
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000135bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Ted Kremenekb59f9cf2008-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 Naroff72a6ebc2008-04-15 22:42:06 +0000138 if (VD && VD->isBlockVarDecl()) {
Ted Kremenek6b576492007-09-28 00:09:38 +0000139 if (Stmt* I = VD->getInit())
140 V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
Ted Kremenek0898e862007-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 Kremenekbfbb7fb2007-09-27 18:20:22 +0000157 }
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000158 }
Ted Kremenek6b576492007-09-28 00:09:38 +0000159 return Uninitialized; // Value is never consumed.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000160}
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000161
Ted Kremenek334b30a2007-09-17 18:31:23 +0000162bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremeneka1d35862007-09-18 21:47:41 +0000163 VisitChildren(C);
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000164 return Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000165}
166
167bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000168 switch (U->getOpcode()) {
Argiris Kirtzidis2d22f4e2008-04-17 13:52:22 +0000169 case UnaryOperator::AddrOf: {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000170 VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
171 if (VD && VD->isBlockVarDecl())
Ted Kremeneke33d1002007-12-13 04:47:15 +0000172 return V(VD,AD) = Initialized;
Ted Kremeneke33d1002007-12-13 04:47:15 +0000173 break;
Argiris Kirtzidis2d22f4e2008-04-17 13:52:22 +0000174 }
Ted Kremeneke33d1002007-12-13 04:47:15 +0000175
Ted Kremeneke33d1002007-12-13 04:47:15 +0000176 default:
177 break;
178 }
Ted Kremenek334b30a2007-09-17 18:31:23 +0000179
Ted Kremenek6b576492007-09-28 00:09:38 +0000180 return Visit(U->getSubExpr());
181}
182
Ted Kremeneke71b3c32008-11-12 21:58:46 +0000183bool
184TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
Ted Kremenek24b81b52008-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))
196 VD = cast<VarDecl>(DS->getSolitaryDecl());
197 else
198 VD = cast<VarDecl>(cast<DeclRefExpr>(Element)->getDecl());
199
200 V(VD,AD) = Initialized;
201 return Initialized;
202}
203
204
Ted Kremenek6b576492007-09-28 00:09:38 +0000205bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
206 Visit(C->getCond());
Anders Carlsson37365fc2007-11-30 19:04:31 +0000207
208 bool rhsResult = Visit(C->getRHS());
209 // Handle the GNU extension for missing LHS.
210 if (Expr *lhs = C->getLHS())
211 return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
212 else
213 return rhsResult;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000214}
215
216bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000217 bool x = Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000218
219 // We don't stop at the first subexpression that is Uninitialized because
220 // evaluating some subexpressions may result in propogating "Uninitialized"
221 // or "Initialized" to variables referenced in the other subexpressions.
222 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenek6b576492007-09-28 00:09:38 +0000223 if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000224
225 return x;
226}
Ted Kremenek6b576492007-09-28 00:09:38 +0000227
228bool TransferFuncs::Visit(Stmt *S) {
229 if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
230 else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
231}
Ted Kremenek334b30a2007-09-17 18:31:23 +0000232
233bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenekc6fda602008-01-26 00:03:27 +0000234 bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
235 if (AD.isTracked(E)) V(E,AD) = x;
236 return x;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000237}
238
Ted Kremenek7f49f502007-09-14 22:49:21 +0000239} // end anonymous namespace
240
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000241//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000242// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000243//
244// In our transfer functions we take the approach that any
Nick Lewycky5e94eea2008-08-16 17:46:53 +0000245// combination of uninitialized values, e.g.
246// Uninitialized + ___ = Uninitialized.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000247//
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000248// Merges take the same approach, preferring soundness. At a confluence point,
249// if any predecessor has a variable marked uninitialized, the value is
250// uninitialized at the confluence point.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000251//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000252
253namespace {
Ted Kremenek24b81b52008-11-11 19:41:42 +0000254 typedef ExprDeclBitVector_Types::Union Merge;
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000255 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
256}
Ted Kremenek7f49f502007-09-14 22:49:21 +0000257
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000258//===----------------------------------------------------------------------===//
Nick Lewycky5e94eea2008-08-16 17:46:53 +0000259// Uninitialized values checker. Scan an AST and flag variable uses
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000260//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000261
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000262UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
263
264namespace {
Ted Kremenekec818352008-01-08 18:19:08 +0000265class VISIBILITY_HIDDEN UninitializedValuesChecker
266 : public UninitializedValues::ObserverTy {
267
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000268 ASTContext &Ctx;
269 Diagnostic &Diags;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000270 llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000271
272public:
273 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
274 : Ctx(ctx), Diags(diags) {}
275
276 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
277 UninitializedValues::AnalysisDataTy& AD,
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000278 DeclRefExpr* DR, VarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000279
Ted Kremenekf92ba512007-09-18 21:43:18 +0000280 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
281
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000282 if (V(VD,AD) == Uninitialized)
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000283 if (AlreadyWarned.insert(VD))
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000284 Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
285 diag::warn_uninit_val);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000286 }
287};
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000288} // end anonymous namespace
289
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000290namespace clang {
Ted Kremenek6b576492007-09-28 00:09:38 +0000291void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
292 bool FullUninitTaint) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000293
Nick Lewycky5e94eea2008-08-16 17:46:53 +0000294 // Compute the uninitialized values information.
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000295 UninitializedValues U(cfg);
Ted Kremenek6b576492007-09-28 00:09:38 +0000296 U.getAnalysisData().FullUninitTaint = FullUninitTaint;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000297 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000298 S.runOnCFG(cfg);
299
300 // Scan for DeclRefExprs that use uninitialized values.
301 UninitializedValuesChecker Observer(Ctx,Diags);
302 U.getAnalysisData().Observer = &Observer;
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000303 S.runOnAllBlocks(cfg);
Ted Kremenek7f49f502007-09-14 22:49:21 +0000304}
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000305} // end namespace clang