Skip to content

Add support for stdarg.h#2

Merged
nunoplopes merged 23 commits intoCpp2Rust:masterfrom
lucic71:vaargs
Apr 13, 2026
Merged

Add support for stdarg.h#2
nunoplopes merged 23 commits intoCpp2Rust:masterfrom
lucic71:vaargs

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented Apr 13, 2026

This PR adds support for va_list, va_start, va_arg, va_end, va_copy.

va_list is implemented as libcc2rs::VaList, a cursor over a slice containing VaArgs.

pub enum VaArg {
    Int(i32),
    UInt(u32),
    Long(i64),
    ULong(u64),
    Double(f64),
    RawPtr(*mut c_void),
    Ptr(AnyPtr),
}

pub struct VaList<'a> {
    args: &'a [VaArg],
    pos: usize,
}

impl<'a> VaList<'a> {
    pub fn arg<T: VaArgGet>(&mut self) -> T {
       ...
    }
}

Callers of variadic functions build an array of VaArgs, for example:

int sum_ints(int first, ...);
sum_ints(1, 2, 3, 4, 0)

becomes in unsafe

pub unsafe fn sum_ints_0(mut first: i32, args: &[VaArg]) -> i32
sum_ints_0(1, &[2.into(), 3.into(), 4.into(), 0.into()])

Then, a va_start call creates a new VaList:

va_list ap;
va_start(ap, n);
let mut ap: VaList = <VaList>::default();
ap = VaList::new(args);

va_end is a no-op, va_arg translates to VaList::arg, and va_copy translates to cloning the VaList.

@nunoplopes nunoplopes merged commit fbb6592 into Cpp2Rust:master Apr 13, 2026
5 checks passed
@lucic71 lucic71 deleted the vaargs branch April 29, 2026 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants