From 6b332f91c82da6e19efad92b58ac1d9c1bc9eec0 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 09:35:19 +0000 Subject: feat(runtime): write runtime diagnostics to stderr Runtime check messages and the unhandled-exception message were printed to stdout by the generated program, so `cpsc x.cps -r >out.txt` captured them. They are now formatted with snprintf and written with write(2). Test expectations and docs are updated accordingly. Co-Authored-By: Claude Sonnet 5.5 --- include/cps/RuntimeCheck.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'include/cps') diff --git a/include/cps/RuntimeCheck.h b/include/cps/RuntimeCheck.h index f5a8aa7..8f33e99 100644 --- a/include/cps/RuntimeCheck.h +++ b/include/cps/RuntimeCheck.h @@ -14,7 +14,10 @@ class RuntimeCheck { llvm::LLVMContext &TheContext; llvm::IRBuilder<> &Builder; - llvm::FunctionCallee PrintfFunc; + llvm::FunctionCallee SnprintfFunc; + llvm::FunctionCallee WriteFunc; + llvm::FunctionCallee StrLenFunc; + llvm::GlobalVariable *ErrBuf; ExceptionHandler &Exceptions; llvm::Value *DivZeroMsg; llvm::Value *OutOfBoundsMsg; @@ -29,6 +32,9 @@ public: RuntimeCheck(llvm::Module &M, llvm::LLVMContext &C, llvm::IRBuilder<> &B, ExceptionHandler &EH); + // Format a runtime diagnostic (printf-style) and write it to stderr. + void emitStderr(llvm::Value *Msg, llvm::ArrayRef FmtArgs = {}); + void emitDivZeroCheck(llvm::Value *Divisor, int Line); void emitIndexCheck(llvm::Value *Index, llvm::Value *Lower, llvm::Value *Upper, int Line); @@ -44,7 +50,7 @@ public: void emitArrayArgBoundsCheck(llvm::Value *ActualLower, llvm::Value *ActualUpper, int64_t DeclaredLower, int64_t DeclaredUpper, int Line); - // Print the diagnostic, then throw an anonymous exception on failure. + // Print the diagnostic to stderr, then throw an anonymous exception on failure. // Runtime-computed variant: FmtArgs are the printf varargs after Msg // (e.g. {i32 line, ptr filename}). Needed when the failure site is inside // a synthesized helper function, where the line number is an argument -- cgit v1.3.1-4-g156e