You need to use an instance of ConstantAggregateZero
instead of an instance of ConstantArray
.
=== Notice - the below information is outdated ===
In general, if you want to know how to emulate what Clang generates for a particular file, you can use the C++ backend on the Clang-generated file to emit C++ code which generates the same output.
For example, if you go to the LLVM demo page, provide the code int a[5] = {0};
and choose the "LLVM C++ API code" target, you'll get:
// Type Definitions
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(mod->getContext(), 32), 5);
PointerType* PointerTy_1 = PointerType::get(ArrayTy_0, 0);
// Function Declarations
// Global Variable Declarations
GlobalVariable* gvar_array_a = new GlobalVariable(/*Module=*/*mod,
/*Type=*/ArrayTy_0,
/*isConstant=*/false,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"a");
gvar_array_a->setAlignment(16);
// Constant Definitions
ConstantAggregateZero* const_array_2 = ConstantAggregateZero::get(ArrayTy_0);
// Global Variable Definitions
gvar_array_a->setInitializer(const_array_2);
And you can see the use of ConstantAggregateZero
at the one-before-last code line.