diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index c8303c9b48..9837b63fd3 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -3150,8 +3150,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor start_new_block(elseBB); { - llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("Assertion failed\n"); - printf(context, *module, *builder, {fmt_ptr}); + if (x.m_msg) { + char* s = ASR::down_cast(x.m_msg)->m_s; + llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("AssertionError: %s\n"); + llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(s); + printf(context, *module, *builder, {fmt_ptr, fmt_ptr2}); + } else { + llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("AssertionError\n"); + printf(context, *module, *builder, {fmt_ptr}); + } int exit_code_int = 1; llvm::Value *exit_code = llvm::ConstantInt::get(context, llvm::APInt(32, exit_code_int)); diff --git a/tests/reference/llvm-assert1-8df4f31.json b/tests/reference/llvm-assert1-8df4f31.json new file mode 100644 index 0000000000..5ca234fa08 --- /dev/null +++ b/tests/reference/llvm-assert1-8df4f31.json @@ -0,0 +1,13 @@ +{ + "basename": "llvm-assert1-8df4f31", + "cmd": "lpython --no-color --show-llvm {infile} -o {outfile}", + "infile": "tests/assert1.py", + "infile_hash": "0ff84ea5ccd3d0815cbb66e1c3d3acd61dee7257c98aa1a27ceeef3b", + "outfile": null, + "outfile_hash": null, + "stdout": "llvm-assert1-8df4f31.stdout", + "stdout_hash": "43ba1e8f621d033901f9c161f1962b78a7861d5fc2abc225602f326c", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/llvm-assert1-8df4f31.stdout b/tests/reference/llvm-assert1-8df4f31.stdout new file mode 100644 index 0000000000..0bfe5701f1 --- /dev/null +++ b/tests/reference/llvm-assert1-8df4f31.stdout @@ -0,0 +1,51 @@ +; ModuleID = 'LFortran' +source_filename = "LFortran" + +@0 = private unnamed_addr constant [20 x i8] c"AssertionError: %s\0A\00", align 1 +@1 = private unnamed_addr constant [11 x i8] c"a is not 5\00", align 1 +@2 = private unnamed_addr constant [16 x i8] c"AssertionError\0A\00", align 1 + +define void @test_assert() { +.entry: + %a = alloca i32, align 4 + store i32 5, i32* %a, align 4 + %0 = load i32, i32* %a, align 4 + %1 = icmp eq i32 %0, 5 + br i1 %1, label %then, label %else + +then: ; preds = %.entry + br label %ifcont + +else: ; preds = %.entry + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @0, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @1, i32 0, i32 0)) + call void @exit(i32 1) + br label %ifcont + +ifcont: ; preds = %else, %then + %2 = load i32, i32* %a, align 4 + %3 = icmp ne i32 %2, 10 + br i1 %3, label %then1, label %else2 + +then1: ; preds = %ifcont + br label %ifcont3 + +else2: ; preds = %ifcont + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @2, i32 0, i32 0)) + call void @exit(i32 1) + br label %ifcont3 + +ifcont3: ; preds = %else2, %then1 + br label %return + +return: ; preds = %ifcont3 + ret void +} + +declare void @_lfortran_printf(i8*, ...) + +declare void @exit(i32) + +define i32 @main() { +.entry: + ret i32 0 +} diff --git a/tests/tests.toml b/tests/tests.toml index e7e6a33f4a..7a116e4058 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -148,6 +148,7 @@ filename = "assert1.py" ast = true asr = true cpp = true +llvm = true [[test]] filename = "assign1.py"