Skip to content

Optional function argument is not exported correctly #1037

Description

@flaviojs

I stumbled upon a weird case... it's unlikely to appear in real code but rust supports it, so cbindgen should support it too.

example.rs

#[allow(unexpected_cfgs)]
#[no_mangle]
pub extern "C" fn foobar(a1: u32, #[cfg(feature = "baz")] a2: u32) -> u32 {
    #[cfg(feature = "baz")]
    {
        a1 + a2
    }
    #[cfg(not(feature = "baz"))]
    {
        a1
    }
}

cbindgen.toml

language = "C"
[defines]
"feature = baz" = "FEATURE_BAZ"

BAD example.h (generated)

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

uint32_t foobar(uint32_t a1, uint32_t a2);

What would be the preferred way to handle this case?

  • generate multiple function declarations, each surrounded by an appropriate ifdef
  • surround each optional argument with an appropriate ifdef
  • apply a macro call to the optional argument text
  • something else

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions