Backlog/lib
Peterino2 79dd39e36a feat: Strip format attribute macros and add va_list support - +2 APIs!
Added support for C printf/scanf format attribute macros and variadic argument
lists, unlocking 2 more perfect APIs.

## Features Added

### 1. Format Attribute Macro Stripping
Strips compiler attribute macros from function declarations:
- `SDL_PRINTF_FORMAT_STRING`
- `SDL_WPRINTF_FORMAT_STRING`
- `SDL_SCANF_FORMAT_STRING`
- `SDL_PRINTF_VARARG_FUNC(N)`
- `SDL_PRINTF_VARARG_FUNCV(N)`
- `SDL_WPRINTF_VARARG_FUNC(N)`
- `SDL_SCANF_VARARG_FUNC(N)`

**Before** (C):
```c
extern SDL_DECLSPEC bool SDLCALL SDL_SetError(
    SDL_PRINTF_FORMAT_STRING const char *fmt, ...
) SDL_PRINTF_VARARG_FUNC(1);
```

**After** (Zig):
```zig
pub inline fn setError(fmt: [*c]const u8, ...) bool {
    return c.SDL_SetError(fmt, ...);
}
```

### 2. Variadic Arguments Support
Added `va_list` type conversion:
- C type: `va_list`
- Zig type: `std.builtin.VaList`

**Implementation**: Added `const std = @import("std");` to generated headers
to make `std.builtin.VaList` available.

### 3. Double Void Pointer Support
Added conversion for `void **`:
- C type: `void **userdata`
- Zig type: `userdata: [*c]?*anyopaque`

## Implementation Details

### Macro Stripping Algorithm (patterns.zig)
1. **Format String Macros**: Scan function text for format macros
   - Pattern: `SDL_PRINTF_FORMAT_STRING const char *fmt`
   - Remove macro, keep type: `const char *fmt`
   - Handle: PRINTF, WPRINTF, SCANF variants

2. **Vararg Function Macros**: Find and remove end-of-declaration macros
   - Pattern: `) SDL_PRINTF_VARARG_FUNC(1);`
   - Locate macro position
   - Find closing `)` and remove from macro to `)`
   - Handle: PRINTF, WPRINTF, SCANF, FUNCV variants

3. **Safe String Manipulation**:
   - Create new string with `std.fmt.allocPrint`
   - Clear and repopulate ArrayList (avoids aliasing)
   - Defer cleanup of temporary strings

### Type Conversions (types.zig)
```zig
// Variadic lists
"va_list" → "std.builtin.VaList"

// Double void pointers
"void **" → "[*c]?*anyopaque"
```

### Header Generation (codegen.zig)
Added std import to all generated files:
```zig
const std = @import("std");
pub const c = @import("c.zig").c;
```

## Results

### Before
- 22/43 APIs perfect (51%)
- Format macros: NOT STRIPPED
- va_list: NOT SUPPORTED
- void **: PARTIALLY SUPPORTED

### After
- **24/43 APIs perfect (56%)** 
- Format macros: FULLY STRIPPED
- va_list: FULLY SUPPORTED
- void **: FULLY SUPPORTED

**Progress: +5% (+2 APIs)**

### New Perfect APIs

 **SDL_error.h** (24 lines)
   - Error handling API
   - `SDL_SetError()` uses printf-style formatting
   - Had 1 error: format macros + va_list
   - Now perfect!

 **SDL_log.h** (148 lines)
   - Logging system with priority levels
   - Multiple printf-style log functions
   - Custom log output callbacks
   - Had 1 error: format macros + void**
   - Now perfect!

## Testing

Tested against all 43 SDL3 headers:
- **24 compile perfectly** (56%) 
- 19 have 1-13 errors
- 0 complete failures

**Cumulative Progress**:
- Session start: 15 APIs (35%)
- After function pointers: 19 APIs (44%)
- After arrays/comments: 22 APIs (51%)
- After format macros: **24 APIs (56%)** 🎉

**More than HALF of SDL3 APIs generate perfectly!**

## Impact

**Immediate**: +2 perfect APIs (5% improvement)
**Unlocked**: Printf-style functions now work everywhere
**Fixed**: Variadic argument handling

## Code Changes

### src/patterns.zig (+60 lines)
- `scanFunction()`: Strip format and vararg macros
- Safe string manipulation with allocPrint
- Handles all format macro variants

### src/types.zig (+2 lines)
- Added `va_list` → `std.builtin.VaList` conversion
- Added `void **` → `[*c]?*anyopaque` conversion

### src/codegen.zig (+2 lines)
- Added `const std = @import("std");` to generated headers
- Updated test expectations

## Known Limitations

Function pointer fields in structs not yet supported:
```c
Sint64 (SDLCALL *size)(void *userdata);  // Struct field
```

This affects:
- SDL_iostream.h (IOStreamInterface)
- SDL_storage.h (StorageInterface)
- SDL_dialog.h (DialogFileFilter callback)

Will be addressed in future commits.

---

Printf-style functions now work perfectly across SDL3!
2026-01-22 14:55:45 -08:00
..
.claude switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
bh switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
cimgui switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
enet switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
lua switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
miniaudio switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
nfd switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
objLoader switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
ozz switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
p2 saving 2026-01-19 21:40:05 -08:00
packer saving 2026-01-19 21:40:05 -08:00
sdl3 feat: Strip format attribute macros and add va_list support - +2 APIs! 2026-01-22 14:55:45 -08:00
spng switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
theoratest switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
tracy switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
watcher switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
zargs deleted a lot of intermediate files and tests 2026-01-22 03:58:37 -08:00
zgltf switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
zmath switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
zphysics switched all builds to bh.declareOptions 2025-11-28 20:14:56 -08:00
readme.md zig build lib 2025-04-11 22:43:41 -07:00
test-libs.py change tests to avoid -fincremental, causes issues on windows, also reducing payload size for concurrent queue test, due to memory constraints 2025-11-06 20:44:59 -08:00
test.zig fixed all tests and added bh.zig 2025-11-06 20:42:48 -08:00

readme.md

libraries go here.