blob: dd741c1af32ae72a2dac4e7449853a1d903a9c79 [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"
15#include "clang/Analysis/CFGVarDeclVisitor.h"
16#include "clang/Analysis/CFGStmtVisitor.h"
17#include "DataflowSolver.h"
18
19using namespace clang;
20
21//===--------------------------------------------------------------------===//
22// Dataflow initialization logic.
23//===--------------------------------------------------------------------===//
24
25namespace {
26
Ted Kremenek3e039752007-09-17 17:14:52 +000027class RegisterDeclsAndExprs : public CFGVarDeclVisitor<RegisterDeclsAndExprs> {
28 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek7f49f502007-09-14 22:49:21 +000029public:
Ted Kremenek3e039752007-09-17 17:14:52 +000030 RegisterDeclsAndExprs(const CFG& cfg, UninitializedValues::AnalysisDataTy& ad)
31 : CFGVarDeclVisitor<RegisterDeclsAndExprs>(cfg), AD(ad)
32 {}
Ted Kremenek7f49f502007-09-14 22:49:21 +000033
34 void VisitVarDecl(VarDecl* D) {
Ted Kremenek3e039752007-09-17 17:14:52 +000035 if (AD.VMap.find(D) == AD.VMap.end())
36 AD.VMap[D] = AD.Counter++;
37 }
38
39 void BlockStmt_VisitExpr(Expr* E) {
40 if (AD.EMap.find(E) == AD.EMap.end())
41 AD.EMap[E] = AD.Counter++;
42 }
Ted Kremenek7f49f502007-09-14 22:49:21 +000043};
44
45} // end anonymous namespace
46
47void UninitializedValues::InitializeValues(const CFG& cfg) {
Ted Kremenek3e039752007-09-17 17:14:52 +000048 RegisterDeclsAndExprs R(cfg,this->getAnalysisData());
49 R.VisitAllDecls();
50 getBlockDataMap()[ &cfg.getEntry() ].resize( getAnalysisData().Counter );
Ted Kremenek7f49f502007-09-14 22:49:21 +000051}
52
53//===--------------------------------------------------------------------===//
54// Transfer functions.
55//===--------------------------------------------------------------------===//
56
57namespace {
58class TransferFuncs : public CFGStmtVisitor<TransferFuncs,bool> {
59 UninitializedValues::ValTy V;
Ted Kremenek3e039752007-09-17 17:14:52 +000060 UninitializedValues::AnalysisDataTy& AD;
Ted Kremenek7f49f502007-09-14 22:49:21 +000061public:
Ted Kremenek3e039752007-09-17 17:14:52 +000062 TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {
63 V.resize(AD.Counter);
Ted Kremenek7f49f502007-09-14 22:49:21 +000064 }
65
66 UninitializedValues::ValTy& getVal() { return V; }
Ted Kremenek3e039752007-09-17 17:14:52 +000067
68// bool VisitDeclRefExpr(DeclRefExpr* DR);
69// bool VisitBinaryOperator(BinaryOperator* B);
70// bool VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek7f49f502007-09-14 22:49:21 +000071};
72} // end anonymous namespace
73
74//===--------------------------------------------------------------------===//
75// Merge operator.
76//===--------------------------------------------------------------------===//
77
78namespace {
79struct Merge {
80 void operator()(UninitializedValues::ValTy& Dst,
81 UninitializedValues::ValTy& Src) {
82 assert (Dst.size() == Src.size() && "Bitvector sizes do not match.");
83 Src |= Dst;
84 }
85};
86} // end anonymous namespace
87
88//===--------------------------------------------------------------------===//
89// Observer to flag warnings for uses of uninitialized variables.
90//===--------------------------------------------------------------------===//
91
92
93
94
95//===--------------------------------------------------------------------===//
96// External interface (driver logic).
97//===--------------------------------------------------------------------===//
98
99void UninitializedValues::CheckUninitializedValues(const CFG& cfg) {
100
101 typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
102
103 UninitializedValues U;
104
105 { // Compute the unitialized values information.
106 Solver S(U);
107 S.runOnCFG(cfg);
108 }
109
110// WarnObserver O;
111 Solver S(U);
112
113 for (CFG::const_iterator I=cfg.begin(), E=cfg.end(); I!=E; ++I)
114 S.runOnBlock(&*I);
115}