1 //===- StmtIterator.cpp - Iterators for Statements ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines internal methods for StmtIterator.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/StmtIterator.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/Type.h"
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/Support/Casting.h"
21 using namespace clang
;
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?
25 static 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())
31 t
= vt
->getElementType().getTypePtr();
37 void StmtIteratorBase::NextVA() {
40 const VariableArrayType
*p
= getVAPtr();
41 p
= FindVA(p
->getElementType().getTypePtr());
48 if (VarDecl
* VD
= dyn_cast
<VarDecl
>(*DGI
))
55 assert(inSizeOfTypeVA());
60 void StmtIteratorBase::NextDecl(bool ImmediateAdvance
) {
61 assert(getVAPtr() == nullptr);
62 assert(inDeclGroup());
67 for ( ; DGI
!= DGE
; ++DGI
)
74 bool StmtIteratorBase::HandleDecl(Decl
* D
) {
75 if (VarDecl
* VD
= dyn_cast
<VarDecl
>(D
)) {
76 if (const VariableArrayType
* VAPtr
= FindVA(VD
->getType().getTypePtr())) {
84 else if (TypedefNameDecl
* TD
= dyn_cast
<TypedefNameDecl
>(D
)) {
85 if (const VariableArrayType
* VAPtr
=
86 FindVA(TD
->getUnderlyingType().getTypePtr())) {
91 else if (EnumConstantDecl
* ECD
= dyn_cast
<EnumConstantDecl
>(D
)) {
92 if (ECD
->getInitExpr())
99 StmtIteratorBase::StmtIteratorBase(Decl
** dgi
, Decl
** dge
)
100 : DGI(dgi
), RawVAPtr(DeclGroupMode
), DGE(dge
) {
104 StmtIteratorBase::StmtIteratorBase(const VariableArrayType
* t
)
105 : DGI(nullptr), RawVAPtr(SizeOfTypeVAMode
) {
106 RawVAPtr
|= reinterpret_cast<uintptr_t>(t
);
109 Stmt
*& StmtIteratorBase::GetDeclExpr() const {
110 if (const VariableArrayType
* VAPtr
= getVAPtr()) {
111 assert(VAPtr
->SizeExpr
);
112 return const_cast<Stmt
*&>(VAPtr
->SizeExpr
);
115 assert(inDeclGroup());
116 VarDecl
* VD
= cast
<VarDecl
>(*DGI
);
117 return *VD
->getInitAddress();