Skip to content

Commit 92f9d3b

Browse files
committed
Account for cfg items in foreign mods.
Fixes #807
1 parent 49a387e commit 92f9d3b

File tree

11 files changed

+143
-1
lines changed

11 files changed

+143
-1
lines changed

src/bindgen/parser.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ impl Parse {
605605
return;
606606
}
607607

608+
let mod_cfg = Cfg::append(mod_cfg, Cfg::load(&item.attrs));
608609
for foreign_item in &item.items {
609610
if let syn::ForeignItem::Fn(ref function) = *foreign_item {
610611
if !config
@@ -618,7 +619,14 @@ impl Parse {
618619
return;
619620
}
620621
let path = Path::new(function.sig.ident.unraw().to_string());
621-
match Function::load(path, None, &function.sig, true, &function.attrs, mod_cfg) {
622+
match Function::load(
623+
path,
624+
None,
625+
&function.sig,
626+
true,
627+
&function.attrs,
628+
mod_cfg.as_ref(),
629+
) {
622630
Ok(func) => {
623631
info!("Take {}::{}.", crate_name, &function.sig.ident);
624632

tests/expectations/cfg.c

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ typedef struct {
7878
;
7979
} ConditionalField;
8080

81+
typedef struct {
82+
int32_t x;
83+
float y;
84+
} Normal;
85+
8186
#if (defined(PLATFORM_UNIX) && defined(X11))
8287
void root(FooHandle a, C c);
8388
#endif
@@ -87,3 +92,11 @@ void root(BarHandle a, C c);
8792
#endif
8893

8994
void cond(ConditionalField a);
95+
96+
#if defined(PLATFORM_WIN)
97+
extern int32_t foo(void);
98+
#endif
99+
100+
#if defined(PLATFORM_WIN)
101+
extern void bar(Normal a);
102+
#endif

tests/expectations/cfg.compat.c

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ typedef struct {
9696
;
9797
} ConditionalField;
9898

99+
typedef struct {
100+
int32_t x;
101+
float y;
102+
} Normal;
103+
99104
#ifdef __cplusplus
100105
extern "C" {
101106
#endif // __cplusplus
@@ -110,6 +115,14 @@ void root(BarHandle a, C c);
110115

111116
void cond(ConditionalField a);
112117

118+
#if defined(PLATFORM_WIN)
119+
extern int32_t foo(void);
120+
#endif
121+
122+
#if defined(PLATFORM_WIN)
123+
extern void bar(Normal a);
124+
#endif
125+
113126
#ifdef __cplusplus
114127
} // extern "C"
115128
#endif // __cplusplus

tests/expectations/cfg.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,20 @@ struct ConditionalField {
202202
;
203203
};
204204

205+
struct Normal {
206+
int32_t x;
207+
float y;
208+
209+
bool operator==(const Normal& other) const {
210+
return x == other.x &&
211+
y == other.y;
212+
}
213+
bool operator!=(const Normal& other) const {
214+
return x != other.x ||
215+
y != other.y;
216+
}
217+
};
218+
205219
extern "C" {
206220

207221
#if (defined(PLATFORM_UNIX) && defined(X11))
@@ -214,4 +228,12 @@ void root(BarHandle a, C c);
214228

215229
void cond(ConditionalField a);
216230

231+
#if defined(PLATFORM_WIN)
232+
extern int32_t foo();
233+
#endif
234+
235+
#if defined(PLATFORM_WIN)
236+
extern void bar(Normal a);
237+
#endif
238+
217239
} // extern "C"

tests/expectations/cfg.pyx

+10
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,20 @@ cdef extern from *:
5858
ctypedef struct ConditionalField:
5959
int32_t field;
6060

61+
ctypedef struct Normal:
62+
int32_t x;
63+
float y;
64+
6165
IF (PLATFORM_UNIX and X11):
6266
void root(FooHandle a, C c);
6367

6468
IF (PLATFORM_WIN or M_32):
6569
void root(BarHandle a, C c);
6670

6771
void cond(ConditionalField a);
72+
73+
IF PLATFORM_WIN:
74+
extern int32_t foo();
75+
76+
IF PLATFORM_WIN:
77+
extern void bar(Normal a);

tests/expectations/cfg_both.c

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ typedef struct ConditionalField {
7878
;
7979
} ConditionalField;
8080

81+
typedef struct Normal {
82+
int32_t x;
83+
float y;
84+
} Normal;
85+
8186
#if (defined(PLATFORM_UNIX) && defined(X11))
8287
void root(struct FooHandle a, union C c);
8388
#endif
@@ -87,3 +92,11 @@ void root(struct BarHandle a, union C c);
8792
#endif
8893

8994
void cond(struct ConditionalField a);
95+
96+
#if defined(PLATFORM_WIN)
97+
extern int32_t foo(void);
98+
#endif
99+
100+
#if defined(PLATFORM_WIN)
101+
extern void bar(struct Normal a);
102+
#endif

tests/expectations/cfg_both.compat.c

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ typedef struct ConditionalField {
9696
;
9797
} ConditionalField;
9898

99+
typedef struct Normal {
100+
int32_t x;
101+
float y;
102+
} Normal;
103+
99104
#ifdef __cplusplus
100105
extern "C" {
101106
#endif // __cplusplus
@@ -110,6 +115,14 @@ void root(struct BarHandle a, union C c);
110115

111116
void cond(struct ConditionalField a);
112117

118+
#if defined(PLATFORM_WIN)
119+
extern int32_t foo(void);
120+
#endif
121+
122+
#if defined(PLATFORM_WIN)
123+
extern void bar(struct Normal a);
124+
#endif
125+
113126
#ifdef __cplusplus
114127
} // extern "C"
115128
#endif // __cplusplus

tests/expectations/cfg_tag.c

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ struct ConditionalField {
7878
;
7979
};
8080

81+
struct Normal {
82+
int32_t x;
83+
float y;
84+
};
85+
8186
#if (defined(PLATFORM_UNIX) && defined(X11))
8287
void root(struct FooHandle a, union C c);
8388
#endif
@@ -87,3 +92,11 @@ void root(struct BarHandle a, union C c);
8792
#endif
8893

8994
void cond(struct ConditionalField a);
95+
96+
#if defined(PLATFORM_WIN)
97+
extern int32_t foo(void);
98+
#endif
99+
100+
#if defined(PLATFORM_WIN)
101+
extern void bar(struct Normal a);
102+
#endif

tests/expectations/cfg_tag.compat.c

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ struct ConditionalField {
9696
;
9797
};
9898

99+
struct Normal {
100+
int32_t x;
101+
float y;
102+
};
103+
99104
#ifdef __cplusplus
100105
extern "C" {
101106
#endif // __cplusplus
@@ -110,6 +115,14 @@ void root(struct BarHandle a, union C c);
110115

111116
void cond(struct ConditionalField a);
112117

118+
#if defined(PLATFORM_WIN)
119+
extern int32_t foo(void);
120+
#endif
121+
122+
#if defined(PLATFORM_WIN)
123+
extern void bar(struct Normal a);
124+
#endif
125+
113126
#ifdef __cplusplus
114127
} // extern "C"
115128
#endif // __cplusplus

tests/expectations/cfg_tag.pyx

+10
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,20 @@ cdef extern from *:
5858
cdef struct ConditionalField:
5959
int32_t field;
6060

61+
cdef struct Normal:
62+
int32_t x;
63+
float y;
64+
6165
IF (PLATFORM_UNIX and X11):
6266
void root(FooHandle a, C c);
6367

6468
IF (PLATFORM_WIN or M_32):
6569
void root(BarHandle a, C c);
6670

6771
void cond(ConditionalField a);
72+
73+
IF PLATFORM_WIN:
74+
extern int32_t foo();
75+
76+
IF PLATFORM_WIN:
77+
extern void bar(Normal a);

tests/rust/cfg.rs

+14
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,17 @@ pub extern "C" fn root(a: BarHandle, c: C)
6262
#[no_mangle]
6363
pub extern "C" fn cond(a: ConditionalField)
6464
{ }
65+
66+
// src/lib.rs
67+
#[repr(C)]
68+
struct Normal {
69+
x: i32,
70+
y: f32,
71+
}
72+
73+
#[cfg(windows)]
74+
extern "C" {
75+
fn foo() -> i32;
76+
77+
fn bar(a: Normal);
78+
}

0 commit comments

Comments
 (0)