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 /// Is this use uninitialized whenever the function is called?
44 /// Is this use uninitialized whenever the variable declaration is reached?
47 /// Does this use always see an uninitialized value?
50 /// This use is always uninitialized if it occurs after any of these branches
52 SmallVector<Branch, 2> UninitBranches;
55 UninitUse(const Expr *User, bool AlwaysUninit)
56 : User(User), UninitAfterCall(false), UninitAfterDecl(false),
57 AlwaysUninit(AlwaysUninit) {}
59 void addUninitBranch(Branch B) {
60 UninitBranches.push_back(B);
63 void setUninitAfterCall() { UninitAfterCall = true; }
64 void setUninitAfterDecl() { UninitAfterDecl = true; }
66 /// Get the expression containing the uninitialized use.
67 const Expr *getUser() const { return User; }
69 /// The kind of uninitialized use.
71 /// The use might be uninitialized.
73 /// The use is uninitialized whenever a certain branch is taken.
75 /// The use is uninitialized the first time it is reached after we reach
76 /// the variable's declaration.
78 /// The use is uninitialized the first time it is reached after the function
81 /// The use is always uninitialized.
85 /// Get the kind of uninitialized use.
86 Kind getKind() const {
87 return AlwaysUninit ? Always :
88 UninitAfterCall ? AfterCall :
89 UninitAfterDecl ? AfterDecl :
90 !branch_empty() ? Sometimes : Maybe;
93 typedef SmallVectorImpl<Branch>::const_iterator branch_iterator;
94 /// Branches which inevitably result in the variable being used uninitialized.
95 branch_iterator branch_begin() const { return UninitBranches.begin(); }
96 branch_iterator branch_end() const { return UninitBranches.end(); }
97 bool branch_empty() const { return UninitBranches.empty(); }
100 class UninitVariablesHandler {
102 UninitVariablesHandler() {}
103 virtual ~UninitVariablesHandler();
105 /// Called when the uninitialized variable is used at the given expression.
106 virtual void handleUseOfUninitVariable(const VarDecl *vd,
107 const UninitUse &use) {}
109 /// Called when the uninitialized variable analysis detects the
110 /// idiom 'int x = x'. All other uses of 'x' within the initializer
111 /// are handled by handleUseOfUninitVariable.
112 virtual void handleSelfInit(const VarDecl *vd) {}
115 struct UninitVariablesAnalysisStats {
116 unsigned NumVariablesAnalyzed;
117 unsigned NumBlockVisits;
120 void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg,
121 AnalysisDeclContext &ac,
122 UninitVariablesHandler &handler,
123 UninitVariablesAnalysisStats &stats);