Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ exclude = ["Bad"]
# default: no prefix is added
prefix = "CAPI_"

# If specific items should not get above prefix, they can be listed here
# default: []
prefix_excludes = ["ItemWithoutPrefix"]

# Types of items that we'll generate. If empty, then all types of item are emitted.
#
# possible items: (TODO: explain these in detail)
Expand Down
9 changes: 9 additions & 0 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ impl Builder {
self
}

#[allow(unused)]
pub fn exclude_item_prefix<S: AsRef<str>>(mut self, item_name: S) -> Builder {
self.config
.export
.prefix_excludes
.push(String::from(item_name.as_ref()));
self
}

#[allow(unused)]
pub fn with_parse_deps(mut self, parse_deps: bool) -> Builder {
self.config.parse.parse_deps = parse_deps;
Expand Down
6 changes: 5 additions & 1 deletion src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ pub struct ExportConfig {
pub body: HashMap<String, String>,
/// A prefix to add before the name of every item
pub prefix: Option<String>,
/// A list of items not to add a prefix to (should one be specified).
pub prefix_excludes: Vec<String>,
/// Types of items to generate.
pub item_types: Vec<ItemType>,
/// Whether renaming overrides or extends prefixing.
Expand Down Expand Up @@ -376,7 +378,9 @@ impl ExportConfig {
}
}
if let Some(ref prefix) = self.prefix {
item_name.insert_str(0, prefix);
if !self.prefix_excludes.contains(&item_name) {
item_name.insert_str(0, prefix);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations-symbols/prefix_exclude.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
caller;
};
18 changes: 18 additions & 0 deletions tests/expectations/prefix_exclude.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct {
uintptr_t id;
} Struct1;

typedef struct {
uintptr_t id;
} PREFIX_Struct2;

typedef int32_t PREFIX_Type1[3];

typedef int32_t Type2[15];

void caller(Struct1 s1, PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);
26 changes: 26 additions & 0 deletions tests/expectations/prefix_exclude.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct {
uintptr_t id;
} Struct1;

typedef struct {
uintptr_t id;
} PREFIX_Struct2;

typedef int32_t PREFIX_Type1[3];

typedef int32_t Type2[15];

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void caller(Struct1 s1, PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
23 changes: 23 additions & 0 deletions tests/expectations/prefix_exclude.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

struct Struct1 {
uintptr_t id;
};

struct PREFIX_Struct2 {
uintptr_t id;
};

using PREFIX_Type1 = int32_t[3];

using Type2 = int32_t[15];

extern "C" {

void caller(Struct1 s1, PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);

} // extern "C"
19 changes: 19 additions & 0 deletions tests/expectations/prefix_exclude.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

ctypedef struct Struct1:
uintptr_t id;

ctypedef struct PREFIX_Struct2:
uintptr_t id;

ctypedef int32_t PREFIX_Type1[3];

ctypedef int32_t Type2[15];

void caller(Struct1 s1, PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);
18 changes: 18 additions & 0 deletions tests/expectations/prefix_exclude_both.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Struct1 {
uintptr_t id;
} Struct1;

typedef struct PREFIX_Struct2 {
uintptr_t id;
} PREFIX_Struct2;

typedef int32_t PREFIX_Type1[3];

typedef int32_t Type2[15];

void caller(struct Struct1 s1, struct PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);
26 changes: 26 additions & 0 deletions tests/expectations/prefix_exclude_both.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Struct1 {
uintptr_t id;
} Struct1;

typedef struct PREFIX_Struct2 {
uintptr_t id;
} PREFIX_Struct2;

typedef int32_t PREFIX_Type1[3];

typedef int32_t Type2[15];

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void caller(struct Struct1 s1, struct PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
18 changes: 18 additions & 0 deletions tests/expectations/prefix_exclude_tag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

struct Struct1 {
uintptr_t id;
};

struct PREFIX_Struct2 {
uintptr_t id;
};

typedef int32_t PREFIX_Type1[3];

typedef int32_t Type2[15];

void caller(struct Struct1 s1, struct PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);
26 changes: 26 additions & 0 deletions tests/expectations/prefix_exclude_tag.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

struct Struct1 {
uintptr_t id;
};

struct PREFIX_Struct2 {
uintptr_t id;
};

typedef int32_t PREFIX_Type1[3];

typedef int32_t Type2[15];

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void caller(struct Struct1 s1, struct PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
19 changes: 19 additions & 0 deletions tests/expectations/prefix_exclude_tag.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

cdef struct Struct1:
uintptr_t id;

cdef struct PREFIX_Struct2:
uintptr_t id;

ctypedef int32_t PREFIX_Type1[3];

ctypedef int32_t Type2[15];

void caller(Struct1 s1, PREFIX_Struct2 s2, PREFIX_Type1 t1, Type2 t2);
15 changes: 15 additions & 0 deletions tests/rust/prefix_exclude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub type Type1 = [i32; 3];
pub type Type2 = [i32; 15];

#[repr(C)]
pub struct Struct1 {
id: usize,
}

#[repr(C)]
pub struct Struct2 {
id: usize,
}

#[no_mangle]
pub extern "C" fn caller(s1: Struct1, s2: Struct2, t1: Type1, t2: Type2) {}
3 changes: 3 additions & 0 deletions tests/rust/prefix_exclude.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[export]
prefix = "PREFIX_"
prefix_excludes = ["Type2", "Struct1"]