From 9892c8bf9a0755dbedb3b5691f50e1e827e0b831 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Thu, 6 Dec 2012 11:39:22 -0500 Subject: [PATCH] Fix logic for ordering of struct declarations in generated header files. When a struct had an array of another struct type as a member, we weren't detecting that the struct type in the array needed to be declared before the enclosing struct type. Fixes issue #408. --- module.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/module.cpp b/module.cpp index be6796af..b99153b7 100644 --- a/module.cpp +++ b/module.cpp @@ -1075,6 +1075,23 @@ Module::writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine, } +/** Given a pointer to an element of a structure, see if it is a struct + type or an array of a struct type. If so, return a pointer to the + underlying struct type. */ +static const StructType * +lGetElementStructType(const Type *t) { + const StructType *st = CastType(t); + if (st != NULL) + return st; + + const ArrayType *at = CastType(t); + if (at != NULL) + return lGetElementStructType(at->GetElementType()); + + return NULL; +} + + /** Emits a declaration for the given struct to the given file. This function first makes sure that declarations for any structs that are (recursively) members of this struct are emitted first. @@ -1092,7 +1109,7 @@ lEmitStructDecl(const StructType *st, std::vector *emittedSt // Otherwise first make sure any contained structs have been declared. for (int i = 0; i < st->GetElementCount(); ++i) { const StructType *elementStructType = - CastType(st->GetElementType(i)); + lGetElementStructType(st->GetElementType(i)); if (elementStructType != NULL) lEmitStructDecl(elementStructType, emittedStructs, file); }