| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_UNINITIALIZEDVALUES_H |
| 15 | #define LLVM_CLANG_ANALYSIS_ANALYSES_UNINITIALIZEDVALUES_H |
| 16 | |
| 17 | #include "clang/Basic/LLVM.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | |
| 22 | class AnalysisDeclContext; |
| 23 | class CFG; |
| 24 | class DeclContext; |
| 25 | class Expr; |
| 26 | class Stmt; |
| 27 | class VarDecl; |
| 28 | |
| 29 | |
| 30 | class UninitUse { |
| 31 | public: |
| 32 | struct Branch { |
| 33 | const Stmt *Terminator; |
| 34 | unsigned Output; |
| 35 | }; |
| 36 | |
| 37 | private: |
| 38 | |
| 39 | const Expr *User; |
| 40 | |
| 41 | |
| 42 | bool UninitAfterCall = false; |
| 43 | |
| 44 | |
| 45 | bool UninitAfterDecl = false; |
| 46 | |
| 47 | |
| 48 | bool AlwaysUninit; |
| 49 | |
| 50 | |
| 51 | |
| 52 | SmallVector<Branch, 2> UninitBranches; |
| 53 | |
| 54 | public: |
| 55 | UninitUse(const Expr *User, bool AlwaysUninit) |
| 56 | : User(User), AlwaysUninit(AlwaysUninit) {} |
| 57 | |
| 58 | void addUninitBranch(Branch B) { |
| 59 | UninitBranches.push_back(B); |
| 60 | } |
| 61 | |
| 62 | void setUninitAfterCall() { UninitAfterCall = true; } |
| 63 | void setUninitAfterDecl() { UninitAfterDecl = true; } |
| 64 | |
| 65 | |
| 66 | const Expr *getUser() const { return User; } |
| 67 | |
| 68 | |
| 69 | enum Kind { |
| 70 | |
| 71 | Maybe, |
| 72 | |
| 73 | |
| 74 | Sometimes, |
| 75 | |
| 76 | |
| 77 | |
| 78 | AfterDecl, |
| 79 | |
| 80 | |
| 81 | |
| 82 | AfterCall, |
| 83 | |
| 84 | |
| 85 | Always |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | Kind getKind() const { |
| 90 | return AlwaysUninit ? Always : |
| 91 | UninitAfterCall ? AfterCall : |
| 92 | UninitAfterDecl ? AfterDecl : |
| 93 | !branch_empty() ? Sometimes : Maybe; |
| 94 | } |
| 95 | |
| 96 | using branch_iterator = SmallVectorImpl<Branch>::const_iterator; |
| 97 | |
| 98 | |
| 99 | branch_iterator branch_begin() const { return UninitBranches.begin(); } |
| 100 | branch_iterator branch_end() const { return UninitBranches.end(); } |
| 101 | bool branch_empty() const { return UninitBranches.empty(); } |
| 102 | }; |
| 103 | |
| 104 | class UninitVariablesHandler { |
| 105 | public: |
| 106 | UninitVariablesHandler() = default; |
| 107 | virtual ~UninitVariablesHandler(); |
| 108 | |
| 109 | |
| 110 | virtual void handleUseOfUninitVariable(const VarDecl *vd, |
| 111 | const UninitUse &use) {} |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | virtual void handleSelfInit(const VarDecl *vd) {} |
| 117 | }; |
| 118 | |
| 119 | struct UninitVariablesAnalysisStats { |
| 120 | unsigned NumVariablesAnalyzed; |
| 121 | unsigned NumBlockVisits; |
| 122 | }; |
| 123 | |
| 124 | void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg, |
| 125 | AnalysisDeclContext &ac, |
| 126 | UninitVariablesHandler &handler, |
| 127 | UninitVariablesAnalysisStats &stats); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | #endif |
| 132 | |