Fix C assert calls for newlib (#4351)

This commit is contained in:
Hiroki Noda 2023-04-13 22:42:41 +09:00 committed by GitHub
parent 25dc3fc6eb
commit d001a7cdb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View file

@ -317,6 +317,14 @@ void DtoCAssert(Module *M, const Loc &loc, LLValue *msg) {
args.push_back(file); args.push_back(file);
args.push_back(line); args.push_back(line);
args.push_back(msg); args.push_back(msg);
} else if (global.params.isNewlibEnvironment) {
const auto irFunc = gIR->func();
const auto funcName =
irFunc && irFunc->decl ? irFunc->decl->toPrettyChars() : "";
args.push_back(file);
args.push_back(line);
args.push_back(DtoConstCString(funcName));
args.push_back(msg);
} else { } else {
args.push_back(msg); args.push_back(msg);
args.push_back(file); args.push_back(file);

View file

@ -364,6 +364,8 @@ llvm::Function *getRuntimeFunction(const Loc &loc, llvm::Module &target,
// const char *funcname); // const char *funcname);
// uClibc: void __assert(const char *assertion, const char *filename, int linenumber, // uClibc: void __assert(const char *assertion, const char *filename, int linenumber,
// const char *function); // const char *function);
// newlib: void __assert_func(const char *file, int line, const char *func,
// const char *failedexpr)
// else: void __assert(const char *msg, const char *file, unsigned line) // else: void __assert(const char *msg, const char *file, unsigned line)
static const char *getCAssertFunctionName() { static const char *getCAssertFunctionName() {
@ -376,6 +378,8 @@ static const char *getCAssertFunctionName() {
return "__assert_c99"; return "__assert_c99";
} else if (triple.isMusl()) { } else if (triple.isMusl()) {
return "__assert_fail"; return "__assert_fail";
} else if (global.params.isNewlibEnvironment) {
return "__assert_func";
} }
return "__assert"; return "__assert";
} }
@ -392,6 +396,9 @@ static std::vector<PotentiallyLazyType> getCAssertFunctionParamTypes() {
if (triple.getEnvironment() == llvm::Triple::Android) { if (triple.getEnvironment() == llvm::Triple::Android) {
return {voidPtr, uint, voidPtr}; return {voidPtr, uint, voidPtr};
} }
if (global.params.isNewlibEnvironment) {
return {voidPtr, uint, voidPtr, voidPtr};
}
return {voidPtr, voidPtr, uint}; return {voidPtr, voidPtr, uint};
} }

View file

@ -0,0 +1,8 @@
// RUN: %ldc %s -c --output-ll -of=%t.ll --mtriple=arm-none-newlibeabi --betterC --checkaction=C > %t && FileCheck %s < %t.ll
extern (C) void main()
{
assert(false);
}
// CHECK: declare void @__assert_func(i8*, i32, i8*, i8*)