clang 22.0.0git
ASTOps.h
Go to the documentation of this file.
1//===-- ASTOps.h -------------------------------*- 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// Operations on AST nodes that are used in flow-sensitive analysis.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_ASTOPS_H
14#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_ASTOPS_H
15
16#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/Type.h"
22#include "llvm/ADT/DenseSet.h"
23#include "llvm/ADT/SetVector.h"
24
25namespace clang {
26namespace dataflow {
27
28/// Skip past nodes that the CFG does not emit. These nodes are invisible to
29/// flow-sensitive analysis, and should be ignored as they will effectively not
30/// exist.
31///
32/// * `ParenExpr` - The CFG takes the operator precedence into account, but
33/// otherwise omits the node afterwards.
34///
35/// * `ExprWithCleanups` - The CFG will generate the appropriate calls to
36/// destructors and then omit the node.
37///
38const Expr &ignoreCFGOmittedNodes(const Expr &E);
39const Stmt &ignoreCFGOmittedNodes(const Stmt &S);
40
41/// A set of `FieldDecl *`. Use `SmallSetVector` to guarantee deterministic
42/// iteration order.
44
45/// Returns the set of all fields in the type.
47
48/// Returns whether `Fields` and `FieldLocs` contain the same fields.
49bool containsSameFields(const FieldSet &Fields,
50 const RecordStorageLocation::FieldToLoc &FieldLocs);
51
52/// Helper class for initialization of a record with an `InitListExpr`.
53/// `InitListExpr::inits()` contains the initializers for both the base classes
54/// and the fields of the record; this helper class separates these out into two
55/// different lists. In addition, it deals with special cases associated with
56/// unions.
58public:
59 // `InitList` must have record type.
60 RecordInitListHelper(const InitListExpr *InitList);
61 RecordInitListHelper(const CXXParenListInitExpr *ParenInitList);
62
63 // Base classes with their associated initializer expressions.
67
68 // Fields with their associated initializer expressions.
70 return FieldInits;
71 }
72
73private:
74 RecordInitListHelper(QualType Ty, std::vector<const FieldDecl *> Fields,
75 ArrayRef<Expr *> Inits);
76
79
80 // We potentially synthesize an `ImplicitValueInitExpr` for unions. It's a
81 // member variable because we store a pointer to it in `FieldInits`.
82 std::optional<ImplicitValueInitExpr> ImplicitValueInitForUnion;
83};
84
85/// Specialization of `RecursiveASTVisitor` that visits those nodes that are
86/// relevant to the dataflow analysis; generally, these are the ones that also
87/// appear in the CFG.
88/// To start the traversal, call `TraverseStmt()` on the statement or body of
89/// the function to analyze. Don't call `TraverseDecl()` on the function itself;
90/// this won't work as `TraverseDecl()` contains code to avoid traversing nested
91/// functions.
93public:
98
99 bool TraverseDecl(Decl *D) override {
100 // Don't traverse nested record or function declarations.
101 // - We won't be analyzing code contained in these anyway
102 // - We don't model fields that are used only in these nested declaration,
103 // so trying to propagate a result object to initializers of such fields
104 // would cause an error.
105 if (isa_and_nonnull<RecordDecl>(D) || isa_and_nonnull<FunctionDecl>(D))
106 return true;
107
109 }
110
111 // Don't traverse expressions in unevaluated contexts, as we don't model
112 // fields that are only used in these.
113 // Note: The operand of the `noexcept` operator is an unevaluated operand, but
114 // nevertheless it appears in the Clang CFG, so we don't exclude it here.
116 bool TraverseQualifier) override {
117 return true;
118 }
120 bool TraverseQualifier) override {
121 return true;
122 }
124 if (TIE->isPotentiallyEvaluated())
125 return DynamicRecursiveASTVisitor::TraverseCXXTypeidExpr(TIE);
126 return true;
127 }
129 return true;
130 }
131
132 bool TraverseBindingDecl(BindingDecl *BD) override {
133 // `RecursiveASTVisitor` doesn't traverse holding variables for
134 // `BindingDecl`s by itself, so we need to tell it to.
135 if (VarDecl *HoldingVar = BD->getHoldingVar())
136 TraverseDecl(HoldingVar);
137 return DynamicRecursiveASTVisitor::TraverseBindingDecl(BD);
138 }
139};
140
141/// A collection of several types of declarations, all referenced from the same
142/// function.
144 /// Non-static member variables.
146 /// All variables with static storage duration, notably including static
147 /// member variables and static variables declared within a function.
148 llvm::DenseSet<const VarDecl *> Globals;
149 /// Local variables, not including parameters or static variables declared
150 /// within a function.
151 llvm::DenseSet<const VarDecl *> Locals;
152 /// Free functions and member functions which are referenced (but not
153 /// necessarily called).
154 llvm::DenseSet<const FunctionDecl *> Functions;
155 /// When analyzing a lambda's call operator, the set of all parameters (from
156 /// the surrounding function) that the lambda captures. Captured local
157 /// variables are already included in `Locals` above.
158 llvm::DenseSet<const ParmVarDecl *> LambdaCapturedParams;
159};
160
161/// Returns declarations that are declared in or referenced from `FD`.
163
164/// Returns declarations that are declared in or referenced from `S`.
166
167} // namespace dataflow
168} // namespace clang
169
170#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_ASTOPS_H
Defines the clang::Expr interface and subclasses for C++ expressions.
C Language Family Type Representation.
A binding in a decomposition declaration.
Definition DeclCXX.h:4185
VarDecl * getHoldingVar() const
Get the variable (if any) that holds the value of evaluating the binding.
Definition DeclCXX.cpp:3605
Represents a list-initialization with parenthesis.
Definition ExprCXX.h:5135
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition ExprCXX.h:848
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr....
Definition ExprCXX.cpp:134
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
virtual bool TraverseDecl(MaybeConst< Decl > *D)
Represents a function declaration or definition.
Definition Decl.h:1999
Describes an C or C++ initializer list.
Definition Expr.h:5233
A (possibly-)qualified type.
Definition TypeBase.h:937
Stmt - This represents one statement.
Definition Stmt.h:85
The base class of the type hierarchy.
Definition TypeBase.h:1833
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition Expr.h:2625
Represents a variable declaration or definition.
Definition Decl.h:925
bool TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *) override
Definition ASTOps.h:128
bool TraverseBindingDecl(BindingDecl *BD) override
Definition ASTOps.h:132
bool TraverseTypeOfExprTypeLoc(TypeOfExprTypeLoc, bool TraverseQualifier) override
Definition ASTOps.h:119
bool TraverseCXXTypeidExpr(CXXTypeidExpr *TIE) override
Definition ASTOps.h:123
bool TraverseDecltypeTypeLoc(DecltypeTypeLoc, bool TraverseQualifier) override
Definition ASTOps.h:115
bool TraverseDecl(Decl *D) override
Definition ASTOps.h:99
ArrayRef< std::pair< const FieldDecl *, Expr * > > field_inits() const
Definition ASTOps.h:69
RecordInitListHelper(const InitListExpr *InitList)
Definition ASTOps.cpp:118
ArrayRef< std::pair< const CXXBaseSpecifier *, Expr * > > base_inits() const
Definition ASTOps.h:64
llvm::DenseMap< const ValueDecl *, StorageLocation * > FieldToLoc
Dataflow Directional Tag Classes.
Definition AdornedCFG.h:29
ReferencedDecls getReferencedDecls(const FunctionDecl &FD)
Returns declarations that are declared in or referenced from FD.
Definition ASTOps.cpp:276
const Expr & ignoreCFGOmittedNodes(const Expr &E)
Skip past nodes that the CFG does not emit.
Definition ASTOps.cpp:35
FieldSet getObjectFields(QualType Type)
Returns the set of all fields in the type.
Definition ASTOps.cpp:74
bool containsSameFields(const FieldSet &Fields, const RecordStorageLocation::FieldToLoc &FieldLocs)
Returns whether Fields and FieldLocs contain the same fields.
Definition ASTOps.cpp:80
llvm::SmallSetVector< const FieldDecl *, 4 > FieldSet
A set of FieldDecl *.
Definition ASTOps.h:43
The JSON file list parser is used to communicate input to InstallAPI.
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
A collection of several types of declarations, all referenced from the same function.
Definition ASTOps.h:143
llvm::DenseSet< const VarDecl * > Globals
All variables with static storage duration, notably including static member variables and static vari...
Definition ASTOps.h:148
llvm::DenseSet< const VarDecl * > Locals
Local variables, not including parameters or static variables declared within a function.
Definition ASTOps.h:151
llvm::DenseSet< const ParmVarDecl * > LambdaCapturedParams
When analyzing a lambda's call operator, the set of all parameters (from the surrounding function) th...
Definition ASTOps.h:158
llvm::DenseSet< const FunctionDecl * > Functions
Free functions and member functions which are referenced (but not necessarily called).
Definition ASTOps.h:154
FieldSet Fields
Non-static member variables.
Definition ASTOps.h:145