llvm-project
118 строк · 2.9 Кб
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
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"
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) {
26while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
27if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
28if (vat->getSizeExpr())
29return vat;
30
31t = vt->getElementType().getTypePtr();
32}
33
34return nullptr;
35}
36
37void StmtIteratorBase::NextVA() {
38assert(getVAPtr());
39
40const VariableArrayType *p = getVAPtr();
41p = FindVA(p->getElementType().getTypePtr());
42setVAPtr(p);
43
44if (p)
45return;
46
47if (inDeclGroup()) {
48if (VarDecl* VD = dyn_cast<VarDecl>(*DGI))
49if (VD->hasInit())
50return;
51
52NextDecl();
53}
54else {
55assert(inSizeOfTypeVA());
56RawVAPtr = 0;
57}
58}
59
60void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
61assert(getVAPtr() == nullptr);
62assert(inDeclGroup());
63
64if (ImmediateAdvance)
65++DGI;
66
67for ( ; DGI != DGE; ++DGI)
68if (HandleDecl(*DGI))
69return;
70
71RawVAPtr = 0;
72}
73
74bool StmtIteratorBase::HandleDecl(Decl* D) {
75if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
76if (const VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) {
77setVAPtr(VAPtr);
78return true;
79}
80
81if (VD->getInit())
82return true;
83}
84else if (TypedefNameDecl* TD = dyn_cast<TypedefNameDecl>(D)) {
85if (const VariableArrayType* VAPtr =
86FindVA(TD->getUnderlyingType().getTypePtr())) {
87setVAPtr(VAPtr);
88return true;
89}
90}
91else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(D)) {
92if (ECD->getInitExpr())
93return true;
94}
95
96return false;
97}
98
99StmtIteratorBase::StmtIteratorBase(Decl** dgi, Decl** dge)
100: DGI(dgi), RawVAPtr(DeclGroupMode), DGE(dge) {
101NextDecl(false);
102}
103
104StmtIteratorBase::StmtIteratorBase(const VariableArrayType* t)
105: DGI(nullptr), RawVAPtr(SizeOfTypeVAMode) {
106RawVAPtr |= reinterpret_cast<uintptr_t>(t);
107}
108
109Stmt*& StmtIteratorBase::GetDeclExpr() const {
110if (const VariableArrayType* VAPtr = getVAPtr()) {
111assert(VAPtr->SizeExpr);
112return const_cast<Stmt*&>(VAPtr->SizeExpr);
113}
114
115assert(inDeclGroup());
116VarDecl* VD = cast<VarDecl>(*DGI);
117return *VD->getInitAddress();
118}
119