[llvm-dev] [BUG] Incorrect ASCII escape characters on Mac

Ramkumar Ramachandra via llvm-dev llvm-dev at lists.llvm.org
Thu Aug 6 09:07:21 PDT 2015


*sigh*

So, it turns out that it's just a bug in dump(), which doesn't bother
me enough. Sure enough, the i8 array itself is fine.

    IRBuilder<> Builder(getGlobalContext());
    auto M = new llvm::Module("main", getGlobalContext());
    std::vector<Type *> ATyAr;
    auto FTy = FunctionType::get(Builder.getVoidTy(), ATyAr, false);
    auto ExecutionHandle = cast<Function>(M->getOrInsertFunction("main", FTy));
    auto BB = BasicBlock::Create(Builder.getContext(), "entry",
ExecutionHandle);
    Builder.SetInsertPoint(BB);
    std::vector<int> rawData = { 34 , -48 , 18 , -12 , 33 , 0 , 21 ,
-7 , -20 , -31 };
    std::vector<Constant *> coercedData;
    auto i8Ty = Builder.getInt8Ty();
    for (auto el : rawData) {
        coercedData.push_back(ConstantInt::get(i8Ty, el, 10));
    }

    auto i8PtrTy = Builder.getInt8PtrTy();
    ArrayRef<Type *> ArgTys(i8PtrTy);
    FunctionType *PrintfTy =
        FunctionType::get(Builder.getInt32Ty(), ArgTys, /* IsVarArgs = */ true);
    auto PrintfHandle =
        dyn_cast<Function>(M->getOrInsertFunction("printf", PrintfTy));
    PrintfHandle->setCallingConv(CallingConv::C);
    auto FormatStringPtr = Builder.CreateGlobalStringPtr("%d ");

    auto i8ArTy = ArrayType::get(i8Ty, 10);
    auto StrConstant = ConstantArray::get(i8ArTy, ArrayRef<Constant
*>(coercedData));
    auto GV = new GlobalVariable(*M, StrConstant->getType(), true,
                                 GlobalValue::PrivateLinkage, StrConstant);
    auto ConstantZero =
ConstantInt::get(Type::getInt32Ty(Builder.getContext()), 0);
    for (auto i = 0; i < 10; i++) {
        std::vector<Value *> ThisElIdx =
            { ConstantZero,
ConstantInt::get(Type::getInt32Ty(Builder.getContext()), i) };
        auto FirstEl = Builder.CreateGEP(GV, ArrayRef<Value *>(ThisElIdx));
        auto LoadedV = Builder.CreateLoad(FirstEl);
        Builder.CreateCall2(PrintfHandle, FormatStringPtr, LoadedV);
    }
    Builder.CreateRet(nullptr);

    LLVMInitializeNativeTarget();
    LLVMInitializeNativeAsmPrinter();

    auto EE = EngineBuilder(M).create();
    assert(EE && "Error creating MCJIT with EngineBuilder");
    typedef int (*MainFTy)();
    union {
        uint64_t raw;
        MainFTy usable;
    } functionPointer;
    functionPointer.raw = (uint64_t)EE->getPointerToFunction(ExecutionHandle);
    assert(functionPointer.usable && "no main function found");
    testing::internal::CaptureStdout();
    functionPointer.usable();
    auto Captured = testing::internal::GetCapturedStdout();
    // check Captured


More information about the llvm-dev mailing list