-
Notifications
You must be signed in to change notification settings - Fork 360
Feature: add namespace attribute support #1123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tarnishablec
wants to merge
8
commits into
mozilla:main
Choose a base branch
from
tarnishablec:copilot/add-namespace-attribute-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature: add namespace attribute support #1123
tarnishablec
wants to merge
8
commits into
mozilla:main
from
tarnishablec:copilot/add-namespace-attribute-support
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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-macrocrate, 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
cbindgen-macrocrate, 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 bycbindgenfor header generation. [1] [2] [3]Parsing and IR enhancements
Functionstruct insrc/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
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
Testing