blob: bdc0e7c621f7fb6b5db47cb1b9b302c49f7ab619 [file] [log] [blame]
Nick Lewycky0fb45f62008-08-16 17:46:53 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenek5746d062007-09-14 22:49:21 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenek5746d062007-09-14 22:49:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Uninitialized Values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekbf593f82007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenek24c62442007-09-20 21:42:55 +000015#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Analysis/AnalysisDiagnostic.h"
Ted Kremenekdd301532007-09-17 19:59:27 +000017#include "clang/AST/ASTContext.h"
Ted Kremenek39fc60f2007-09-25 21:00:24 +000018#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenek5746d062007-09-14 22:49:21 +000019
Ted Kremenekdd301532007-09-17 19:59:27 +000020#include "llvm/ADT/SmallPtrSet.h"
21
Ted Kremenek5746d062007-09-14 22:49:21 +000022using namespace clang;
23
Ted Kremenekdd301532007-09-17 19:59:27 +000024//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +000025// Dataflow initialization logic.
Mike Stump11289f42009-09-09 15:08:12 +000026//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +000027
28namespace {
29
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000030class RegisterDecls
Mike Stump11289f42009-09-09 15:08:12 +000031 : public CFGRecStmtDeclVisitor<RegisterDecls> {
Ted Kremenek96b1ce42008-01-08 18:19:08 +000032
Ted Kremenek789ea072007-09-17 17:14:52 +000033 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek5746d062007-09-14 22:49:21 +000034public:
Ted Kremenekfb4750b2007-10-01 20:33:52 +000035 RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Mike Stump11289f42009-09-09 15:08:12 +000036
Ted Kremenek62044982008-04-15 23:02:18 +000037 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9d0acca2007-11-20 03:01:58 +000038 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek5746d062007-09-14 22:49:21 +000039};
Mike Stump11289f42009-09-09 15:08:12 +000040
Ted Kremenek5746d062007-09-14 22:49:21 +000041} // end anonymous namespace
42
43void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenekfb4750b2007-10-01 20:33:52 +000044 RegisterDecls R(getAnalysisData());
Ted Kremenek360c3b42007-09-18 20:59:00 +000045 cfg.VisitBlockStmts(R);
Ted Kremenek5746d062007-09-14 22:49:21 +000046}
47
Ted Kremenekdd301532007-09-17 19:59:27 +000048//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +000049// Transfer functions.
Mike Stump11289f42009-09-09 15:08:12 +000050//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +000051
52namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000053class TransferFuncs
Ted Kremenek96b1ce42008-01-08 18:19:08 +000054 : public CFGStmtVisitor<TransferFuncs,bool> {
Mike Stump11289f42009-09-09 15:08:12 +000055
Ted Kremenek5746d062007-09-14 22:49:21 +000056 UninitializedValues::ValTy V;
Ted Kremenek789ea072007-09-17 17:14:52 +000057 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek5746d062007-09-14 22:49:21 +000058public:
Ted Kremenek82ff6d62008-04-15 18:35:30 +000059 TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Mike Stump11289f42009-09-09 15:08:12 +000060
Ted Kremenek5746d062007-09-14 22:49:21 +000061 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek9d0acca2007-11-20 03:01:58 +000062 CFG& getCFG() { return AD.getCFG(); }
Mike Stump11289f42009-09-09 15:08:12 +000063
Ted Kremenek82ff6d62008-04-15 18:35:30 +000064 void SetTopValue(UninitializedValues::ValTy& X) {
Ted Kremenek7a188582008-11-11 19:41:42 +000065 X.setDeclValues(AD);
Ted Kremeneka9d3e6c2008-11-14 01:14:18 +000066 X.resetBlkExprValues(AD);
Ted Kremenek82ff6d62008-04-15 18:35:30 +000067 }
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenek6f075142007-09-17 18:31:23 +000069 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 Kremenekdd301532007-09-17 19:59:27 +000074 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenekf91d1c92007-09-28 00:09:38 +000075 bool VisitConditionalOperator(ConditionalOperator* C);
Ted Kremenek65dd30f2008-11-12 21:58:46 +000076 bool BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
Mike Stump11289f42009-09-09 15:08:12 +000077
Ted Kremenekf91d1c92007-09-28 00:09:38 +000078 bool Visit(Stmt *S);
79 bool BlockStmt_VisitExpr(Expr* E);
Mike Stump11289f42009-09-09 15:08:12 +000080
Ted Kremenekc1f9a282008-04-16 21:10:48 +000081 void VisitTerminator(CFGBlock* B) { }
Ted Kremenek5746d062007-09-14 22:49:21 +000082};
Mike Stump11289f42009-09-09 15:08:12 +000083
Ted Kremenek7a188582008-11-11 19:41:42 +000084static const bool Initialized = false;
Mike Stump11289f42009-09-09 15:08:12 +000085static const bool Uninitialized = true;
Ted Kremenek6f075142007-09-17 18:31:23 +000086
Ted Kremenek6f075142007-09-17 18:31:23 +000087bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Mike Stump11289f42009-09-09 15:08:12 +000088
Ted Kremeneke556f9e2008-04-16 02:59:55 +000089 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
90 if (VD->isBlockVarDecl()) {
Mike Stump11289f42009-09-09 15:08:12 +000091
Ted Kremeneke556f9e2008-04-16 02:59:55 +000092 if (AD.Observer)
93 AD.Observer->ObserveDeclRefExpr(V, AD, DR, VD);
Mike Stump11289f42009-09-09 15:08:12 +000094
Ted Kremeneke556f9e2008-04-16 02:59:55 +000095 // 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 Kremenekf91d1c92007-09-28 00:09:38 +0000100
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000101 if (AD.FullUninitTaint)
102 return V(VD,AD);
103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000105 return Initialized;
Ted Kremenek27116102007-09-18 21:43:18 +0000106}
107
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000108static VarDecl* FindBlockVarDecl(Expr* E) {
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000110 // Blast through casts and parentheses to find any DeclRefExprs that
111 // refer to a block VarDecl.
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000113 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
Mike Stump11289f42009-09-09 15:08:12 +0000114 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000115 if (VD->isBlockVarDecl()) return VD;
116
117 return NULL;
Ted Kremenek6f075142007-09-17 18:31:23 +0000118}
119
120bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremeneke556f9e2008-04-16 02:59:55 +0000121
122 if (VarDecl* VD = FindBlockVarDecl(B->getLHS()))
Ted Kremeneka1c256d2007-09-28 21:08:51 +0000123 if (B->isAssignmentOp()) {
Ted Kremenek2e04d732007-11-24 20:07:36 +0000124 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 Kremeneka1c256d2007-09-28 21:08:51 +0000128 }
129
Ted Kremenek6f075142007-09-17 18:31:23 +0000130 return VisitStmt(B);
131}
132
Ted Kremenekdd301532007-09-17 19:59:27 +0000133bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000134 for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) {
135 VarDecl *VD = dyn_cast<VarDecl>(*I);
Steve Naroff08899ff2008-04-15 22:42:06 +0000136 if (VD && VD->isBlockVarDecl()) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 if (Stmt* I = VD->getInit())
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000138 V(VD,AD) = AD.FullUninitTaint ? V(cast<Expr>(I),AD) : Initialized;
Ted Kremenek3a742d22007-12-13 05:14:22 +0000139 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
Mike Stump11289f42009-09-09 15:08:12 +0000146 // uninitialized, but expressions like "(char *) x" really do refer to
147 // an initialized value. This simple dataflow analysis does not reason
Ted Kremenek3a742d22007-12-13 05:14:22 +0000148 // 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;
Mike Stump11289f42009-09-09 15:08:12 +0000152 else
Ted Kremenek3a742d22007-12-13 05:14:22 +0000153 V(VD,AD) = Uninitialized;
154 }
Ted Kremenekb9ce2952007-09-27 18:20:22 +0000155 }
Steve Naroff08899ff2008-04-15 22:42:06 +0000156 }
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000157 return Uninitialized; // Value is never consumed.
Ted Kremenekdd301532007-09-17 19:59:27 +0000158}
Mike Stump11289f42009-09-09 15:08:12 +0000159
Ted Kremenek6f075142007-09-17 18:31:23 +0000160bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremenek8d4dcc52007-09-18 21:47:41 +0000161 VisitChildren(C);
Ted Kremenekb9ce2952007-09-27 18:20:22 +0000162 return Initialized;
Ted Kremenek6f075142007-09-17 18:31:23 +0000163}
164
165bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek78dcda62007-12-13 04:47:15 +0000166 switch (U->getOpcode()) {
Argyrios Kyrtzidisfc2f0582008-04-17 13:52:22 +0000167 case UnaryOperator::AddrOf: {
Steve Naroff08899ff2008-04-15 22:42:06 +0000168 VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
169 if (VD && VD->isBlockVarDecl())
Ted Kremenek78dcda62007-12-13 04:47:15 +0000170 return V(VD,AD) = Initialized;
Ted Kremenek78dcda62007-12-13 04:47:15 +0000171 break;
Argyrios Kyrtzidisfc2f0582008-04-17 13:52:22 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenek78dcda62007-12-13 04:47:15 +0000174 default:
175 break;
176 }
Ted Kremenek6f075142007-09-17 18:31:23 +0000177
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000178 return Visit(U->getSubExpr());
179}
Mike Stump11289f42009-09-09 15:08:12 +0000180
Ted Kremenek65dd30f2008-11-12 21:58:46 +0000181bool
182TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
Ted Kremenek7a188582008-11-11 19:41:42 +0000183 // This represents a use of the 'collection'
184 bool x = Visit(S->getCollection());
185
186 if (x == Uninitialized)
187 return Uninitialized;
188
189 // This represents an initialization of the 'element' value.
190 Stmt* Element = S->getElement();
191 VarDecl* VD = 0;
192
193 if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
Chris Lattner529efc72009-03-28 06:33:19 +0000194 VD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek8959a1a2008-11-14 18:21:25 +0000195 else {
196 Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
Ted Kremenek7a188582008-11-11 19:41:42 +0000197
Ted Kremenek8959a1a2008-11-14 18:21:25 +0000198 // Initialize the value of the reference variable.
199 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ElemExpr))
200 VD = cast<VarDecl>(DR->getDecl());
201 else
202 return Visit(ElemExpr);
203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Ted Kremenek7a188582008-11-11 19:41:42 +0000205 V(VD,AD) = Initialized;
206 return Initialized;
207}
Mike Stump11289f42009-09-09 15:08:12 +0000208
209
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000210bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
211 Visit(C->getCond());
Anders Carlsson801c5c72007-11-30 19:04:31 +0000212
213 bool rhsResult = Visit(C->getRHS());
214 // Handle the GNU extension for missing LHS.
215 if (Expr *lhs = C->getLHS())
216 return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
217 else
218 return rhsResult;
Ted Kremenek6f075142007-09-17 18:31:23 +0000219}
220
221bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekb9ce2952007-09-27 18:20:22 +0000222 bool x = Initialized;
Ted Kremenek6f075142007-09-17 18:31:23 +0000223
224 // We don't stop at the first subexpression that is Uninitialized because
225 // evaluating some subexpressions may result in propogating "Uninitialized"
226 // or "Initialized" to variables referenced in the other subexpressions.
227 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000228 if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
Mike Stump11289f42009-09-09 15:08:12 +0000229
Ted Kremenek6f075142007-09-17 18:31:23 +0000230 return x;
231}
Mike Stump11289f42009-09-09 15:08:12 +0000232
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000233bool TransferFuncs::Visit(Stmt *S) {
234 if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
235 else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
236}
Ted Kremenek6f075142007-09-17 18:31:23 +0000237
238bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Mike Stump11289f42009-09-09 15:08:12 +0000239 bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
Ted Kremenek95a123c2008-01-26 00:03:27 +0000240 if (AD.isTracked(E)) V(E,AD) = x;
241 return x;
Ted Kremenek6f075142007-09-17 18:31:23 +0000242}
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenek5746d062007-09-14 22:49:21 +0000244} // end anonymous namespace
245
Ted Kremenekdd301532007-09-17 19:59:27 +0000246//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +0000247// Merge operator.
Ted Kremenek6f075142007-09-17 18:31:23 +0000248//
249// In our transfer functions we take the approach that any
Nick Lewycky0fb45f62008-08-16 17:46:53 +0000250// combination of uninitialized values, e.g.
251// Uninitialized + ___ = Uninitialized.
Ted Kremenek6f075142007-09-17 18:31:23 +0000252//
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000253// Merges take the same approach, preferring soundness. At a confluence point,
254// if any predecessor has a variable marked uninitialized, the value is
255// uninitialized at the confluence point.
Mike Stump11289f42009-09-09 15:08:12 +0000256//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +0000257
258namespace {
Ted Kremeneka9d3e6c2008-11-14 01:14:18 +0000259 typedef StmtDeclBitVector_Types::Union Merge;
Ted Kremenekb9ce2952007-09-27 18:20:22 +0000260 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
261}
Ted Kremenek5746d062007-09-14 22:49:21 +0000262
Ted Kremenekdd301532007-09-17 19:59:27 +0000263//===----------------------------------------------------------------------===//
Nick Lewycky0fb45f62008-08-16 17:46:53 +0000264// Uninitialized values checker. Scan an AST and flag variable uses
Mike Stump11289f42009-09-09 15:08:12 +0000265//===----------------------------------------------------------------------===//
Ted Kremenek5746d062007-09-14 22:49:21 +0000266
Ted Kremenekdd301532007-09-17 19:59:27 +0000267UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
268
269namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000270class UninitializedValuesChecker
Ted Kremenek96b1ce42008-01-08 18:19:08 +0000271 : public UninitializedValues::ObserverTy {
Mike Stump11289f42009-09-09 15:08:12 +0000272
Ted Kremenekdd301532007-09-17 19:59:27 +0000273 ASTContext &Ctx;
274 Diagnostic &Diags;
Steve Naroff08899ff2008-04-15 22:42:06 +0000275 llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Ted Kremenekdd301532007-09-17 19:59:27 +0000277public:
278 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
279 : Ctx(ctx), Diags(diags) {}
Mike Stump11289f42009-09-09 15:08:12 +0000280
Ted Kremenekdd301532007-09-17 19:59:27 +0000281 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
282 UninitializedValues::AnalysisDataTy& AD,
Steve Naroff08899ff2008-04-15 22:42:06 +0000283 DeclRefExpr* DR, VarDecl* VD) {
Ted Kremenekdd301532007-09-17 19:59:27 +0000284
Ted Kremenek27116102007-09-18 21:43:18 +0000285 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
Mike Stump11289f42009-09-09 15:08:12 +0000286
Ted Kremenekb9ce2952007-09-27 18:20:22 +0000287 if (V(VD,AD) == Uninitialized)
Ted Kremenekdd301532007-09-17 19:59:27 +0000288 if (AlreadyWarned.insert(VD))
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000289 Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
290 diag::warn_uninit_val);
Ted Kremenekdd301532007-09-17 19:59:27 +0000291 }
292};
Ted Kremenekdd301532007-09-17 19:59:27 +0000293} // end anonymous namespace
294
Ted Kremenek7e61e812007-09-17 20:49:30 +0000295namespace clang {
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000296void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
297 bool FullUninitTaint) {
Mike Stump11289f42009-09-09 15:08:12 +0000298
Nick Lewycky0fb45f62008-08-16 17:46:53 +0000299 // Compute the uninitialized values information.
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000300 UninitializedValues U(cfg);
Ted Kremenekf91d1c92007-09-28 00:09:38 +0000301 U.getAnalysisData().FullUninitTaint = FullUninitTaint;
Ted Kremenek5746d062007-09-14 22:49:21 +0000302 Solver S(U);
Ted Kremenekdd301532007-09-17 19:59:27 +0000303 S.runOnCFG(cfg);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenekdd301532007-09-17 19:59:27 +0000305 // Scan for DeclRefExprs that use uninitialized values.
306 UninitializedValuesChecker Observer(Ctx,Diags);
307 U.getAnalysisData().Observer = &Observer;
Ted Kremenekb1361ea2007-09-18 21:08:21 +0000308 S.runOnAllBlocks(cfg);
Ted Kremenek5746d062007-09-14 22:49:21 +0000309}
Ted Kremenekb1361ea2007-09-18 21:08:21 +0000310} // end namespace clang