Skip to content

Commit 965a13c

Browse files
igeladoemilio
authored andcommitted
Add test
1 parent b9c6984 commit 965a13c

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

bindgen-integration/build.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate bindgen;
22

33
use bindgen::callbacks::{
4-
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
4+
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks, Token, TokenKind,
55
};
66
use bindgen::{Builder, EnumVariation, Formatter};
77
use std::collections::HashSet;
@@ -44,9 +44,9 @@ impl ParseCallbacks for MacroCallback {
4444
assert_eq!(value, b"string");
4545
*self.seen_hellos.lock().unwrap() += 1;
4646
}
47-
"TESTMACRO_STRING_EXPANDED" |
48-
"TESTMACRO_STRING" |
49-
"TESTMACRO_INTEGER" => {
47+
"TESTMACRO_STRING_EXPANDED"
48+
| "TESTMACRO_STRING"
49+
| "TESTMACRO_INTEGER" => {
5050
// The integer test macro is, actually, not expected to show up here at all -- but
5151
// should produce an error if it does.
5252
assert_eq!(
@@ -145,6 +145,48 @@ impl ParseCallbacks for MacroCallback {
145145
vec![]
146146
}
147147
}
148+
149+
fn modify_macro(&self, _name: &str, tokens: &mut Vec<Token>) {
150+
// Handle macros dealing with bit positions of the format HI:LO
151+
if tokens.len() == 4 && tokens[2].kind == TokenKind::Punctuation {
152+
if let Ok(colon) = std::str::from_utf8(&tokens[2].raw) {
153+
if colon != ":" {
154+
return;
155+
}
156+
let high = match std::str::from_utf8(&tokens[1].raw) {
157+
Ok(s) => {
158+
if let Ok(val) = s.parse::<u16>() {
159+
val
160+
} else {
161+
return;
162+
}
163+
}
164+
Err(_) => {
165+
return;
166+
}
167+
};
168+
169+
let low = match std::str::from_utf8(&tokens[3].raw) {
170+
Ok(s) => {
171+
if let Ok(val) = s.parse::<u16>() {
172+
val
173+
} else {
174+
return;
175+
}
176+
}
177+
Err(_) => {
178+
return;
179+
}
180+
};
181+
let value: u32 = ((high as u32) << 16) | low as u32;
182+
tokens[1] = Token::from((
183+
TokenKind::Literal,
184+
value.to_string().as_bytes(),
185+
));
186+
tokens.truncate(2);
187+
}
188+
}
189+
}
148190
}
149191

150192
impl Drop for MacroCallback {

bindgen-integration/cpp/Test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define TESTMACRO_STRING_EXPR ("string")
2323
#define TESTMACRO_STRING_FUNC_NON_UTF8(x) (x "ÿÿ") /* invalid UTF-8 on purpose */
2424

25+
#define TESTMACRO_COLON_VALUE 1:2
26+
2527
enum {
2628
MY_ANNOYING_MACRO =
2729
#define MY_ANNOYING_MACRO 1

bindgen-integration/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,9 @@ fn test_wrap_static_fns() {
345345
extern_bindings::wrap_as_variadic_fn2_wrapped(1, 2);
346346
}
347347
}
348+
349+
#[test]
350+
fn test_colon_define() {
351+
let gold: u32 = (1u32 << 16) | 2;
352+
assert_eq!(gold, bindings::TESTMACRO_COLON_VALUE);
353+
}

0 commit comments

Comments
 (0)