clang 20.0.0git
UninitializedValues.h
Go to the documentation of this file.
1//=- UninitializedValues.h - Finding uses of uninitialized values -*- C++ -*-=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines APIs for invoking and reported uninitialized values
10// warnings.
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
20namespace clang {
21
22class AnalysisDeclContext;
23class CFG;
24class DeclContext;
25class Expr;
26class Stmt;
27class VarDecl;
28
29/// A use of a variable, which might be uninitialized.
30class UninitUse {
31public:
32 struct Branch {
34 unsigned Output;
35 };
36
37private:
38 /// The expression which uses this variable.
39 const Expr *User;
40
41 /// Is this use uninitialized whenever the function is called?
42 bool UninitAfterCall = false;
43
44 /// Is this use uninitialized whenever the variable declaration is reached?
45 bool UninitAfterDecl = false;
46
47 /// Does this use always see an uninitialized value?
48 bool AlwaysUninit;
49
50 /// This use is always uninitialized if it occurs after any of these branches
51 /// is taken.
52 SmallVector<Branch, 2> UninitBranches;
53
54public:
55 UninitUse(const Expr *User, bool AlwaysUninit)
56 : User(User), AlwaysUninit(AlwaysUninit) {}
57
59 UninitBranches.push_back(B);
60 }
61
62 void setUninitAfterCall() { UninitAfterCall = true; }
63 void setUninitAfterDecl() { UninitAfterDecl = true; }
64
65 /// Get the expression containing the uninitialized use.
66 const Expr *getUser() const { return User; }
67
68 /// The kind of uninitialized use.
69 enum Kind {
70 /// The use might be uninitialized.
72
73 /// The use is uninitialized whenever a certain branch is taken.
75
76 /// The use is uninitialized the first time it is reached after we reach
77 /// the variable's declaration.
79
80 /// The use is uninitialized the first time it is reached after the function
81 /// is called.
83
84 /// The use is always uninitialized.
85 Always
86 };
87
88 /// Get the kind of uninitialized use.
89 Kind getKind() const {
90 return AlwaysUninit ? Always :
91 UninitAfterCall ? AfterCall :
92 UninitAfterDecl ? AfterDecl :
94 }
95
97
98 /// Branches which inevitably result in the variable being used uninitialized.
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
105public:
108
109 /// Called when the uninitialized variable is used at the given expression.
110 virtual void handleUseOfUninitVariable(const VarDecl *vd,
111 const UninitUse &use) {}
112
113 /// Called when the uninitialized variable is used as const refernce argument.
115 const UninitUse &use) {}
116
117 /// Called when the uninitialized variable analysis detects the
118 /// idiom 'int x = x'. All other uses of 'x' within the initializer
119 /// are handled by handleUseOfUninitVariable.
120 virtual void handleSelfInit(const VarDecl *vd) {}
121};
122
126};
127
128void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg,
130 UninitVariablesHandler &handler,
132
133} // namespace clang
134
135#endif // LLVM_CLANG_ANALYSIS_ANALYSES_UNINITIALIZEDVALUES_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1214
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1436
This represents one expression.
Definition: Expr.h:110
Stmt - This represents one statement.
Definition: Stmt.h:84
A use of a variable, which might be uninitialized.
const Expr * getUser() const
Get the expression containing the uninitialized use.
UninitUse(const Expr *User, bool AlwaysUninit)
SmallVectorImpl< Branch >::const_iterator branch_iterator
bool branch_empty() const
branch_iterator branch_end() const
void addUninitBranch(Branch B)
branch_iterator branch_begin() const
Branches which inevitably result in the variable being used uninitialized.
Kind
The kind of uninitialized use.
@ Always
The use is always uninitialized.
@ AfterDecl
The use is uninitialized the first time it is reached after we reach the variable's declaration.
@ Maybe
The use might be uninitialized.
@ AfterCall
The use is uninitialized the first time it is reached after the function is called.
@ Sometimes
The use is uninitialized whenever a certain branch is taken.
Kind getKind() const
Get the kind of uninitialized use.
virtual void handleUseOfUninitVariable(const VarDecl *vd, const UninitUse &use)
Called when the uninitialized variable is used at the given expression.
virtual void handleSelfInit(const VarDecl *vd)
Called when the uninitialized variable analysis detects the idiom 'int x = x'.
virtual void handleConstRefUseOfUninitVariable(const VarDecl *vd, const UninitUse &use)
Called when the uninitialized variable is used as const refernce argument.
Represents a variable declaration or definition.
Definition: Decl.h:879
The JSON file list parser is used to communicate input to InstallAPI.
void runUninitializedVariablesAnalysis(const DeclContext &dc, const CFG &cfg, AnalysisDeclContext &ac, UninitVariablesHandler &handler, UninitVariablesAnalysisStats &stats)