blob: c5b13a4074af14fa547a59a397a62baee5830aa5 [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);
76
77 // Initialize the values of the last block.
Ted Kremeneke6148f32007-09-17 21:59:08 +000078// UninitializedValues::ValTy& V = getBlockDataMap()[&cfg.getEntry()];
79// V.resetValues(getAnalysisData());
Ted Kremenek7f49f502007-09-14 22:49:21 +000080}
81
Ted Kremenek3871d8e2007-09-17 19:59:27 +000082//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000083// Transfer functions.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000084//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000085
86namespace {
Ted Kremenek334b30a2007-09-17 18:31:23 +000087
Ted Kremenek7f49f502007-09-14 22:49:21 +000088class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
89 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000090 UninitializedValues::AnalysisDataTy& AD;
Ted Kremeneke6148f32007-09-17 21:59:08 +000091 bool InitWithAssigns;
Ted Kremenek7f49f502007-09-14 22:49:21 +000092public:
Ted Kremeneke6148f32007-09-17 21:59:08 +000093 TransferFuncs(UninitializedValues::AnalysisDataTy& ad,
94 bool init_with_assigns=true) :
95 AD(ad), InitWithAssigns(init_with_assigns) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000096 V.resetValues(AD);
Ted Kremenek7f49f502007-09-14 22:49:21 +000097 }
98
99 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek3e039752007-09-17 17:14:52 +0000100
Ted Kremenek334b30a2007-09-17 18:31:23 +0000101 bool VisitDeclRefExpr(DeclRefExpr* DR);
102 bool VisitBinaryOperator(BinaryOperator* B);
103 bool VisitUnaryOperator(UnaryOperator* U);
104 bool VisitStmt(Stmt* S);
105 bool VisitCallExpr(CallExpr* C);
106 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000107 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000108
109 static inline bool Initialized() { return true; }
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000110 static inline bool Uninitialized() { return false; }
Ted Kremenek7f49f502007-09-14 22:49:21 +0000111};
Ted Kremenek334b30a2007-09-17 18:31:23 +0000112
113
114bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000115 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000116 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000117 if (AD.Observer)
118 AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
119
Ted Kremenek334b30a2007-09-17 18:31:23 +0000120 return V.DeclBV[ AD.VMap[VD] ];
121 }
122 else
123 return Initialized();
124}
125
126bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
127 if (CFG::hasImplicitControlFlow(B)) {
128 assert ( AD.EMap.find(B) != AD.EMap.end() && "Unknown block-level expr.");
129 return V.ExprBV[ AD.EMap[B] ];
130 }
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000131
132 if (B->isAssignmentOp()) {
133 // Get the Decl for the LHS, if any
134 for (Stmt* S = B->getLHS() ;; ) {
135 if (ParenExpr* P = dyn_cast<ParenExpr>(S))
136 S = P->getSubExpr();
137 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000138 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000139 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremeneke6148f32007-09-17 21:59:08 +0000140
141 if(InitWithAssigns) {
142 // Pseudo-hack to prevent cascade of warnings. If the RHS uses
143 // an uninitialized value, then we are already going to flag a warning
144 // related to the "cause". Thus, propogating uninitialized doesn't
145 // make sense, since we are just adding extra messages that don't
146 // contribute to diagnosing the bug. In InitWithAssigns mode
147 // we unconditionally set the assigned variable to Initialized to
148 // prevent Uninitialized propogation.
149 return V.DeclBV[AD.VMap[VD]] = Initialized();
150 }
151 else
152 return V.DeclBV[ AD.VMap[VD] ] = Visit(B->getRHS());
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000153 }
Ted Kremeneke6148f32007-09-17 21:59:08 +0000154
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000155 break;
156 }
157 }
158
Ted Kremenek334b30a2007-09-17 18:31:23 +0000159 return VisitStmt(B);
160}
161
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000162bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
163 bool x = Initialized();
164
165 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000166 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D))
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000167 if (Stmt* I = VD->getInit()) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000168 assert ( AD.EMap.find(cast<Expr>(I)) !=
169 AD.EMap.end() && "Unknown Expr.");
170
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000171 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremeneke6148f32007-09-17 21:59:08 +0000172 x = V.ExprBV[ AD.EMap[cast<Expr>(I)] ];
173 V.DeclBV[ AD.VMap[VD] ] = x;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000174 }
175
176 return x;
177}
178
Ted Kremenek334b30a2007-09-17 18:31:23 +0000179bool TransferFuncs::VisitCallExpr(CallExpr* C) {
180 VisitStmt(C);
181 return Initialized();
182}
183
184bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
185 switch (U->getOpcode()) {
186 case UnaryOperator::AddrOf: {
187 // Blast through parentheses and find the decl (if any). Treat it
188 // as initialized from this point forward.
189 for (Stmt* S = U->getSubExpr() ;; )
190 if (ParenExpr* P = dyn_cast<ParenExpr>(S))
191 S = P->getSubExpr();
192 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000193 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000194 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
195 V.DeclBV[ AD.VMap[VD] ] = Initialized();
196 }
197 break;
198 }
199 else {
200 // Evaluate the transfer function for subexpressions, even
201 // if we cannot reason more deeply about the &-expression.
202 return Visit(U->getSubExpr());
203 }
204
205 return Initialized();
206 }
207
208 default:
209 return Visit(U->getSubExpr());
210 }
211}
212
213bool TransferFuncs::VisitStmt(Stmt* S) {
214 bool x = Initialized();
215
216 // We don't stop at the first subexpression that is Uninitialized because
217 // evaluating some subexpressions may result in propogating "Uninitialized"
218 // or "Initialized" to variables referenced in the other subexpressions.
219 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000220 if (Visit(*I) == Uninitialized())
221 x = Uninitialized();
Ted Kremenek334b30a2007-09-17 18:31:23 +0000222
223 return x;
224}
225
226bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
227 assert ( AD.EMap.find(E) != AD.EMap.end() );
228 return V.ExprBV[ AD.EMap[E] ] = Visit(E);
229}
230
Ted Kremenek7f49f502007-09-14 22:49:21 +0000231} // end anonymous namespace
232
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000233//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000234// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000235//
236// In our transfer functions we take the approach that any
237// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
238//
239// Merges take the opposite approach.
240//
241// In the merge of dataflow values (for Decls) we prefer unsoundness, and
242// prefer false negatives to false positives. At merges, if a value for a
243// tracked Decl is EVER initialized in any of the predecessors we treat it as
244// initialized at the confluence point.
245//
246// For tracked CFGBlock-level expressions (such as the result of
247// short-circuit), we do the opposite merge: if a value is EVER uninitialized
248// in a predecessor we treat it as uninitalized at the confluence point.
249// The reason we do this is because dataflow values for tracked Exprs are
250// not as control-dependent as dataflow values for tracked Decls.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000251//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000252
253namespace {
254struct Merge {
255 void operator()(UninitializedValues::ValTy& Dst,
256 UninitializedValues::ValTy& Src) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000257 assert (Dst.DeclBV.size() == Src.DeclBV.size()
258 && "Bitvector sizes do not match.");
259
260 Dst.DeclBV |= Src.DeclBV;
261
262 assert (Dst.ExprBV.size() == Src.ExprBV.size()
263 && "Bitvector sizes do not match.");
264
Ted Kremeneke6148f32007-09-17 21:59:08 +0000265 Dst.ExprBV &= Src.ExprBV;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000266 }
267};
268} // end anonymous namespace
269
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000270//===----------------------------------------------------------------------===//
271// Unitialized values checker. Scan an AST and flag variable uses
272//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000273
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000274UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
275
276namespace {
277
278class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
279 ASTContext &Ctx;
280 Diagnostic &Diags;
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000281 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000282
283public:
284 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
285 : Ctx(ctx), Diags(diags) {}
286
287 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
288 UninitializedValues::AnalysisDataTy& AD,
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000289 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000290
291 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
292 if (V.DeclBV[ AD.VMap[VD] ] == TransferFuncs::Uninitialized())
293 if (AlreadyWarned.insert(VD))
294 Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
295 }
296};
297
298} // end anonymous namespace
299
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000300namespace clang {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000301
302void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000303
304 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000305
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000306 // Compute the unitialized values information.
307 UninitializedValues U;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000308 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000309 S.runOnCFG(cfg);
310
311 // Scan for DeclRefExprs that use uninitialized values.
312 UninitializedValuesChecker Observer(Ctx,Diags);
313 U.getAnalysisData().Observer = &Observer;
314
315 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I)
Ted Kremenek7f49f502007-09-14 22:49:21 +0000316 S.runOnBlock(&*I);
317}
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000318
319}