1 //= UninitializedValues.h - Finding uses of uninitialized values -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines APIs for invoking and reported uninitialized values
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_UNINIT_VALS_H
16 #define LLVM_CLANG_UNINIT_VALS_H
18 #include "llvm/ADT/SmallVector.h"
22 class AnalysisDeclContext;
28 /// A use of a variable, which might be uninitialized.
32 const Stmt *Terminator;
37 /// The expression which uses this variable.
40 /// Does this use always see an uninitialized value?
43 /// This use is always uninitialized if it occurs after any of these branches
45 llvm::SmallVector<Branch, 2> UninitBranches;
48 UninitUse(const Expr *User, bool AlwaysUninit) :
49 User(User), AlwaysUninit(AlwaysUninit) {}
51 void addUninitBranch(Branch B) {
52 UninitBranches.push_back(B);
55 /// Get the expression containing the uninitialized use.
56 const Expr *getUser() const { return User; }
58 /// The kind of uninitialized use.
60 /// The use might be uninitialized.
62 /// The use is uninitialized whenever a certain branch is taken.
64 /// The use is always uninitialized.
68 /// Get the kind of uninitialized use.
69 Kind getKind() const {
70 return AlwaysUninit ? Always :
71 !branch_empty() ? Sometimes : Maybe;
74 typedef llvm::SmallVectorImpl<Branch>::const_iterator branch_iterator;
75 /// Branches which inevitably result in the variable being used uninitialized.
76 branch_iterator branch_begin() const { return UninitBranches.begin(); }
77 branch_iterator branch_end() const { return UninitBranches.end(); }
78 bool branch_empty() const { return UninitBranches.empty(); }
81 class UninitVariablesHandler {
83 UninitVariablesHandler() {}
84 virtual ~UninitVariablesHandler();
86 /// Called when the uninitialized variable is used at the given expression.
87 virtual void handleUseOfUninitVariable(const VarDecl *vd,
88 const UninitUse &use) {}
90 /// Called when the uninitialized variable analysis detects the
91 /// idiom 'int x = x'. All other uses of 'x' within the initializer
92 /// are handled by handleUseOfUninitVariable.
93 virtual void handleSelfInit(const VarDecl *vd) {}
96 struct UninitVariablesAnalysisStats {
97 unsigned NumVariablesAnalyzed;
98 unsigned NumBlockVisits;
101 void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg,
102 AnalysisDeclContext &ac,
103 UninitVariablesHandler &handler,
104 UninitVariablesAnalysisStats &stats);