clang 8.0.0

UninitializedValues.h
Go to the documentation of this file.
1 //=- UninitializedValues.h - Finding uses of uninitialized values -*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines APIs for invoking and reported uninitialized values
11 // warnings.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_UNINITIALIZEDVALUES_H
16 #define LLVM_CLANG_ANALYSIS_ANALYSES_UNINITIALIZEDVALUES_H
17
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/SmallVector.h"
20
21 namespace clang {
22
23 class AnalysisDeclContext;
24 class CFG;
25 class DeclContext;
26 class Expr;
27 class Stmt;
28 class VarDecl;
29
30 /// A use of a variable, which might be uninitialized.
31 class UninitUse {
32 public:
33 struct Branch {
34 const Stmt *Terminator;
35 unsigned Output;
36 };
37
38 private:
39 /// The expression which uses this variable.
40 const Expr *User;
41
42 /// Is this use uninitialized whenever the function is called?
43 bool UninitAfterCall = false;
44
45 /// Is this use uninitialized whenever the variable declaration is reached?
46 bool UninitAfterDecl = false;
47
48 /// Does this use always see an uninitialized value?
49 bool AlwaysUninit;
50
51 /// This use is always uninitialized if it occurs after any of these branches
52 /// is taken.
53 SmallVector<Branch, 2> UninitBranches;
54
55 public:
56 UninitUse(const Expr *User, bool AlwaysUninit)
57 : User(User), AlwaysUninit(AlwaysUninit) {}
58
59 void addUninitBranch(Branch B) {
60 UninitBranches.push_back(B);
61 }
62
63 void setUninitAfterCall() { UninitAfterCall = true; }
64 void setUninitAfterDecl() { UninitAfterDecl = true; }
65
66 /// Get the expression containing the uninitialized use.
67 const Expr *getUser() const { return User; }
68
69 /// The kind of uninitialized use.
70 enum Kind {
71 /// The use might be uninitialized.
72 Maybe,
73
74 /// The use is uninitialized whenever a certain branch is taken.
75 Sometimes,
76
77 /// The use is uninitialized the first time it is reached after we reach
78 /// the variable's declaration.
79 AfterDecl,
80
81 /// The use is uninitialized the first time it is reached after the function
82 /// is called.
83 AfterCall,
84
85 /// The use is always uninitialized.
86 Always
87 };
88
89 /// Get the kind of uninitialized use.
90 Kind getKind() const {
91 return AlwaysUninit ? Always :
92 UninitAfterCall ? AfterCall :
93 UninitAfterDecl ? AfterDecl :
94 !branch_empty() ? Sometimes : Maybe;
95 }
96
97 using branch_iterator = SmallVectorImpl<Branch>::const_iterator;
98
99 /// Branches which inevitably result in the variable being used uninitialized.
100 branch_iterator branch_begin() const { return UninitBranches.begin(); }
101 branch_iterator branch_end() const { return UninitBranches.end(); }
102 bool branch_empty() const { return UninitBranches.empty(); }
103 };
104
105 class UninitVariablesHandler {
106 public:
107 UninitVariablesHandler() = default;
108 virtual ~UninitVariablesHandler();
109
110 /// Called when the uninitialized variable is used at the given expression.
111 virtual void handleUseOfUninitVariable(const VarDecl *vd,
112 const UninitUse &use) {}
113
114 /// Called when the uninitialized variable analysis detects the
115 /// idiom 'int x = x'. All other uses of 'x' within the initializer
116 /// are handled by handleUseOfUninitVariable.
117 virtual void handleSelfInit(const VarDecl *vd) {}
118 };
119
120 struct UninitVariablesAnalysisStats {
121 unsigned NumVariablesAnalyzed;
122 unsigned NumBlockVisits;
123 };
124
125 void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg,
126 AnalysisDeclContext &ac,
127 UninitVariablesHandler &handler,
128 UninitVariablesAnalysisStats &stats);
129
130 } // namespace clang
131
132 #endif // LLVM_CLANG_ANALYSIS_ANALYSES_UNINITIALIZEDVALUES_H
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::UninitUse::branch_empty
bool branch_empty() const
Definition: UninitializedValues.h:102
clang::UninitUse::Sometimes
The use is uninitialized whenever a certain branch is taken.
Definition: UninitializedValues.h:75
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:813
clang::UninitUse::Maybe
The use might be uninitialized.
Definition: UninitializedValues.h:72
clang::UninitUse::branch_end
branch_iterator branch_end() const
Definition: UninitializedValues.h:101
clang::UninitUse::setUninitAfterCall
void setUninitAfterCall()
Definition: UninitializedValues.h:63
clang::UninitUse::getKind
Kind getKind() const
Get the kind of uninitialized use.
Definition: UninitializedValues.h:90
clang::UninitUse::branch_begin
branch_iterator branch_begin() const
Branches which inevitably result in the variable being used uninitialized.
Definition: UninitializedValues.h:100
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function or method under analysis.
Definition: AnalysisDeclContext.h:70
llvm::SmallVectorImpl
Definition: LLVM.h:37
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::UninitUse::Kind
Kind
The kind of uninitialized use.
Definition: UninitializedValues.h:70
clang::UninitUse::Branch::Output
unsigned Output
Definition: UninitializedValues.h:35
clang::UninitUse::Branch::Terminator
const Stmt * Terminator
Definition: UninitializedValues.h:34
clang::Expr
This represents one expression.
Definition: Expr.h:106
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:1003
clang::UninitVariablesHandler::handleSelfInit
virtual void handleSelfInit(const VarDecl *vd)
Called when the uninitialized variable analysis detects the idiom &#39;int x = x&#39;.
Definition: UninitializedValues.h:117
clang::runUninitializedVariablesAnalysis
void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg, AnalysisDeclContext &ac, UninitVariablesHandler &handler, UninitVariablesAnalysisStats &stats)
Definition: UninitializedValues.cpp:885
clang::UninitUse::UninitUse
UninitUse(const Expr *User, bool AlwaysUninit)
Definition: UninitializedValues.h:56
clang::UninitUse::Branch
Definition: UninitializedValues.h:33
clang::UninitUse
A use of a variable, which might be uninitialized.
Definition: UninitializedValues.h:31
clang::UninitVariablesAnalysisStats
Definition: UninitializedValues.h:120
clang::UninitVariablesHandler::handleUseOfUninitVariable
virtual void handleUseOfUninitVariable(const VarDecl *vd, const UninitUse &use)
Called when the uninitialized variable is used at the given expression.
Definition: UninitializedValues.h:111
llvm::SmallVector
Definition: LLVM.h:36
clang::UninitVariablesAnalysisStats::NumBlockVisits
unsigned NumBlockVisits
Definition: UninitializedValues.h:122
clang::UninitUse::AfterDecl
The use is uninitialized the first time it is reached after we reach the variable&#39;s declaration...
Definition: UninitializedValues.h:79
clang::UninitUse::branch_iterator
SmallVectorImpl< Branch >::const_iterator branch_iterator
Definition: UninitializedValues.h:97
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
clang::UninitUse::setUninitAfterDecl
void setUninitAfterDecl()
Definition: UninitializedValues.h:64
clang::UninitUse::getUser
const Expr * getUser() const
Get the expression containing the uninitialized use.
Definition: UninitializedValues.h:67
clang::UninitUse::Always
The use is always uninitialized.
Definition: UninitializedValues.h:86
clang::UninitUse::AfterCall
The use is uninitialized the first time it is reached after the function is called.
Definition: UninitializedValues.h:83
clang::UninitVariablesAnalysisStats::NumVariablesAnalyzed
unsigned NumVariablesAnalyzed
Definition: UninitializedValues.h:121
clang::UninitUse::addUninitBranch
void addUninitBranch(Branch B)
Definition: UninitializedValues.h:59
clang::UninitVariablesHandler
Definition: UninitializedValues.h:105