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 "clang/AST/Stmt.h"
19 #include "llvm/ADT/SmallVector.h"
23 class AnalysisDeclContext;
29 /// A use of a variable, which might be uninitialized.
33 const Stmt *Terminator;
38 /// The expression which uses this variable.
41 /// Does this use always see an uninitialized value?
44 /// This use is always uninitialized if it occurs after any of these branches
46 SmallVector<Branch, 2> UninitBranches;
49 UninitUse(const Expr *User, bool AlwaysUninit) :
50 User(User), AlwaysUninit(AlwaysUninit) {}
52 void addUninitBranch(Branch B) {
53 UninitBranches.push_back(B);
56 /// Get the expression containing the uninitialized use.
57 const Expr *getUser() const { return User; }
59 /// The kind of uninitialized use.
61 /// The use might be uninitialized.
63 /// The use is uninitialized whenever a certain branch is taken.
65 /// The use is always uninitialized.
69 /// Get the kind of uninitialized use.
70 Kind getKind() const {
71 return AlwaysUninit ? Always :
72 !branch_empty() ? Sometimes : Maybe;
75 typedef SmallVectorImpl<Branch>::const_iterator branch_iterator;
76 /// Branches which inevitably result in the variable being used uninitialized.
77 branch_iterator branch_begin() const { return UninitBranches.begin(); }
78 branch_iterator branch_end() const { return UninitBranches.end(); }
79 bool branch_empty() const { return UninitBranches.empty(); }
82 class UninitVariablesHandler {
84 UninitVariablesHandler() {}
85 virtual ~UninitVariablesHandler();
87 /// Called when the uninitialized variable is used at the given expression.
88 virtual void handleUseOfUninitVariable(const VarDecl *vd,
89 const UninitUse &use) {}
91 /// Called when the uninitialized variable analysis detects the
92 /// idiom 'int x = x'. All other uses of 'x' within the initializer
93 /// are handled by handleUseOfUninitVariable.
94 virtual void handleSelfInit(const VarDecl *vd) {}
97 struct UninitVariablesAnalysisStats {
98 unsigned NumVariablesAnalyzed;
99 unsigned NumBlockVisits;
102 void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg,
103 AnalysisDeclContext &ac,
104 UninitVariablesHandler &handler,
105 UninitVariablesAnalysisStats &stats);