blob: c472b3a846516b5618c0da21420bd50a332e2b6a [file] [log] [blame]
Ted Kremenek7f49f502007-09-14 22:49:21 +00001//==- UninitializedValues.cpp - Find Unintialized Values --------*- C++ --*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Uninitialized Values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/UninitializedValues.h"
Ted Kremenek7f49f502007-09-14 22:49:21 +000015#include "clang/Analysis/CFGStmtVisitor.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 Kremenek7f49f502007-09-14 22:49:21 +000019#include "DataflowSolver.h"
20
Ted Kremenek3871d8e2007-09-17 19:59:27 +000021#include "llvm/ADT/SmallPtrSet.h"
22
Ted Kremenek7f49f502007-09-14 22:49:21 +000023using namespace clang;
24
Ted Kremenek3871d8e2007-09-17 19:59:27 +000025//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000026// Dataflow initialization logic.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000027//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000028
29namespace {
30
Ted Kremenek0a03ce62007-09-17 20:49:30 +000031class RegisterDeclsAndExprs : public CFGStmtVisitor<RegisterDeclsAndExprs> {
Ted Kremenek3e039752007-09-17 17:14:52 +000032 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek7f49f502007-09-14 22:49:21 +000033public:
Ted Kremenek0a03ce62007-09-17 20:49:30 +000034 RegisterDeclsAndExprs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek7f49f502007-09-14 22:49:21 +000035
Ted Kremenek0a03ce62007-09-17 20:49:30 +000036 void VisitBlockVarDecl(BlockVarDecl* VD) {
37 if (AD.VMap.find(VD) == AD.VMap.end())
38 AD.VMap[VD] = AD.NumDecls++;
39 }
40
41 void VisitDeclChain(ScopedDecl* D) {
42 for (; D != NULL; D = D->getNextDeclarator())
43 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D))
44 VisitBlockVarDecl(VD);
Ted Kremenek3e039752007-09-17 17:14:52 +000045 }
46
47 void BlockStmt_VisitExpr(Expr* E) {
48 if (AD.EMap.find(E) == AD.EMap.end())
Ted Kremenek334b30a2007-09-17 18:31:23 +000049 AD.EMap[E] = AD.NumBlockExprs++;
Ted Kremenek0a03ce62007-09-17 20:49:30 +000050
51 Visit(E);
52 }
53
54 void VisitDeclRefExpr(DeclRefExpr* DR) {
55 VisitDeclChain(DR->getDecl());
56 }
57
58 void VisitDeclStmt(DeclStmt* S) {
59 VisitDeclChain(S->getDecl());
60 }
61
62 void VisitStmt(Stmt* S) {
63 VisitChildren(S);
64 }
65
Ted Kremenek7f49f502007-09-14 22:49:21 +000066};
67
68} // end anonymous namespace
69
70void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000071 RegisterDeclsAndExprs R(this->getAnalysisData());
72
73 for (CFG::const_iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I)
74 for (CFGBlock::const_iterator BI=I->begin(), BE=I->end(); BI!=BE; ++BI)
75 R.BlockStmt_Visit(*BI);
Ted Kremenek7f49f502007-09-14 22:49:21 +000076}
77
Ted Kremenek3871d8e2007-09-17 19:59:27 +000078//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000079// Transfer functions.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000080//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000081
82namespace {
Ted Kremenek334b30a2007-09-17 18:31:23 +000083
Ted Kremenek7f49f502007-09-14 22:49:21 +000084class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
85 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000086 UninitializedValues::AnalysisDataTy& AD;
Ted Kremeneke6148f32007-09-17 21:59:08 +000087 bool InitWithAssigns;
Ted Kremenek7f49f502007-09-14 22:49:21 +000088public:
Ted Kremeneke6148f32007-09-17 21:59:08 +000089 TransferFuncs(UninitializedValues::AnalysisDataTy& ad,
90 bool init_with_assigns=true) :
91 AD(ad), InitWithAssigns(init_with_assigns) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000092 V.resetValues(AD);
Ted Kremenek7f49f502007-09-14 22:49:21 +000093 }
94
95 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek3e039752007-09-17 17:14:52 +000096
Ted Kremenek334b30a2007-09-17 18:31:23 +000097 bool VisitDeclRefExpr(DeclRefExpr* DR);
98 bool VisitBinaryOperator(BinaryOperator* B);
99 bool VisitUnaryOperator(UnaryOperator* U);
100 bool VisitStmt(Stmt* S);
101 bool VisitCallExpr(CallExpr* C);
102 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000103 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000104
105 static inline bool Initialized() { return true; }
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000106 static inline bool Uninitialized() { return false; }
Ted Kremenek7f49f502007-09-14 22:49:21 +0000107};
Ted Kremenek334b30a2007-09-17 18:31:23 +0000108
109
110bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000111 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000112 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000113 if (AD.Observer)
114 AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
115
Ted Kremenek334b30a2007-09-17 18:31:23 +0000116 return V.DeclBV[ AD.VMap[VD] ];
117 }
118 else
119 return Initialized();
120}
121
122bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
123 if (CFG::hasImplicitControlFlow(B)) {
124 assert ( AD.EMap.find(B) != AD.EMap.end() && "Unknown block-level expr.");
125 return V.ExprBV[ AD.EMap[B] ];
126 }
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000127
128 if (B->isAssignmentOp()) {
129 // Get the Decl for the LHS, if any
130 for (Stmt* S = B->getLHS() ;; ) {
131 if (ParenExpr* P = dyn_cast<ParenExpr>(S))
132 S = P->getSubExpr();
133 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000134 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000135 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremeneke6148f32007-09-17 21:59:08 +0000136
137 if(InitWithAssigns) {
138 // Pseudo-hack to prevent cascade of warnings. If the RHS uses
139 // an uninitialized value, then we are already going to flag a warning
140 // related to the "cause". Thus, propogating uninitialized doesn't
141 // make sense, since we are just adding extra messages that don't
142 // contribute to diagnosing the bug. In InitWithAssigns mode
143 // we unconditionally set the assigned variable to Initialized to
144 // prevent Uninitialized propogation.
145 return V.DeclBV[AD.VMap[VD]] = Initialized();
146 }
147 else
148 return V.DeclBV[ AD.VMap[VD] ] = Visit(B->getRHS());
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000149 }
Ted Kremeneke6148f32007-09-17 21:59:08 +0000150
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000151 break;
152 }
153 }
154
Ted Kremenek334b30a2007-09-17 18:31:23 +0000155 return VisitStmt(B);
156}
157
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000158bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
159 bool x = Initialized();
160
161 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000162 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D))
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000163 if (Stmt* I = VD->getInit()) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000164 assert ( AD.EMap.find(cast<Expr>(I)) !=
165 AD.EMap.end() && "Unknown Expr.");
166
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000167 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremeneke6148f32007-09-17 21:59:08 +0000168 x = V.ExprBV[ AD.EMap[cast<Expr>(I)] ];
169 V.DeclBV[ AD.VMap[VD] ] = x;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000170 }
171
172 return x;
173}
174
Ted Kremenek334b30a2007-09-17 18:31:23 +0000175bool TransferFuncs::VisitCallExpr(CallExpr* C) {
176 VisitStmt(C);
177 return Initialized();
178}
179
180bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
181 switch (U->getOpcode()) {
182 case UnaryOperator::AddrOf: {
183 // Blast through parentheses and find the decl (if any). Treat it
184 // as initialized from this point forward.
185 for (Stmt* S = U->getSubExpr() ;; )
186 if (ParenExpr* P = dyn_cast<ParenExpr>(S))
187 S = P->getSubExpr();
188 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000189 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000190 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
191 V.DeclBV[ AD.VMap[VD] ] = Initialized();
192 }
193 break;
194 }
195 else {
196 // Evaluate the transfer function for subexpressions, even
197 // if we cannot reason more deeply about the &-expression.
198 return Visit(U->getSubExpr());
199 }
200
201 return Initialized();
202 }
203
204 default:
205 return Visit(U->getSubExpr());
206 }
207}
208
209bool TransferFuncs::VisitStmt(Stmt* S) {
210 bool x = Initialized();
211
212 // We don't stop at the first subexpression that is Uninitialized because
213 // evaluating some subexpressions may result in propogating "Uninitialized"
214 // or "Initialized" to variables referenced in the other subexpressions.
215 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000216 if (Visit(*I) == Uninitialized())
217 x = Uninitialized();
Ted Kremenek334b30a2007-09-17 18:31:23 +0000218
219 return x;
220}
221
222bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
223 assert ( AD.EMap.find(E) != AD.EMap.end() );
224 return V.ExprBV[ AD.EMap[E] ] = Visit(E);
225}
226
Ted Kremenek7f49f502007-09-14 22:49:21 +0000227} // end anonymous namespace
228
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000229//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000230// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000231//
232// In our transfer functions we take the approach that any
233// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
234//
235// Merges take the opposite approach.
236//
237// In the merge of dataflow values (for Decls) we prefer unsoundness, and
238// prefer false negatives to false positives. At merges, if a value for a
239// tracked Decl is EVER initialized in any of the predecessors we treat it as
240// initialized at the confluence point.
241//
242// For tracked CFGBlock-level expressions (such as the result of
243// short-circuit), we do the opposite merge: if a value is EVER uninitialized
244// in a predecessor we treat it as uninitalized at the confluence point.
245// The reason we do this is because dataflow values for tracked Exprs are
246// not as control-dependent as dataflow values for tracked Decls.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000247//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000248
249namespace {
250struct Merge {
251 void operator()(UninitializedValues::ValTy& Dst,
252 UninitializedValues::ValTy& Src) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000253 assert (Dst.DeclBV.size() == Src.DeclBV.size()
254 && "Bitvector sizes do not match.");
255
256 Dst.DeclBV |= Src.DeclBV;
257
258 assert (Dst.ExprBV.size() == Src.ExprBV.size()
259 && "Bitvector sizes do not match.");
260
Ted Kremeneke6148f32007-09-17 21:59:08 +0000261 Dst.ExprBV &= Src.ExprBV;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000262 }
263};
264} // end anonymous namespace
265
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000266//===----------------------------------------------------------------------===//
267// Unitialized values checker. Scan an AST and flag variable uses
268//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000269
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000270UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
271
272namespace {
273
274class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
275 ASTContext &Ctx;
276 Diagnostic &Diags;
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000277 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-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,
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000285 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000286
287 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
288 if (V.DeclBV[ AD.VMap[VD] ] == TransferFuncs::Uninitialized())
289 if (AlreadyWarned.insert(VD))
290 Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
291 }
292};
293
294} // end anonymous namespace
295
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000296namespace clang {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000297
298void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000299
300 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000301
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000302 // Compute the unitialized values information.
303 UninitializedValues U;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000304 Solver S(U);
Ted Kremenek3871d8e2007-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;
310
311 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I)
Ted Kremenek7f49f502007-09-14 22:49:21 +0000312 S.runOnBlock(&*I);
313}
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000314
315}