2017-11-22 07:26:08 +08:00
|
|
|
//===- StmtIterator.cpp - Iterators for Statements ------------------------===//
|
2007-10-18 08:24:38 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-18 08:24:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines internal methods for StmtIterator.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/StmtIterator.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2007-10-18 08:24:38 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-12-06 07:32:09 +08:00
|
|
|
// FIXME: Add support for dependent-sized array types in C++?
|
|
|
|
// Does it even make sense to build a CFG for an uninstantiated template?
|
2011-01-19 14:33:43 +08:00
|
|
|
static inline const VariableArrayType *FindVA(const Type* t) {
|
|
|
|
while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
|
|
|
|
if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
|
2007-10-30 04:50:16 +08:00
|
|
|
if (vat->getSizeExpr())
|
|
|
|
return vat;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
t = vt->getElementType().getTypePtr();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2007-10-26 06:24:19 +08:00
|
|
|
}
|
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
void StmtIteratorBase::NextVA() {
|
2017-11-22 07:26:08 +08:00
|
|
|
assert(getVAPtr());
|
2007-10-19 00:25:40 +08:00
|
|
|
|
2011-01-19 14:33:43 +08:00
|
|
|
const VariableArrayType *p = getVAPtr();
|
2007-10-30 04:50:16 +08:00
|
|
|
p = FindVA(p->getElementType().getTypePtr());
|
|
|
|
setVAPtr(p);
|
2007-10-19 00:25:40 +08:00
|
|
|
|
2008-10-08 07:35:42 +08:00
|
|
|
if (p)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-28 04:39:04 +08:00
|
|
|
if (inDeclGroup()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(*DGI))
|
2015-12-30 10:51:00 +08:00
|
|
|
if (VD->hasInit())
|
2008-10-08 07:35:42 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
NextDecl();
|
2008-10-08 07:35:42 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-06-28 04:39:04 +08:00
|
|
|
assert(inSizeOfTypeVA());
|
2007-12-15 06:52:23 +08:00
|
|
|
RawVAPtr = 0;
|
2008-05-21 13:06:46 +08:00
|
|
|
}
|
2007-10-19 02:19:31 +08:00
|
|
|
}
|
|
|
|
|
2007-10-30 04:50:16 +08:00
|
|
|
void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
|
2014-05-12 13:36:57 +08:00
|
|
|
assert(getVAPtr() == nullptr);
|
2013-06-28 04:39:04 +08:00
|
|
|
assert(inDeclGroup());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-28 04:39:04 +08:00
|
|
|
if (ImmediateAdvance)
|
|
|
|
++DGI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-28 04:39:04 +08:00
|
|
|
for ( ; DGI != DGE; ++DGI)
|
|
|
|
if (HandleDecl(*DGI))
|
2009-03-30 00:50:03 +08:00
|
|
|
return;
|
2008-10-08 07:04:14 +08:00
|
|
|
|
|
|
|
RawVAPtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StmtIteratorBase::HandleDecl(Decl* D) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
|
2011-01-19 14:33:43 +08:00
|
|
|
if (const VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) {
|
2008-10-08 07:04:14 +08:00
|
|
|
setVAPtr(VAPtr);
|
|
|
|
return true;
|
2007-10-30 05:38:03 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
if (VD->getInit())
|
|
|
|
return true;
|
2007-10-19 02:19:31 +08:00
|
|
|
}
|
2011-04-15 22:24:37 +08:00
|
|
|
else if (TypedefNameDecl* TD = dyn_cast<TypedefNameDecl>(D)) {
|
2011-01-19 14:33:43 +08:00
|
|
|
if (const VariableArrayType* VAPtr =
|
2008-10-08 07:04:14 +08:00
|
|
|
FindVA(TD->getUnderlyingType().getTypePtr())) {
|
|
|
|
setVAPtr(VAPtr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
if (ECD->getInitExpr())
|
|
|
|
return true;
|
2007-10-30 05:23:58 +08:00
|
|
|
}
|
2008-10-08 07:04:14 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return false;
|
2007-10-30 04:50:16 +08:00
|
|
|
}
|
|
|
|
|
2008-10-08 07:04:14 +08:00
|
|
|
StmtIteratorBase::StmtIteratorBase(Decl** dgi, Decl** dge)
|
2017-11-22 07:26:08 +08:00
|
|
|
: DGI(dgi), RawVAPtr(DeclGroupMode), DGE(dge) {
|
2008-10-08 07:04:14 +08:00
|
|
|
NextDecl(false);
|
|
|
|
}
|
|
|
|
|
2011-01-19 14:33:43 +08:00
|
|
|
StmtIteratorBase::StmtIteratorBase(const VariableArrayType* t)
|
2017-11-22 07:26:08 +08:00
|
|
|
: DGI(nullptr), RawVAPtr(SizeOfTypeVAMode) {
|
2007-12-15 06:52:23 +08:00
|
|
|
RawVAPtr |= reinterpret_cast<uintptr_t>(t);
|
|
|
|
}
|
|
|
|
|
2007-10-26 06:24:19 +08:00
|
|
|
Stmt*& StmtIteratorBase::GetDeclExpr() const {
|
2011-01-19 14:33:43 +08:00
|
|
|
if (const VariableArrayType* VAPtr = getVAPtr()) {
|
2017-11-22 07:26:08 +08:00
|
|
|
assert(VAPtr->SizeExpr);
|
2011-01-19 14:33:43 +08:00
|
|
|
return const_cast<Stmt*&>(VAPtr->SizeExpr);
|
2007-10-26 06:24:19 +08:00
|
|
|
}
|
2009-03-12 02:17:16 +08:00
|
|
|
|
2017-11-22 07:26:08 +08:00
|
|
|
assert(inDeclGroup());
|
2013-06-28 04:39:04 +08:00
|
|
|
VarDecl* VD = cast<VarDecl>(*DGI);
|
|
|
|
return *VD->getInitAddress();
|
2007-10-18 08:24:38 +08:00
|
|
|
}
|