blob: 09f195e382656ac80f341504caabb9f07caa8e84 [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
Ted Kremenek68447a62007-09-18 20:59:00 +000054 void VisitDeclRefExpr(DeclRefExpr* DR) { VisitDeclChain(DR->getDecl()); }
55 void VisitDeclStmt(DeclStmt* S) { VisitDeclChain(S->getDecl()); }
56 void VisitStmt(Stmt* S) { VisitChildren(S); }
57 void operator()(Stmt* S) { BlockStmt_Visit(S); }
Ted Kremenek7f49f502007-09-14 22:49:21 +000058};
59
60} // end anonymous namespace
61
62void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000063 RegisterDeclsAndExprs R(this->getAnalysisData());
Ted Kremenek68447a62007-09-18 20:59:00 +000064 cfg.VisitBlockStmts(R);
Ted Kremenek7f49f502007-09-14 22:49:21 +000065}
66
Ted Kremenek3871d8e2007-09-17 19:59:27 +000067//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000068// Transfer functions.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000069//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000070
71namespace {
Ted Kremenek334b30a2007-09-17 18:31:23 +000072
Ted Kremenek7f49f502007-09-14 22:49:21 +000073class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
74 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000075 UninitializedValues::AnalysisDataTy& AD;
Ted Kremeneke6148f32007-09-17 21:59:08 +000076 bool InitWithAssigns;
Ted Kremenek7f49f502007-09-14 22:49:21 +000077public:
Ted Kremeneke6148f32007-09-17 21:59:08 +000078 TransferFuncs(UninitializedValues::AnalysisDataTy& ad,
79 bool init_with_assigns=true) :
80 AD(ad), InitWithAssigns(init_with_assigns) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000081 V.resetValues(AD);
Ted Kremenek7f49f502007-09-14 22:49:21 +000082 }
83
84 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek3e039752007-09-17 17:14:52 +000085
Ted Kremenek334b30a2007-09-17 18:31:23 +000086 bool VisitDeclRefExpr(DeclRefExpr* DR);
87 bool VisitBinaryOperator(BinaryOperator* B);
88 bool VisitUnaryOperator(UnaryOperator* U);
89 bool VisitStmt(Stmt* S);
90 bool VisitCallExpr(CallExpr* C);
91 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek3871d8e2007-09-17 19:59:27 +000092 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek334b30a2007-09-17 18:31:23 +000093
94 static inline bool Initialized() { return true; }
Ted Kremenek3871d8e2007-09-17 19:59:27 +000095 static inline bool Uninitialized() { return false; }
Ted Kremenek7f49f502007-09-14 22:49:21 +000096};
Ted Kremenek334b30a2007-09-17 18:31:23 +000097
98
99bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000100 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000101 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000102 if (AD.Observer)
103 AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
104
Ted Kremenek334b30a2007-09-17 18:31:23 +0000105 return V.DeclBV[ AD.VMap[VD] ];
106 }
107 else
108 return Initialized();
109}
110
111bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
112 if (CFG::hasImplicitControlFlow(B)) {
113 assert ( AD.EMap.find(B) != AD.EMap.end() && "Unknown block-level expr.");
114 return V.ExprBV[ AD.EMap[B] ];
115 }
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000116
117 if (B->isAssignmentOp()) {
118 // Get the Decl for the LHS, if any
119 for (Stmt* S = B->getLHS() ;; ) {
120 if (ParenExpr* P = dyn_cast<ParenExpr>(S))
121 S = P->getSubExpr();
122 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000123 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000124 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremeneke6148f32007-09-17 21:59:08 +0000125
126 if(InitWithAssigns) {
127 // Pseudo-hack to prevent cascade of warnings. If the RHS uses
128 // an uninitialized value, then we are already going to flag a warning
129 // related to the "cause". Thus, propogating uninitialized doesn't
130 // make sense, since we are just adding extra messages that don't
131 // contribute to diagnosing the bug. In InitWithAssigns mode
132 // we unconditionally set the assigned variable to Initialized to
133 // prevent Uninitialized propogation.
134 return V.DeclBV[AD.VMap[VD]] = Initialized();
135 }
136 else
137 return V.DeclBV[ AD.VMap[VD] ] = Visit(B->getRHS());
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000138 }
Ted Kremeneke6148f32007-09-17 21:59:08 +0000139
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000140 break;
141 }
142 }
143
Ted Kremenek334b30a2007-09-17 18:31:23 +0000144 return VisitStmt(B);
145}
146
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000147bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
148 bool x = Initialized();
149
150 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000151 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D))
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000152 if (Stmt* I = VD->getInit()) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000153 assert ( AD.EMap.find(cast<Expr>(I)) !=
154 AD.EMap.end() && "Unknown Expr.");
155
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000156 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
Ted Kremeneke6148f32007-09-17 21:59:08 +0000157 x = V.ExprBV[ AD.EMap[cast<Expr>(I)] ];
158 V.DeclBV[ AD.VMap[VD] ] = x;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000159 }
160
161 return x;
162}
163
Ted Kremenek334b30a2007-09-17 18:31:23 +0000164bool TransferFuncs::VisitCallExpr(CallExpr* C) {
165 VisitStmt(C);
166 return Initialized();
167}
168
169bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
170 switch (U->getOpcode()) {
171 case UnaryOperator::AddrOf: {
172 // Blast through parentheses and find the decl (if any). Treat it
173 // as initialized from this point forward.
174 for (Stmt* S = U->getSubExpr() ;; )
175 if (ParenExpr* P = dyn_cast<ParenExpr>(S))
176 S = P->getSubExpr();
177 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000178 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenek334b30a2007-09-17 18:31:23 +0000179 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
180 V.DeclBV[ AD.VMap[VD] ] = Initialized();
181 }
182 break;
183 }
184 else {
185 // Evaluate the transfer function for subexpressions, even
186 // if we cannot reason more deeply about the &-expression.
187 return Visit(U->getSubExpr());
188 }
189
190 return Initialized();
191 }
192
193 default:
194 return Visit(U->getSubExpr());
195 }
196}
197
198bool TransferFuncs::VisitStmt(Stmt* S) {
199 bool x = Initialized();
200
201 // We don't stop at the first subexpression that is Uninitialized because
202 // evaluating some subexpressions may result in propogating "Uninitialized"
203 // or "Initialized" to variables referenced in the other subexpressions.
204 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000205 if (Visit(*I) == Uninitialized())
206 x = Uninitialized();
Ted Kremenek334b30a2007-09-17 18:31:23 +0000207
208 return x;
209}
210
211bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
212 assert ( AD.EMap.find(E) != AD.EMap.end() );
213 return V.ExprBV[ AD.EMap[E] ] = Visit(E);
214}
215
Ted Kremenek7f49f502007-09-14 22:49:21 +0000216} // end anonymous namespace
217
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000218//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000219// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000220//
221// In our transfer functions we take the approach that any
222// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
223//
224// Merges take the opposite approach.
225//
Ted Kremenek68447a62007-09-18 20:59:00 +0000226// In the merge of dataflow values we prefer unsoundness, and
Ted Kremenek334b30a2007-09-17 18:31:23 +0000227// prefer false negatives to false positives. At merges, if a value for a
228// tracked Decl is EVER initialized in any of the predecessors we treat it as
229// initialized at the confluence point.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000230//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000231
232namespace {
233struct Merge {
234 void operator()(UninitializedValues::ValTy& Dst,
235 UninitializedValues::ValTy& Src) {
Ted Kremenek68447a62007-09-18 20:59:00 +0000236 assert (Dst.DeclBV.size() == Src.DeclBV.size() && "BV sizes do not match.");
237 assert (Dst.ExprBV.size() == Src.ExprBV.size() && "BV sizes do not match.");
Ted Kremenek334b30a2007-09-17 18:31:23 +0000238
Ted Kremenek68447a62007-09-18 20:59:00 +0000239 Dst.DeclBV |= Src.DeclBV;
240 Dst.ExprBV |= Src.ExprBV;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000241 }
242};
243} // end anonymous namespace
244
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000245//===----------------------------------------------------------------------===//
246// Unitialized values checker. Scan an AST and flag variable uses
247//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000248
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000249UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
250
251namespace {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000252class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
253 ASTContext &Ctx;
254 Diagnostic &Diags;
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000255 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000256
257public:
258 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
259 : Ctx(ctx), Diags(diags) {}
260
261 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
262 UninitializedValues::AnalysisDataTy& AD,
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000263 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000264
265 assert ( AD.VMap.find(VD) != AD.VMap.end() && "Unknown VarDecl.");
266 if (V.DeclBV[ AD.VMap[VD] ] == TransferFuncs::Uninitialized())
267 if (AlreadyWarned.insert(VD))
268 Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
269 }
270};
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000271} // end anonymous namespace
272
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000273namespace clang {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000274void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000275
276 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000277
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000278 // Compute the unitialized values information.
279 UninitializedValues U;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000280 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000281 S.runOnCFG(cfg);
282
283 // Scan for DeclRefExprs that use uninitialized values.
284 UninitializedValuesChecker Observer(Ctx,Diags);
285 U.getAnalysisData().Observer = &Observer;
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000286 S.runOnAllBlocks(cfg);
Ted Kremenek7f49f502007-09-14 22:49:21 +0000287}
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000288} // end namespace clang