mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.37k stars 305 forks source link

cbindgen emits constants in spite of no-export and/or ignore annotations #947

Closed scovich closed 4 months ago

scovich commented 5 months ago

For the following rust code:

// cbindgen:ignore
// cbindgen:no-export
pub struct Foo;

// cbindgen:ignore
// cbindgen:no-export
impl Foo {
    // cbindgen:ignore
    // cbindgen:no-export
    pub const BAR: i64 = 42;
}

cbindgen still emits the following header code:

constexpr static const int64_t Foo_BAR = 42;

I haven't found any combination of annotations that can suppress it, even tho the docs suggest it should work?

For ignore:

You can manually ignore other stuff with the ignore annotation attribute

For no-export:

cbindgen will usually emit all items it finds, as instructed by the parse and export config sections. This annotation will make cbindgen skip this item from the output, while still being aware of it.

emilio commented 5 months ago

I think you need to use triple-dash, otherwise the comment isn't preserved by syn

emilio commented 5 months ago

Triple slash that is, sorry.

scovich commented 5 months ago

This still emits:

pub struct Foo;

impl Foo {
    /// cbindgen:ignore
    /// cbindgen:no-export
    pub const BAR: i64 = 42;
}

This suppresses it (along with everything else inside the impl Foo block):

pub struct Foo;

/// cbindgen:ignore
/// cbindgen:no-export
impl Foo {
    pub const BAR: i64 = 42;
}

So it still seems like the annotations don't affect constants -- only the surrounding struct impl?

scovich commented 5 months ago

Starting exploring in https://github.com/mozilla/cbindgen/pull/949

It turns out /// cbindgen:XXX annotations don't affect anything inside a struct impl block -- in fact, they're not even parsed (verified by inserting a panic! at the NameValue "doc" case of is_skip_item_attr)

scovich commented 5 months ago

I was able to find and add the missing should_skip_parsing calls (https://github.com/mozilla/cbindgen/pull/949), which fixes the cbindgen:ignore case. Still looking for the code that processes cbindgen:no-export, tho.

emilio commented 4 months ago

Fixed by #949.