parser: Introduce cbindgen:ignore comment annotation, to allow ignoring items or modules.

This commit is contained in:
Emilio Cobos Álvarez
2020-05-01 04:24:07 +02:00
parent f75d65ccb8
commit b04aa7e699
10 changed files with 121 additions and 13 deletions
+13 -1
View File
@@ -235,6 +235,19 @@ An annotation may be a bool, string (no quotes), or list of strings. If just the
Most annotations are just local overrides for identical settings in the cbindgen.toml, but a few are unique because they don't make sense in a global context. The set of supported annotation are as follows: Most annotations are just local overrides for identical settings in the cbindgen.toml, but a few are unique because they don't make sense in a global context. The set of supported annotation are as follows:
### Ignore annotation
cbindgen will automatically ignore any `#[test]` or `#[cfg(test)]` item it
finds. You can manually ignore other stuff with the `ignore` annotation
attribute:
```rust
pub mod my_interesting_mod;
/// cbindgen:ignore
pub mod my_uninteresting_mod; // This won't be scanned by cbindgen.
```
### Struct Annotations ### Struct Annotations
* field-names=\[field1, field2, ...\] -- sets the names of all the fields in the output struct. These names will be output verbatim, and are not eligible for renaming. * field-names=\[field1, field2, ...\] -- sets the names of all the fields in the output struct. These names will be output verbatim, and are not eligible for renaming.
@@ -335,7 +348,6 @@ Given configuration in the cbindgen.toml, `cbindgen` can generate these attribut
This is controlled by the `swift_name_macro` option in the cbindgen.toml. This is controlled by the `swift_name_macro` option in the cbindgen.toml.
## cbindgen.toml ## cbindgen.toml
Most configuration happens through your cbindgen.toml file. Every value has a default (that is usually reasonable), so you can start with an empty cbindgen.toml and tweak it until you like the output you're getting. Most configuration happens through your cbindgen.toml file. Every value has a default (that is usually reasonable), so you can start with an empty cbindgen.toml and tweak it until you like the output you're getting.
+26 -12
View File
@@ -71,26 +71,40 @@ impl SynItemFnHelpers for syn::ImplItemMethod {
} }
} }
/// Returns whether this attribute causes us to skip at item. This basically
/// checks for `#[cfg(test)]`, `#[test]`, `/// cbindgen::ignore` and
/// variations thereof.
fn is_skip_item_attr(attr: &syn::Meta) -> bool { fn is_skip_item_attr(attr: &syn::Meta) -> bool {
match *attr { match *attr {
syn::Meta::Path(ref path) => { syn::Meta::Path(ref path) => {
if path.is_ident("test") { // TODO(emilio): It'd be great if rustc allowed us to use a syntax
return true; // like `#[cbindgen::ignore]` or such.
} path.is_ident("test")
} }
syn::Meta::List(ref list) => { syn::Meta::List(ref list) => {
if list.path.is_ident("cfg") { if !list.path.is_ident("cfg") {
return list.nested.iter().any(|nested| match *nested { return false;
syn::NestedMeta::Meta(ref meta) => {
return is_skip_item_attr(meta);
}
syn::NestedMeta::Lit(..) => false,
});
} }
list.nested.iter().any(|nested| match *nested {
syn::NestedMeta::Meta(ref meta) => {
return is_skip_item_attr(meta);
}
syn::NestedMeta::Lit(..) => false,
})
}
syn::Meta::NameValue(ref name_value) => {
if name_value.path.is_ident("doc") {
if let syn::Lit::Str(ref content) = name_value.lit {
// FIXME(emilio): Maybe should use the general annotation
// mechanism, but it seems overkill for this.
if content.value().trim() == "cbindgen:ignore" {
return true;
}
}
}
false
} }
syn::Meta::NameValue(..) => {}
} }
false
} }
pub trait SynAttributeHelpers { pub trait SynAttributeHelpers {
+6
View File
@@ -0,0 +1,6 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void no_ignore_root(void);
+14
View File
@@ -0,0 +1,14 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void no_ignore_root(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+6
View File
@@ -0,0 +1,6 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void no_ignore_root(void);
+14
View File
@@ -0,0 +1,14 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void no_ignore_root(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+10
View File
@@ -0,0 +1,10 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>
extern "C" {
void no_ignore_root();
} // extern "C"
+6
View File
@@ -0,0 +1,6 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void no_ignore_root(void);
+14
View File
@@ -0,0 +1,14 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void no_ignore_root(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+12
View File
@@ -0,0 +1,12 @@
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn root() {}
/// cbindgen:ignore
///
/// Something else.
#[no_mangle]
pub extern "C" fn another_root() {}
#[no_mangle]
pub extern "C" fn no_ignore_root() {}