blob: 63c9e9f2f94e2c15b54970f8576d494247b9d10e [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
Ted Kremenekf92ba512007-09-18 21:43:18 +000094 BlockVarDecl* FindBlockVarDecl(Stmt* S);
95
Ted Kremenek334b30a2007-09-17 18:31:23 +000096 static inline bool Initialized() { return true; }
Ted Kremenek3871d8e2007-09-17 19:59:27 +000097 static inline bool Uninitialized() { return false; }
Ted Kremenek7f49f502007-09-14 22:49:21 +000098};
Ted Kremenek334b30a2007-09-17 18:31:23 +000099
100
101bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000102 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000103 if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000104
Ted Kremenekf92ba512007-09-18 21:43:18 +0000105 return V.getBitRef(VD,AD);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000106 }
Ted Kremenekf92ba512007-09-18 21:43:18 +0000107 else return Initialized();
108}
109
110BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) {
111 for (;;) {
112 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
113 S = P->getSubExpr();
114 continue;
115 }
116 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
117 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl()))
118 return VD;
119
120 return NULL;
121 }
Ted Kremenek334b30a2007-09-17 18:31:23 +0000122}
123
124bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremeneke6148f32007-09-17 21:59:08 +0000125
Ted Kremenekf92ba512007-09-18 21:43:18 +0000126 if (CFG::hasImplicitControlFlow(B))
127 return V.getBitRef(B,AD);
128
129 if (B->isAssignmentOp())
130 // Get the Decl for the LHS (if any).
131 if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS()))
132 if(InitWithAssigns) {
133 // Pseudo-hack to prevent cascade of warnings. If the RHS uses
134 // an uninitialized value, then we are already going to flag a warning
135 // for the RHS, or for the root "source" of the unintialized values.
136 // Thus, propogating uninitialized doesn't make sense, since we are
137 // just adding extra messages that don't
138 // contribute to diagnosing the bug. In InitWithAssigns mode
139 // we unconditionally set the assigned variable to Initialized to
140 // prevent Uninitialized propogation.
141 return V.getBitRef(VD,AD) = Initialized();
142 }
143 else return V.getBitRef(VD,AD) = Visit(B->getRHS());
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000144
Ted Kremenek334b30a2007-09-17 18:31:23 +0000145 return VisitStmt(B);
146}
147
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000148bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
149 bool x = Initialized();
150
151 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000152 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D))
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000153 if (Stmt* I = VD->getInit()) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000154 x = V.getBitRef(cast<Expr>(I),AD);
155 V.getBitRef(VD,AD) = x;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000156 }
157
158 return x;
159}
160
Ted Kremenek334b30a2007-09-17 18:31:23 +0000161bool TransferFuncs::VisitCallExpr(CallExpr* C) {
162 VisitStmt(C);
163 return Initialized();
164}
165
166bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
167 switch (U->getOpcode()) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000168 case UnaryOperator::AddrOf:
169 // For "&x", treat "x" as now being initialized.
170 if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr()))
171 V.getBitRef(VD,AD) = Initialized();
172 else
173 return Visit(U->getSubExpr());
Ted Kremenek334b30a2007-09-17 18:31:23 +0000174
175 default:
176 return Visit(U->getSubExpr());
177 }
178}
179
180bool TransferFuncs::VisitStmt(Stmt* S) {
181 bool x = Initialized();
182
183 // We don't stop at the first subexpression that is Uninitialized because
184 // evaluating some subexpressions may result in propogating "Uninitialized"
185 // or "Initialized" to variables referenced in the other subexpressions.
186 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenekf92ba512007-09-18 21:43:18 +0000187 if (Visit(*I) == Uninitialized()) x = Uninitialized();
Ted Kremenek334b30a2007-09-17 18:31:23 +0000188
189 return x;
190}
191
192bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000193 assert (AD.isTracked(E));
194 return V.getBitRef(E,AD) = Visit(E);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000195}
196
Ted Kremenek7f49f502007-09-14 22:49:21 +0000197} // end anonymous namespace
198
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000199//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000200// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000201//
202// In our transfer functions we take the approach that any
203// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
204//
205// Merges take the opposite approach.
206//
Ted Kremenek68447a62007-09-18 20:59:00 +0000207// In the merge of dataflow values we prefer unsoundness, and
Ted Kremenek334b30a2007-09-17 18:31:23 +0000208// prefer false negatives to false positives. At merges, if a value for a
209// tracked Decl is EVER initialized in any of the predecessors we treat it as
210// initialized at the confluence point.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000211//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000212
213namespace {
214struct Merge {
215 void operator()(UninitializedValues::ValTy& Dst,
216 UninitializedValues::ValTy& Src) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000217 assert (Src.sizesEqual(Dst) && "BV sizes do not match.");
Ted Kremenek68447a62007-09-18 20:59:00 +0000218 Dst.DeclBV |= Src.DeclBV;
219 Dst.ExprBV |= Src.ExprBV;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000220 }
221};
222} // end anonymous namespace
223
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000224//===----------------------------------------------------------------------===//
225// Unitialized values checker. Scan an AST and flag variable uses
226//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000227
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000228UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
229
230namespace {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000231class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
232 ASTContext &Ctx;
233 Diagnostic &Diags;
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000234 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000235
236public:
237 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
238 : Ctx(ctx), Diags(diags) {}
239
240 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
241 UninitializedValues::AnalysisDataTy& AD,
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000242 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000243
Ted Kremenekf92ba512007-09-18 21:43:18 +0000244 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
245
246 if (V.getBitRef(VD,AD) == TransferFuncs::Uninitialized())
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000247 if (AlreadyWarned.insert(VD))
248 Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
249 }
250};
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000251} // end anonymous namespace
252
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000253namespace clang {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000254void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000255
256 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000257
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000258 // Compute the unitialized values information.
259 UninitializedValues U;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000260 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000261 S.runOnCFG(cfg);
262
263 // Scan for DeclRefExprs that use uninitialized values.
264 UninitializedValuesChecker Observer(Ctx,Diags);
265 U.getAnalysisData().Observer = &Observer;
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000266 S.runOnAllBlocks(cfg);
Ted Kremenek7f49f502007-09-14 22:49:21 +0000267}
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000268} // end namespace clang