Skip to content

Conversation

@tarnishablec
Copy link

This pull request introduces support for per-function C++ namespace specification in generated headers using a new procedural macro attribute, improving flexibility for organizing C++ bindings. The main changes include the creation of a new cbindgen-macro crate, parsing and storing per-function namespace attributes, updating the C++ code generation logic to group functions into nested namespaces, and comprehensive documentation and tests to demonstrate the feature.

Procedural macro and attribute support

  • Added the new cbindgen-macro crate, which provides the #[cbindgen_macro::namespace("...")] procedural macro attribute for specifying C++ namespaces for individual functions. The macro is a no-op at compile time but is parsed by cbindgen for header generation. [1] [2] [3]

Parsing and IR enhancements

  • Updated the Rust IR (Function struct in src/bindgen/ir/function.rs) to store the per-function namespace parsed from the attribute, and added helper logic to extract this attribute from source code. [1] [2] [3] [4] [5]

C++ code generation changes

  • Modified the C++ backend (src/bindgen/language_backend/clike.rs) so that functions with per-item namespace attributes are grouped and emitted within nested namespace blocks in generated headers. Functions without the attribute use the global or no namespace as before. [1] [2] [3]

Documentation

  • Added documentation explaining how to use the new attribute, including examples and notes on its precedence and behavior with global namespaces.

Testing

  • Added and updated tests and expectations to cover the new feature, including precedence between global and per-item namespaces, and output for C, C++, and Cython bindings. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

Copilot AI and others added 8 commits November 25, 2025 14:22
Add support for `#[cbindgen::namespace = "..."]` attribute on functions to
specify C++ namespaces. Features:
- Parse attribute using `::` separator (e.g., "ffi::bar")
- Group functions by namespace in C++ output
- Emit nested namespace blocks inside extern "C"
- Ignore attribute for C and Cython output
- Add tests and documentation

Co-authored-by: tarnishablec <42984497+tarnishablec@users.noreply.github.com>
Changed from `#[cbindgen::namespace = "..."]` attribute syntax to
`/// cbindgen:namespace=...` doc comment annotation syntax.

The attribute syntax requires cbindgen to export a proc-macro, which
it doesn't. The doc comment annotation is the existing cbindgen way
to specify per-item configuration and doesn't cause Rust compilation
errors.

Example usage:
```rust
/// cbindgen:namespace=ffi::bar
#[no_mangle]
pub extern "C" fn foo(a: *const c_char) {}
```

Co-authored-by: tarnishablec <42984497+tarnishablec@users.noreply.github.com>
Created a separate proc-macro crate that provides the `namespace`
attribute. This allows users to use the Rust attribute syntax:

```rust
#[cbindgen_macro::namespace = "ffi::bar"]
#[no_mangle]
pub extern "C" fn foo() {}
```

The attribute is a no-op at compile time but is parsed by cbindgen
to determine the C++ namespace for the generated header.

Changes:
- Created cbindgen-macro crate with `namespace` proc-macro attribute
- Updated utilities.rs to accept both `cbindgen::namespace` and
  `cbindgen_macro::namespace` attribute paths
- Updated tests to use `cbindgen_macro::namespace` syntax
- Updated documentation with usage instructions

Co-authored-by: tarnishablec <42984497+tarnishablec@users.noreply.github.com>
Changed the namespace attribute syntax from:
  #[cbindgen_macro::namespace = "ffi::bar"]
to:
  #[cbindgen_macro::namespace("ffi::bar")]

This change follows the function-like attribute syntax preferred by
the user.

Updated:
- cbindgen-macro proc-macro crate
- utilities.rs parsing logic to handle Meta::List instead of Meta::NameValue
- Test files to use the new syntax
- Documentation with the new syntax examples

Co-authored-by: tarnishablec <42984497+tarnishablec@users.noreply.github.com>
Introduce a cbindgen-macro procedural macro crate and a #[cbindgen_macro::namespace("...")] attribute to specify C++ namespaces on individual extern "C" functions. Extend the internal function model to store optional namespace paths and update the C++ backend to group functions by namespace and emit nested namespace blocks inside the extern "C" region.

The new per-item namespace attribute takes precedence over the existing global namespace setting while preserving behavior for code that does not use the attribute. Add integration tests and documentation updates to cover both ffi:bar and ffi::bar style separators, verify precedence with global configuration, and ensure C output remains unchanged.

Co-authored-by: tarnishablec <42984497+tarnishablec@users.noreply.github.com>
…support' into copilot/add-namespace-attribute-support
@tarnishablec tarnishablec changed the title Copilot/add namespace attribute support Feature/add namespace attribute support Nov 26, 2025
@tarnishablec tarnishablec changed the title Feature/add namespace attribute support Feature: add namespace attribute support Nov 26, 2025
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.

1 participant