Skip to content

Commit c360261

Browse files
committed
Provide a list of tokens
If we cannot reproduce the original whitespace anyway, then we should let the callee decide what to do with the tokens, rather than smashing them together in a "safe" way.
1 parent 4db7b55 commit c360261

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

bindgen-integration/build.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl ParseCallbacks for MacroCallback {
6565
}
6666
}
6767

68-
fn func_macro(&self, name: &str, value: &[u8]) {
68+
fn func_macro(&self, name: &str, value: &[&[u8]]) {
6969
match name {
7070
"TESTMACRO_NONFUNCTIONAL" => {
7171
panic!("func_macro was called for a non-functional macro");
@@ -76,19 +76,29 @@ impl ParseCallbacks for MacroCallback {
7676
// change in the future, but it is safe by the definition of a
7777
// token in C, whereas leaving the spaces out could change
7878
// tokenization.
79-
assert_eq!(value, b"- TESTMACRO_INTEGER");
79+
assert_eq!(
80+
value,
81+
&["-".as_bytes(), "TESTMACRO_INTEGER".as_bytes()]
82+
);
8083
*self.seen_funcs.lock().unwrap() += 1;
8184
}
8285
"TESTMACRO_FUNCTIONAL_EMPTY(TESTMACRO_INTEGER)" => {
83-
assert_eq!(value, b"");
86+
assert_eq!(value, &[] as &[&[u8]]);
8487
*self.seen_funcs.lock().unwrap() += 1;
8588
}
8689
"TESTMACRO_FUNCTIONAL_TOKENIZED(a,b,c,d,e)" => {
87-
assert_eq!(value, b"a / b c d ## e");
90+
assert_eq!(
91+
value,
92+
["a", "/", "b", "c", "d", "##", "e"]
93+
.iter()
94+
.map(|s| s.as_bytes())
95+
.collect::<Vec<_>>()
96+
.as_slice()
97+
);
8898
*self.seen_funcs.lock().unwrap() += 1;
8999
}
90100
"TESTMACRO_FUNCTIONAL_SPLIT(a,b)" => {
91-
assert_eq!(value, b"b , a");
101+
assert_eq!(value, &[b"b", b",", b"a"]);
92102
*self.seen_funcs.lock().unwrap() += 1;
93103
}
94104
_ => {

src/callbacks.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
4545
///
4646
/// The first parameter represents the name and argument list (including the
4747
/// parentheses) of the function-like macro. The second parameter represents
48-
/// the expansion of the macro. It is not guaranteed that the whitespace of
49-
/// the original is preserved, but it is guaranteed that tokenization will
50-
/// not be changed.
51-
fn func_macro(&self, _name: &str, _value: &[u8]) {}
48+
/// the expansion of the macro as a sequence of tokens.
49+
fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}
5250

5351
/// This function should return whether, given an enum variant
5452
/// name, and value, this enum variant will forcibly be a constant.

src/ir/var.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn default_macro_constant_type(value: i64) -> IntKind {
137137
/// be infallible), then `Err(ParseError::Continue)` is returned as well.
138138
fn handle_function_macro(
139139
cursor: &clang::Cursor,
140-
func_macro: impl FnOnce(&str, &[u8]),
140+
func_macro: impl FnOnce(&str, &[&[u8]]),
141141
) -> Result<(), ParseError> {
142142
// cexpr explicitly does not handle function-like macros, except for a
143143
// degenerate case in which it behaves in a non-conformant way. We prevent
@@ -161,7 +161,7 @@ fn handle_function_macro(
161161
let (left, right) = joined.split_at(boundary);
162162
let left = String::from_utf8(left.concat())
163163
.map_err(|_| ParseError::Continue)?;
164-
let right = right.join(&b' ');
164+
let right: Vec<_> = right.iter().map(Vec::as_slice).collect();
165165
func_macro(&left, &right);
166166

167167
// We handled the macro, skip future macro processing.

0 commit comments

Comments
 (0)