blob: 9958166c676f6828b9ae2650772e6cd3b2c42663 [file] [log] [blame]
Ted Kremenek7f49f502007-09-14 22:49:21 +00001//==- UninitializedValues.cpp - Find Unintialized Values --------*- C++ --*-==//
2//
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
Steve Naroff72a6ebc2008-04-15 22:42:06 +000039 void VisitBlockVarDecl(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) {
67 X.resetValues(AD);
68 }
69
Ted Kremenek334b30a2007-09-17 18:31:23 +000070 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 Kremenek3871d8e2007-09-17 19:59:27 +000075 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek6b576492007-09-28 00:09:38 +000076 bool VisitConditionalOperator(ConditionalOperator* C);
77
78 bool Visit(Stmt *S);
79 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremeneka0aa0b12008-04-15 04:39:08 +000080
Ted Kremenek9ea943f2008-04-15 18:35:30 +000081 void VisitTerminator(Stmt* T) { }
Ted Kremenek334b30a2007-09-17 18:31:23 +000082
Steve Naroff72a6ebc2008-04-15 22:42:06 +000083 VarDecl* FindBlockVarDecl(Stmt* S);
Ted Kremenek7f49f502007-09-14 22:49:21 +000084};
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +000085
86static const bool Initialized = true;
87static const bool Uninitialized = false;
Ted Kremenek334b30a2007-09-17 18:31:23 +000088
Ted Kremenek334b30a2007-09-17 18:31:23 +000089bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +000090 // FIXME: Ted, can this be simplified?
91 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
92 if (VD && VD->isBlockVarDecl()) {
Ted Kremenekf92ba512007-09-18 21:43:18 +000093 if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
Ted Kremenek6b576492007-09-28 00:09:38 +000094
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.
100
101 return AD.FullUninitTaint ? V(VD,AD) : Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000102 }
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000103 else return Initialized;
Ted Kremenekf92ba512007-09-18 21:43:18 +0000104}
105
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000106VarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) {
Ted Kremenek6b576492007-09-28 00:09:38 +0000107 for (;;)
Ted Kremenekf92ba512007-09-18 21:43:18 +0000108 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
Ted Kremenek6b576492007-09-28 00:09:38 +0000109 S = P->getSubExpr(); continue;
Ted Kremenekf92ba512007-09-18 21:43:18 +0000110 }
Ted Kremenek6b576492007-09-28 00:09:38 +0000111 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000112 // FIXME: Ted, can this be simplified?
113 VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl());
114 if (VD->isBlockVarDecl())
Ted Kremenekf92ba512007-09-18 21:43:18 +0000115 return VD;
Ted Kremenekbf80ca02007-11-24 20:07:36 +0000116 else
117 return NULL;
Ted Kremenek6b576492007-09-28 00:09:38 +0000118 }
119 else return NULL;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000120}
121
122bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000123 // FIXME: Ted, can this be simplified?
124 VarDecl* VD = FindBlockVarDecl(B->getLHS());
125 if (VD && VD->isBlockVarDecl())
Ted Kremenekf87111b2007-09-28 21:08:51 +0000126 if (B->isAssignmentOp()) {
Ted Kremenekbf80ca02007-11-24 20:07:36 +0000127 if (B->getOpcode() == BinaryOperator::Assign)
128 return V(VD,AD) = Visit(B->getRHS());
129 else // Handle +=, -=, *=, etc. We do want '&', not '&&'.
130 return V(VD,AD) = Visit(B->getLHS()) & Visit(B->getRHS());
Ted Kremenekf87111b2007-09-28 21:08:51 +0000131 }
132
Ted Kremenek334b30a2007-09-17 18:31:23 +0000133 return VisitStmt(B);
134}
135
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000136bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000137 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator()) {
138 VarDecl *VD = dyn_cast<VarDecl>(D);
139 if (VD && VD->isBlockVarDecl()) {
Ted Kremenek6b576492007-09-28 00:09:38 +0000140 if (Stmt* I = VD->getInit())
141 V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
Ted Kremenek0898e862007-12-13 05:14:22 +0000142 else {
143 // Special case for declarations of array types. For things like:
144 //
145 // char x[10];
146 //
147 // we should treat "x" as being initialized, because the variable
148 // "x" really refers to the memory block. Clearly x[1] is
149 // uninitialized, but expressions like "(char *) x" really do refer to
150 // an initialized value. This simple dataflow analysis does not reason
151 // about the contents of arrays, although it could be potentially
152 // extended to do so if the array were of constant size.
153 if (VD->getType()->isArrayType())
154 V(VD,AD) = Initialized;
155 else
156 V(VD,AD) = Uninitialized;
157 }
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000158 }
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000159 }
Ted Kremenek6b576492007-09-28 00:09:38 +0000160 return Uninitialized; // Value is never consumed.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000161}
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000162
Ted Kremenek334b30a2007-09-17 18:31:23 +0000163bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremeneka1d35862007-09-18 21:47:41 +0000164 VisitChildren(C);
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000165 return Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000166}
167
168bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000169 switch (U->getOpcode()) {
170 case UnaryOperator::AddrOf:
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000171 VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
172 if (VD && VD->isBlockVarDecl())
Ted Kremeneke33d1002007-12-13 04:47:15 +0000173 return V(VD,AD) = Initialized;
Ted Kremeneke33d1002007-12-13 04:47:15 +0000174 break;
175
176 case UnaryOperator::SizeOf:
177 return Initialized;
178
179 default:
180 break;
181 }
Ted Kremenek334b30a2007-09-17 18:31:23 +0000182
Ted Kremenek6b576492007-09-28 00:09:38 +0000183 return Visit(U->getSubExpr());
184}
185
186bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
187 Visit(C->getCond());
Anders Carlsson37365fc2007-11-30 19:04:31 +0000188
189 bool rhsResult = Visit(C->getRHS());
190 // Handle the GNU extension for missing LHS.
191 if (Expr *lhs = C->getLHS())
192 return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
193 else
194 return rhsResult;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000195}
196
197bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000198 bool x = Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000199
200 // We don't stop at the first subexpression that is Uninitialized because
201 // evaluating some subexpressions may result in propogating "Uninitialized"
202 // or "Initialized" to variables referenced in the other subexpressions.
203 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenek6b576492007-09-28 00:09:38 +0000204 if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000205
206 return x;
207}
Ted Kremenek6b576492007-09-28 00:09:38 +0000208
209bool TransferFuncs::Visit(Stmt *S) {
210 if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
211 else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
212}
Ted Kremenek334b30a2007-09-17 18:31:23 +0000213
214bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenekc6fda602008-01-26 00:03:27 +0000215 bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
216 if (AD.isTracked(E)) V(E,AD) = x;
217 return x;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000218}
219
Ted Kremenek7f49f502007-09-14 22:49:21 +0000220} // end anonymous namespace
221
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000222//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000223// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000224//
225// In our transfer functions we take the approach that any
226// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
227//
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000228// Merges take the same approach, preferring soundness. At a confluence point,
229// if any predecessor has a variable marked uninitialized, the value is
230// uninitialized at the confluence point.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000231//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000232
233namespace {
Ted Kremenek24d83ad2008-03-22 20:11:00 +0000234 typedef ExprDeclBitVector_Types::Intersect Merge;
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000235 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
236}
Ted Kremenek7f49f502007-09-14 22:49:21 +0000237
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000238//===----------------------------------------------------------------------===//
239// Unitialized values checker. Scan an AST and flag variable uses
240//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000241
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000242UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
243
244namespace {
Ted Kremenekec818352008-01-08 18:19:08 +0000245class VISIBILITY_HIDDEN UninitializedValuesChecker
246 : public UninitializedValues::ObserverTy {
247
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000248 ASTContext &Ctx;
249 Diagnostic &Diags;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000250 llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000251
252public:
253 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
254 : Ctx(ctx), Diags(diags) {}
255
256 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
257 UninitializedValues::AnalysisDataTy& AD,
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000258 DeclRefExpr* DR, VarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000259
Ted Kremenekf92ba512007-09-18 21:43:18 +0000260 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
261
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000262 if (V(VD,AD) == Uninitialized)
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000263 if (AlreadyWarned.insert(VD))
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000264 Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
265 diag::warn_uninit_val);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000266 }
267};
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000268} // end anonymous namespace
269
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000270namespace clang {
Ted Kremenek6b576492007-09-28 00:09:38 +0000271void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
272 bool FullUninitTaint) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000273
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000274 // Compute the unitialized values information.
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000275 UninitializedValues U(cfg);
Ted Kremenek6b576492007-09-28 00:09:38 +0000276 U.getAnalysisData().FullUninitTaint = FullUninitTaint;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000277 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000278 S.runOnCFG(cfg);
279
280 // Scan for DeclRefExprs that use uninitialized values.
281 UninitializedValuesChecker Observer(Ctx,Diags);
282 U.getAnalysisData().Observer = &Observer;
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000283 S.runOnAllBlocks(cfg);
Ted Kremenek7f49f502007-09-14 22:49:21 +0000284}
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000285} // end namespace clang