25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/STLExtras.h"
31#define DEBUG_TYPE "dataflow"
36 const Expr *Current = &
E;
38 while (Current !=
Last) {
40 if (
auto *EWC = dyn_cast<ExprWithCleanups>(Current)) {
41 Current = EWC->getSubExpr();
42 assert(Current !=
nullptr);
44 if (
auto *CE = dyn_cast<ConstantExpr>(Current)) {
45 Current = CE->getSubExpr();
46 assert(Current !=
nullptr);
48 Current = Current->IgnoreParens();
49 assert(Current !=
nullptr);
55 if (
auto *
E = dyn_cast<Expr>(&S))
83 if (Fields.size() != FieldLocs.size())
85 for ([[maybe_unused]]
auto [Field,
Loc] : FieldLocs)
86 if (!Fields.contains(cast_or_null<FieldDecl>(Field)))
95template <
class InitListT>
96static std::vector<const FieldDecl *>
98 const RecordDecl *RD = InitList->getType()->getAsRecordDecl();
99 assert(RD !=
nullptr);
101 std::vector<const FieldDecl *> Fields;
103 if (InitList->getType()->isUnionType()) {
104 if (
const FieldDecl *Field = InitList->getInitializedFieldInUnion())
105 Fields.push_back(Field);
114 RD->
fields(), std::back_inserter(Fields),
115 [](
const FieldDecl *Field) { return !Field->isUnnamedBitField(); });
122 InitList->inits()) {}
128 ParenInitList->getInitExprs()) {}
131 QualType Ty, std::vector<const FieldDecl *> Fields,
134 assert(RD !=
nullptr);
142 assert(Fields.size() <= 1);
143 if (!Fields.empty()) {
144 ImplicitValueInitForUnion.emplace(Fields.front()->getType());
145 InitsForUnion.push_back(&*ImplicitValueInitForUnion);
147 Inits = InitsForUnion;
152 assert(Fields.size() + RD->getNumBases() == Inits.size());
153 for (
const CXXBaseSpecifier &
Base : RD->bases()) {
154 assert(InitIdx < Inits.size());
155 Expr *
Init = Inits[InitIdx++];
156 BaseInits.emplace_back(&
Base,
Init);
159 assert(Fields.size() == Inits.size() - InitIdx);
160 for (
const FieldDecl *Field : Fields) {
161 assert(InitIdx < Inits.size());
162 Expr *
Init = Inits[InitIdx++];
163 FieldInits.emplace_back(Field,
Init);
168 llvm::DenseSet<const VarDecl *> &Globals) {
169 if (
auto *
V = dyn_cast<VarDecl>(&
D))
170 if (
V->hasGlobalStorage())
175 llvm::DenseSet<const VarDecl *> &Locals) {
176 if (
auto *
V = dyn_cast<VarDecl>(&
D))
177 if (
V->hasLocalStorage() && !isa<ParmVarDecl>(
V))
182 llvm::DenseSet<const FunctionDecl *> &Funcs) {
183 if (
auto *FD = dyn_cast<FunctionDecl>(&
D))
190 const auto *MethodDecl = dyn_cast_or_null<CXXMethodDecl>(
C.getCalleeDecl());
193 auto *Body = dyn_cast_or_null<CompoundStmt>(MethodDecl->getBody());
194 if (!Body || Body->size() != 1)
196 if (
auto *RS = dyn_cast<ReturnStmt>(*Body->body_begin()))
197 if (
auto *Return = RS->getRetValue())
198 return dyn_cast<MemberExpr>(Return->IgnoreParenImpCasts());
205 : Referenced(Referenced) {}
209 if (
Init->isMemberInitializer()) {
211 }
else if (
Init->isIndirectMemberInitializer()) {
212 for (
const auto *I :
Init->getIndirectMember()->chain())
213 Referenced.
Fields.insert(cast<FieldDecl>(I));
223 if (
auto *DefaultInit = dyn_cast<CXXDefaultInitExpr>(InitExpr))
246 if (
const auto *FD = dyn_cast<FieldDecl>(
E->getMemberDecl()))
247 Referenced.
Fields.insert(FD);
256 if (
const auto *FD = dyn_cast<FieldDecl>(VD))
257 Referenced.
Fields.insert(FD);
264 Referenced.
Fields.insert(FD);
271 Referenced.
Fields.insert(FD);
283 if (
const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(&FD))
296 if (
const auto *Method = dyn_cast<CXXMethodDecl>(&FD);
298 for (
const auto &
Capture : Method->getParent()->captures()) {
299 if (
Capture.capturesVariable()) {
300 if (
const auto *Param =
301 dyn_cast<ParmVarDecl>(
Capture.getCapturedVar())) {
302 Result.LambdaCapturedParams.insert(Param);
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
C Language Family Type Representation.
Represents a base class of a C++ class.
Represents a C++ constructor within a class.
Represents a C++ base or member initializer.
Represents a call to a member function that may be written either with member call syntax (e....
Represents a list-initialization with parenthesis.
A reference to a declared variable, function, enum, etc.
Decl - This represents one declaration (or definition), e.g.
virtual bool TraverseStmt(Stmt *S)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
This represents one expression.
Represents a member of a struct/union/class.
Represents a function declaration or definition.
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Describes an C or C++ initializer list.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
A (possibly-)qualified type.
Represents a struct/union/class.
field_range fields() const
Stmt - This represents one statement.
The base class of the type hierarchy.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
bool isRecordType() const
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Specialization of RecursiveASTVisitor that visits those nodes that are relevant to the dataflow analy...
Helper class for initialization of a record with an InitListExpr.
RecordInitListHelper(const InitListExpr *InitList)
llvm::DenseMap< const ValueDecl *, StorageLocation * > FieldToLoc
bool VisitCXXParenListInitExpr(CXXParenListInitExpr *ParenInitList) override
bool VisitInitListExpr(InitListExpr *InitList) override
bool VisitMemberExpr(MemberExpr *E) override
bool VisitDeclRefExpr(DeclRefExpr *E) override
void traverseConstructorInits(const CXXConstructorDecl *Ctor)
bool VisitCXXMemberCallExpr(CXXMemberCallExpr *C) override
ReferencedDeclsVisitor(ReferencedDecls &Referenced)
bool VisitDecl(Decl *D) override
Dataflow Directional Tag Classes.
static void getFieldsFromClassHierarchy(QualType Type, FieldSet &Fields)
static void insertIfFunction(const Decl &D, llvm::DenseSet< const FunctionDecl * > &Funcs)
static MemberExpr * getMemberForAccessor(const CXXMemberCallExpr &C)
ReferencedDecls getReferencedDecls(const FunctionDecl &FD)
Returns declarations that are declared in or referenced from FD.
static std::vector< const FieldDecl * > getFieldsForInitListExpr(const InitListT *InitList)
Returns the fields of a RecordDecl that are initialized by an InitListExpr or CXXParenListInitExpr,...
const Expr & ignoreCFGOmittedNodes(const Expr &E)
Skip past nodes that the CFG does not emit.
FieldSet getObjectFields(QualType Type)
Returns the set of all fields in the type.
static void insertIfGlobal(const Decl &D, llvm::DenseSet< const VarDecl * > &Globals)
static void insertIfLocal(const Decl &D, llvm::DenseSet< const VarDecl * > &Locals)
bool containsSameFields(const FieldSet &Fields, const RecordStorageLocation::FieldToLoc &FieldLocs)
Returns whether Fields and FieldLocs contain the same fields.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
@ Result
The result type of a method or function.
A collection of several types of declarations, all referenced from the same function.
llvm::DenseSet< const VarDecl * > Globals
All variables with static storage duration, notably including static member variables and static vari...
llvm::DenseSet< const VarDecl * > Locals
Local variables, not including parameters or static variables declared within a function.
llvm::DenseSet< const FunctionDecl * > Functions
Free functions and member functions which are referenced (but not necessarily called).
FieldSet Fields
Non-static member variables.