-
Notifications
You must be signed in to change notification settings - Fork 372
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22ed210
Initial plan
Copilot 5f99b24
Implement per-item namespace attribute support for C++ output
Copilot 0e00645
Fix: Use doc comment annotation syntax instead of attribute
Copilot 9d805f0
Add cbindgen-macro proc-macro crate for #[namespace] attribute
Copilot 521021d
Change attribute syntax from = to function-like parentheses
Copilot b211230
Add per-item C++ namespace support via cbindgen attribute
Copilot 2b9ef0f
Merge remote-tracking branch 'origin/copilot/add-namespace-attribute-…
tarnishablec f3e2b85
Add Cargo.lock files for Rust tests
tarnishablec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "cbindgen-macro" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| description = "Procedural macro attributes for cbindgen" | ||
| license = "MPL-2.0" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| proc-macro2 = "1.0" | ||
| quote = "1" | ||
| syn = { version = "2.0", features = ["full"] } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| //! Procedural macro attributes for cbindgen. | ||
| //! | ||
| //! This crate provides the `#[cbindgen_macro::namespace()]` attribute that can be used | ||
| //! to specify C++ namespaces for individual functions in generated headers. | ||
| //! | ||
| //! # Example | ||
| //! | ||
| //! ```rust,ignore | ||
| //! use cbindgen_macro::namespace; | ||
| //! | ||
| //! #[namespace("ffi::bar")] | ||
| //! #[no_mangle] | ||
| //! pub extern "C" fn foo() {} | ||
| //! ``` | ||
| //! | ||
| //! The attribute itself is a no-op at compile time - it simply passes through | ||
| //! the item unchanged. However, cbindgen parses this attribute from the source | ||
| //! code to determine the C++ namespace for the function in the generated header. | ||
|
|
||
| use proc_macro::TokenStream; | ||
|
|
||
| /// Specifies a C++ namespace for a function in cbindgen-generated headers. | ||
| /// | ||
| /// This attribute is a no-op at compile time but is parsed by cbindgen to | ||
| /// determine where to place the function declaration in the generated C++ header. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```rust,ignore | ||
| /// #[cbindgen_macro::namespace("ffi::bar")] | ||
| /// #[no_mangle] | ||
| /// pub extern "C" fn foo() {} | ||
| /// ``` | ||
| /// | ||
| /// This will generate the following C++ code: | ||
| /// | ||
| /// ```cpp | ||
| /// extern "C" { | ||
| /// | ||
| /// namespace ffi { | ||
| /// namespace bar { | ||
| /// | ||
| /// void foo(); | ||
| /// | ||
| /// } // namespace bar | ||
| /// } // namespace ffi | ||
| /// | ||
| /// } // extern "C" | ||
| /// ``` | ||
| #[proc_macro_attribute] | ||
| pub fn namespace(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
| // This is a no-op - we just pass through the item unchanged. | ||
| // cbindgen parses the attribute directly from the source code. | ||
| item | ||
| } | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| global_function; | ||
| ffi_function; | ||
| nested_function; | ||
| another_nested_function; | ||
| other_namespace_function; | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| uses_global_namespace; | ||
| uses_item_namespace; | ||
| also_uses_global_namespace; | ||
| }; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So generally we've been using
/// cbindgen:foocomments instead of macros. Any reason not to keep doing that? That allows the crate not to depend on anything at build time.