A problem about builtin-type creation

Dear All,

I am using Clang/LLVM-3.3, at present, I am fixing an error about creating a builtin type, the code is supposed to
generate a "boolean" type with the following code:

clang::Type* bool_type = new clang::BuiltinType(clang::BuiltinType::Bool);
params.push_back(klimt::NamedDecl(clang::QualType(bool_type, 0), pname));

I always encountered the error:

/home/xxx/vendor/llvm-3.3/build32/Release+Asserts/../../include/llvm/ADT/PointerIntPair.h:100: void llvm::PointerIntPair<PointerTy, IntBits, IntType,

::initWithPointer(PointerTy) [with PointerTy = void*; unsigned

int IntBits = 1u; IntType = bool; PtrTraits = llvm::PointerUnionUIntTraits<const clang::Type*, const clang::ExtQuals*>]: Assertion `(PtrWord & ((1 << PtrTraits::NumLowBitsAvailable)-1)) == 0 && "Pointer is not sufficiently aligned"' failed

The assertion fails at the BuiltinType creation.

Any thoughts about how to fix this error is highly appreciated!


It is asserting that the Type pointer is not 16 byte aligned. Types are generally constructed and uniqued in an ASTContext, which guarantees this alignment. See this at the top of Type.h:

enum {

TypeAlignmentInBits = 4,
TypeAlignment = 1 << TypeAlignmentInBits

Presumably you are building 32-bit code, where the system malloc returns 8 byte aligned chunks.

Hi Reid

Thanks a lot for your instant reply.

Yes, I am building everything in 32-bit mode, so would you please
point me out how to create the boolean type in a memory-aligned way?


在 2014-08-05 10:48,Reid Kleckner 写道:

Typically a Clang tool would read ASTContext::BoolTy. I’m not sure what you’re trying to do, but I would look at other clang tools that use lib/Tooling.

Ask your ASTContext for it. Don’t create types yourself; doing so will break Clang’s canonical type system.