blob: 7598a0e168183b462c8816668d2372d7d8c18de2 [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 Kremenek26e47462007-09-20 21:42:55 +000015#include "clang/Analysis/Visitors/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 Kremenek10d80462007-09-25 21:00:24 +000019#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenek7f49f502007-09-14 22:49:21 +000020
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 Kremenekbfbb7fb2007-09-27 18:20:22 +000036 void VisitBlockVarDecl(BlockVarDecl* VD) { AD.Register(VD); }
37 void BlockStmt_VisitExpr(Expr* E) { AD.Register(E); }
Ted Kremenek7f49f502007-09-14 22:49:21 +000038};
39
40} // end anonymous namespace
41
42void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek1ff41152007-09-19 18:00:03 +000043 RegisterDeclsExprs R(this->getAnalysisData());
Ted Kremenek68447a62007-09-18 20:59:00 +000044 cfg.VisitBlockStmts(R);
Ted Kremenek7f49f502007-09-14 22:49:21 +000045}
46
Ted Kremenek3871d8e2007-09-17 19:59:27 +000047//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000048// Transfer functions.
Ted Kremenek3871d8e2007-09-17 19:59:27 +000049//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +000050
51namespace {
Ted Kremenek334b30a2007-09-17 18:31:23 +000052
Ted Kremenek7f49f502007-09-14 22:49:21 +000053class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
54 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000055 UninitializedValues::AnalysisDataTy& AD;
Ted Kremeneke6148f32007-09-17 21:59:08 +000056 bool InitWithAssigns;
Ted Kremenek7f49f502007-09-14 22:49:21 +000057public:
Ted Kremeneke6148f32007-09-17 21:59:08 +000058 TransferFuncs(UninitializedValues::AnalysisDataTy& ad,
59 bool init_with_assigns=true) :
60 AD(ad), InitWithAssigns(init_with_assigns) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000061 V.resetValues(AD);
Ted Kremenek7f49f502007-09-14 22:49:21 +000062 }
63
64 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek3e039752007-09-17 17:14:52 +000065
Ted Kremenek334b30a2007-09-17 18:31:23 +000066 bool VisitDeclRefExpr(DeclRefExpr* DR);
67 bool VisitBinaryOperator(BinaryOperator* B);
68 bool VisitUnaryOperator(UnaryOperator* U);
69 bool VisitStmt(Stmt* S);
70 bool VisitCallExpr(CallExpr* C);
71 bool BlockStmt_VisitExpr(Expr* E);
Ted Kremenek3871d8e2007-09-17 19:59:27 +000072 bool VisitDeclStmt(DeclStmt* D);
Ted Kremenek334b30a2007-09-17 18:31:23 +000073
Ted Kremenekf92ba512007-09-18 21:43:18 +000074 BlockVarDecl* FindBlockVarDecl(Stmt* S);
Ted Kremenek7f49f502007-09-14 22:49:21 +000075};
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +000076
77static const bool Initialized = true;
78static const bool Uninitialized = false;
Ted Kremenek334b30a2007-09-17 18:31:23 +000079
80
81bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek0a03ce62007-09-17 20:49:30 +000082 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl())) {
Ted Kremenekf92ba512007-09-18 21:43:18 +000083 if (AD.Observer) AD.Observer->ObserveDeclRefExpr(V,AD,DR,VD);
Ted Kremenek3871d8e2007-09-17 19:59:27 +000084
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +000085 return V(VD,AD);
Ted Kremenek334b30a2007-09-17 18:31:23 +000086 }
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +000087 else return Initialized;
Ted Kremenekf92ba512007-09-18 21:43:18 +000088}
89
90BlockVarDecl* TransferFuncs::FindBlockVarDecl(Stmt *S) {
91 for (;;) {
92 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
93 S = P->getSubExpr();
94 continue;
95 }
96 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
97 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(DR->getDecl()))
98 return VD;
99
100 return NULL;
101 }
Ted Kremenek334b30a2007-09-17 18:31:23 +0000102}
103
104bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremeneke6148f32007-09-17 21:59:08 +0000105
Ted Kremenekf92ba512007-09-18 21:43:18 +0000106 if (CFG::hasImplicitControlFlow(B))
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000107 return V(B,AD);
Ted Kremenekf92ba512007-09-18 21:43:18 +0000108
109 if (B->isAssignmentOp())
110 // Get the Decl for the LHS (if any).
111 if (BlockVarDecl* VD = FindBlockVarDecl(B->getLHS()))
112 if(InitWithAssigns) {
113 // Pseudo-hack to prevent cascade of warnings. If the RHS uses
114 // an uninitialized value, then we are already going to flag a warning
115 // for the RHS, or for the root "source" of the unintialized values.
116 // Thus, propogating uninitialized doesn't make sense, since we are
117 // just adding extra messages that don't
118 // contribute to diagnosing the bug. In InitWithAssigns mode
119 // we unconditionally set the assigned variable to Initialized to
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000120 // prevent Uninitialized propagation.
121 return V(VD,AD) = Initialized;
Ted Kremenekf92ba512007-09-18 21:43:18 +0000122 }
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000123 else return V(VD,AD) = Visit(B->getRHS());
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000124
Ted Kremenek334b30a2007-09-17 18:31:23 +0000125 return VisitStmt(B);
126}
127
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000128bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000129 bool x = Initialized;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000130
131 for (ScopedDecl* D = S->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000132 if (BlockVarDecl* VD = dyn_cast<BlockVarDecl>(D)) {
133 if (Stmt* I = VD->getInit())
134 x = InitWithAssigns ? Initialized : V(cast<Expr>(I),AD);
135 else
136 x = Uninitialized;
137
138 V(VD,AD) = x;
139 }
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000140
141 return x;
142}
143
Ted Kremenek334b30a2007-09-17 18:31:23 +0000144bool TransferFuncs::VisitCallExpr(CallExpr* C) {
Ted Kremeneka1d35862007-09-18 21:47:41 +0000145 VisitChildren(C);
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000146 return Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000147}
148
149bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
150 switch (U->getOpcode()) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000151 case UnaryOperator::AddrOf:
152 // For "&x", treat "x" as now being initialized.
153 if (BlockVarDecl* VD = FindBlockVarDecl(U->getSubExpr()))
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000154 V(VD,AD) = Initialized;
Ted Kremenekf92ba512007-09-18 21:43:18 +0000155 else
156 return Visit(U->getSubExpr());
Ted Kremenek334b30a2007-09-17 18:31:23 +0000157
158 default:
159 return Visit(U->getSubExpr());
160 }
161}
162
163bool TransferFuncs::VisitStmt(Stmt* S) {
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000164 bool x = Initialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000165
166 // We don't stop at the first subexpression that is Uninitialized because
167 // evaluating some subexpressions may result in propogating "Uninitialized"
168 // or "Initialized" to variables referenced in the other subexpressions.
169 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000170 if (Visit(*I) == Uninitialized) x = Uninitialized;
Ted Kremenek334b30a2007-09-17 18:31:23 +0000171
172 return x;
173}
174
175bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
Ted Kremenekf92ba512007-09-18 21:43:18 +0000176 assert (AD.isTracked(E));
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000177 return V(E,AD) = Visit(E);
Ted Kremenek334b30a2007-09-17 18:31:23 +0000178}
179
Ted Kremenek7f49f502007-09-14 22:49:21 +0000180} // end anonymous namespace
181
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000182//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000183// Merge operator.
Ted Kremenek334b30a2007-09-17 18:31:23 +0000184//
185// In our transfer functions we take the approach that any
186// combination of unintialized values, e.g. Unitialized + ___ = Unitialized.
187//
188// Merges take the opposite approach.
189//
Ted Kremenek68447a62007-09-18 20:59:00 +0000190// In the merge of dataflow values we prefer unsoundness, and
Ted Kremenek334b30a2007-09-17 18:31:23 +0000191// prefer false negatives to false positives. At merges, if a value for a
192// tracked Decl is EVER initialized in any of the predecessors we treat it as
193// initialized at the confluence point.
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000194//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000195
196namespace {
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000197 typedef ExprDeclBitVector_Types::Union Merge;
198 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
199}
Ted Kremenek7f49f502007-09-14 22:49:21 +0000200
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000201//===----------------------------------------------------------------------===//
202// Unitialized values checker. Scan an AST and flag variable uses
203//===----------------------------------------------------------------------===//
Ted Kremenek7f49f502007-09-14 22:49:21 +0000204
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000205UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
206
207namespace {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000208class UninitializedValuesChecker : public UninitializedValues::ObserverTy {
209 ASTContext &Ctx;
210 Diagnostic &Diags;
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000211 llvm::SmallPtrSet<BlockVarDecl*,10> AlreadyWarned;
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000212
213public:
214 UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
215 : Ctx(ctx), Diags(diags) {}
216
217 virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
218 UninitializedValues::AnalysisDataTy& AD,
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000219 DeclRefExpr* DR, BlockVarDecl* VD) {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000220
Ted Kremenekf92ba512007-09-18 21:43:18 +0000221 assert ( AD.isTracked(VD) && "Unknown VarDecl.");
222
Ted Kremenekbfbb7fb2007-09-27 18:20:22 +0000223 if (V(VD,AD) == Uninitialized)
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000224 if (AlreadyWarned.insert(VD))
225 Diags.Report(DR->getSourceRange().Begin(), diag::warn_uninit_val);
226 }
227};
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000228} // end anonymous namespace
229
Ted Kremenek0a03ce62007-09-17 20:49:30 +0000230namespace clang {
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000231void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7f49f502007-09-14 22:49:21 +0000232
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000233 // Compute the unitialized values information.
234 UninitializedValues U;
Ted Kremenek7f49f502007-09-14 22:49:21 +0000235 Solver S(U);
Ted Kremenek3871d8e2007-09-17 19:59:27 +0000236 S.runOnCFG(cfg);
237
238 // Scan for DeclRefExprs that use uninitialized values.
239 UninitializedValuesChecker Observer(Ctx,Diags);
240 U.getAnalysisData().Observer = &Observer;
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000241 S.runOnAllBlocks(cfg);
Ted Kremenek7f49f502007-09-14 22:49:21 +0000242}
Ted Kremenek3fa5e092007-09-18 21:08:21 +0000243} // end namespace clang