blob: ec209cd6700e2f62f3890febbcdedd137931e022 [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 Kremenek1ff41152007-09-19 18:00:03 +000015#include "clang/Analysis/CFGRecStmtDeclVisitor.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 Kremenek1ff41152007-09-19 18:00:03 +000031class RegisterDeclsExprs : public CFGRecStmtDeclVisitor<RegisterDeclsExprs> {
Ted Kremenek3e039752007-09-17 17:14:52 +000032 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek7f49f502007-09-14 22:49:21 +000033public:
Ted Kremenek1ff41152007-09-19 18:00:03 +000034 RegisterDeclsExprs(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) {
Ted Kremenek1ff41152007-09-19 18:00:03 +000037 if (!AD.isTracked(VD)) AD[VD] = AD.NumDecls++;
Ted Kremenek3e039752007-09-17 17:14:52 +000038 }
39
Ted Kremenek1ff41152007-09-19 18:00:03 +000040 void BlockStmt_VisitExpr(Expr* E) {
41 if (!AD.isTracked(E)) AD[E] = AD.NumBlockExprs++;
Ted Kremenek0a03ce62007-09-17 20:49:30 +000042 }
Ted Kremenek7f49f502007-09-14 22:49:21 +000043};
44
45} // end anonymous namespace
46
47void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek1ff41152007-09-19 18:00:03 +000048 RegisterDeclsExprs R(this->getAnalysisData());
Ted Kremenek68447a62007-09-18 20:59:00 +000049 cfg.VisitBlockStmts(R);
Ted Kremenek7f49f502007-09-14 22:49:21 +000050}
51
Ted Kremenek3871d8e2007-09-17 19:59:27 +000052//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000053// Transfer functions.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000054//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000055
56namespace {
Ted Kremenek334b30a2007-09-17 18:31:23 +000057
Ted Kremenek7f49f502007-09-14 22:49:21 +000058class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
59 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000060 UninitializedValues::AnalysisDataTy& AD;
Ted Kremeneke6148f32007-09-17 21:59:08 +000061 bool InitWithAssigns;
Ted Kremenek7f49f502007-09-14 22:49:21 +000062public:
Ted Kremeneke6148f32007-09-17 21:59:08 +000063 TransferFuncs(UninitializedValues::AnalysisDataTy& ad,
64 bool init_with_assigns=true) :
65 AD(ad), InitWithAssigns(init_with_assigns) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000066 V.resetValues(AD);
Ted Kremenek7f49f502007-09-14 22:49:21 +000067 }
68
69 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek3e039752007-09-17 17:14:52 +000070
Ted Kremenek334b30a2007-09-17 18:31:23 +000071 bool VisitDeclRefExpr(DeclRefExpr* DR);
72 bool VisitBinaryOperator(BinaryOperator* B);
73 bool VisitUnaryOperator(UnaryOperator* U);
74 bool VisitStmt(Stmt* S);
75 bool VisitCallExpr(CallExpr* C);
76 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek3871d8e2007-09-17 19:59:27 +000077 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek334b30a2007-09-17 18:31:23 +000078
Ted Kremenekf92ba512007-09-18 21:43:18 +000079 BlockVarDecl* FindBlockVarDecl(Stmt* S);
80
Ted Kremenek334b30a2007-09-17 18:31:23 +000081 static inline bool Initialized() { return true; }
Ted Kremenek3871d8e2007-09-17 19:59:27 +000082 static inline bool Uninitialized() { return false; }
Ted Kremenek7f49f502007-09-14 22:49:21 +000083};
Ted Kremenek334b30a2007-09-17 18:31:23 +000084
85
86bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000087 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenekf92ba512007-09-18 21:43:18 +000088 if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
Ted Kremenek3871d8e2007-09-17 19:59:27 +000089
Ted Kremenekf92ba512007-09-18 21:43:18 +000090 return V.getBitRef(VD,AD);
Ted Kremenek334b30a2007-09-17 18:31:23 +000091 }
Ted Kremenekf92ba512007-09-18 21:43:18 +000092 else return Initialized();
93}
94
95BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) {
96 for (;;) {
97 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
98 S = P->getSubExpr();
99 continue;
100 }
101 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
102 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl()))
103 return VD;
104
105 return NULL;
106 }
Ted Kremenek334b30a2007-09-17 18:31:23 +0000107}
108
109bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremeneke6148f32007-09-17 21:59:08 +0000110
Ted Kremenekf92ba512007-09-18 21:43:18 +0000111 if (CFG::hasImplicitControlFlow(B))
112 return V.getBitRef(B,AD);
113
114 if (B->isAssignmentOp())
115 // Get the Decl for the LHS (if any).
116 if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS()))
117 if(InitWithAssigns) {
118 // Pseudo-hack to prevent cascade of warnings. If the RHS uses
119 // an uninitialized value, then we are already going to flag a warning
120 // for the RHS, or for the root "source" of the unintialized values.
121 // Thus, propogating uninitialized doesn't make sense, since we are
122 // just adding extra messages that don't
123 // contribute to diagnosing the bug. In InitWithAssigns mode
124 // we unconditionally set the assigned variable to Initialized to
125 // prevent Uninitialized propogation.
126 return V.getBitRef(VD,AD) = Initialized();
127 }
128 else return V.getBitRef(VD,AD) = Visit(B->getRHS());
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000129
Ted Kremenek334b30a2007-09-17 18:31:23 +0000130 return VisitStmt(B);
131}
132
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000133bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
134 bool x = Initialized();
135
136 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000137 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D))
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000138 if (Stmt* I = VD->getInit()) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000139 x = V.getBitRef(cast<Expr>(I),AD);
140 V.getBitRef(VD,AD) = x;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000141 }
142
143 return x;
144}
145
Ted Kremenek334b30a2007-09-17 18:31:23 +0000146bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremeneka1d35862007-09-18 21:47:41 +0000147 VisitChildren(C);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000148 return Initialized();
149}
150
151bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
152 switch (U->getOpcode()) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000153 case UnaryOperator::AddrOf:
154 // For "&x", treat "x" as now being initialized.
155 if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr()))
156 V.getBitRef(VD,AD) = Initialized();
157 else
158 return Visit(U->getSubExpr());
Ted Kremenek334b30a2007-09-17 18:31:23 +0000159
160 default:
161 return Visit(U->getSubExpr());
162 }
163}
164
165bool TransferFuncs::VisitStmt(Stmt* S) {
166 bool x = Initialized();
167
168 // We don't stop at the first subexpression that is Uninitialized because
169 // evaluating some subexpressions may result in propogating "Uninitialized"
170 // or "Initialized" to variables referenced in the other subexpressions.
171 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenekf92ba512007-09-18 21:43:18 +0000172 if (Visit(*I) == Uninitialized()) x = Uninitialized();
Ted Kremenek334b30a2007-09-17 18:31:23 +0000173
174 return x;
175}
176
177bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000178 assert (AD.isTracked(E));
179 return V.getBitRef(E,AD) = Visit(E);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000180}
181
Ted Kremenek7f49f502007-09-14 22:49:21 +0000182} // end anonymous namespace
183
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000184//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000185// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000186//
187// In our transfer functions we take the approach that any
188// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
189//
190// Merges take the opposite approach.
191//
Ted Kremenek68447a62007-09-18 20:59:00 +0000192// In the merge of dataflow values we prefer unsoundness, and
Ted Kremenek334b30a2007-09-17 18:31:23 +0000193// prefer false negatives to false positives. At merges, if a value for a
194// tracked Decl is EVER initialized in any of the predecessors we treat it as
195// initialized at the confluence point.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000196//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000197
198namespace {
199struct Merge {
200 void operator()(UninitializedValues::ValTy& Dst,
201 UninitializedValues::ValTy& Src) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000202 assert (Src.sizesEqual(Dst) && "BV sizes do not match.");
Ted Kremenek68447a62007-09-18 20:59:00 +0000203 Dst.DeclBV |= Src.DeclBV;
204 Dst.ExprBV |= Src.ExprBV;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000205 }
206};
207} // end anonymous namespace
208
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000209//===----------------------------------------------------------------------===//
210// Unitialized values checker. Scan an AST and flag variable uses
211//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000212
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000213UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
214
215namespace {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000216class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
217 ASTContext &Ctx;
218 Diagnostic &Diags;
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000219 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000220
221public:
222 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
223 : Ctx(ctx), Diags(diags) {}
224
225 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
226 UninitializedValues::AnalysisDataTy& AD,
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000227 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000228
Ted Kremenekf92ba512007-09-18 21:43:18 +0000229 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
230
231 if (V.getBitRef(VD,AD) == TransferFuncs::Uninitialized())
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000232 if (AlreadyWarned.insert(VD))
233 Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
234 }
235};
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000236} // end anonymous namespace
237
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000238namespace clang {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000239void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000240
241 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000242
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000243 // Compute the unitialized values information.
244 UninitializedValues U;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000245 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000246 S.runOnCFG(cfg);
247
248 // Scan for DeclRefExprs that use uninitialized values.
249 UninitializedValuesChecker Observer(Ctx,Diags);
250 U.getAnalysisData().Observer = &Observer;
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000251 S.runOnAllBlocks(cfg);
Ted Kremenek7f49f502007-09-14 22:49:21 +0000252}
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000253} // end namespace clang