clang 20.0.0git
StmtIterator.cpp
Go to the documentation of this file.
1//===- StmtIterator.cpp - Iterators for Statements ------------------------===//
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 internal methods for StmtIterator.
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/AST/Decl.h"
15#include "clang/AST/Type.h"
16#include "clang/Basic/LLVM.h"
17#include "llvm/Support/Casting.h"
18#include <cassert>
19#include <cstdint>
20
21using namespace clang;
22
23// FIXME: Add support for dependent-sized array types in C++?
24// Does it even make sense to build a CFG for an uninstantiated template?
25static inline const VariableArrayType *FindVA(const Type* t) {
26 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
27 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
28 if (vat->getSizeExpr())
29 return vat;
30
31 t = vt->getElementType().getTypePtr();
32 }
33
34 return nullptr;
35}
36
38 assert(getVAPtr());
39
40 const VariableArrayType *p = getVAPtr();
42 setVAPtr(p);
43
44 if (p)
45 return;
46
47 if (inDeclGroup()) {
48 if (VarDecl* VD = dyn_cast<VarDecl>(*DGI))
49 if (VD->hasInit())
50 return;
51
52 NextDecl();
53 }
54 else {
55 assert(inSizeOfTypeVA());
56 RawVAPtr = 0;
57 }
58}
59
60void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
61 assert(getVAPtr() == nullptr);
62 assert(inDeclGroup());
63
64 if (ImmediateAdvance)
65 ++DGI;
66
67 for ( ; DGI != DGE; ++DGI)
68 if (HandleDecl(*DGI))
69 return;
70
71 RawVAPtr = 0;
72}
73
75 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
76 if (const VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) {
77 setVAPtr(VAPtr);
78 return true;
79 }
80
81 if (VD->getInit())
82 return true;
83 }
84 else if (TypedefNameDecl* TD = dyn_cast<TypedefNameDecl>(D)) {
85 if (const VariableArrayType* VAPtr =
86 FindVA(TD->getUnderlyingType().getTypePtr())) {
87 setVAPtr(VAPtr);
88 return true;
89 }
90 }
91 else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(D)) {
92 if (ECD->getInitExpr())
93 return true;
94 }
95
96 return false;
97}
98
100 : DGI(dgi), RawVAPtr(DeclGroupMode), DGE(dge) {
101 NextDecl(false);
102}
103
105 : DGI(nullptr), RawVAPtr(SizeOfTypeVAMode) {
106 RawVAPtr |= reinterpret_cast<uintptr_t>(t);
107}
108
110 if (const VariableArrayType* VAPtr = getVAPtr()) {
111 assert(VAPtr->SizeExpr);
112 return const_cast<Stmt*&>(VAPtr->SizeExpr);
113 }
114
115 assert(inDeclGroup());
116 VarDecl* VD = cast<VarDecl>(*DGI);
117 return *VD->getInitAddress();
118}
static const VariableArrayType * FindVA(const Type *t)
Definition: CFG.cpp:1413
const Decl * D
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static const VariableArrayType * FindVA(const Type *t)
C Language Family Type Representation.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3571
QualType getElementType() const
Definition: Type.h:3583
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
An instance of this object exists for each enum constant that is defined.
Definition: Decl.h:3274
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7755
void NextDecl(bool ImmediateAdvance=true)
bool HandleDecl(Decl *D)
void setVAPtr(const VariableArrayType *P)
Definition: StmtIterator.h:64
Stmt *& GetDeclExpr() const
const VariableArrayType * getVAPtr() const
Definition: StmtIterator.h:60
bool inDeclGroup() const
Definition: StmtIterator.h:48
bool inSizeOfTypeVA() const
Definition: StmtIterator.h:52
Stmt - This represents one statement.
Definition: Stmt.h:84
The base class of the type hierarchy.
Definition: Type.h:1829
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3409
Represents a variable declaration or definition.
Definition: Decl.h:879
Stmt ** getInitAddress()
Retrieve the address of the initializer expression.
Definition: Decl.cpp:2404
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3800
The JSON file list parser is used to communicate input to InstallAPI.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...