diff --git a/src/ir/var.rs b/src/ir/var.rs index 0e7df61884..1c7b20280e 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -87,17 +87,19 @@ impl ClangSubItemParser for Var { assert!(!id.is_empty(), "Empty macro name?"); - if ctx.parsed_macro(&id) { - let name = String::from_utf8(id).unwrap(); - warn!("Duplicated macro definition: {}", name); - return Err(ParseError::Continue); - } + let previously_defined = ctx.parsed_macro(&id); // NB: It's important to "note" the macro even if the result is // not an integer, otherwise we might loose other kind of // derived macros. ctx.note_parsed_macro(id.clone(), value.clone()); + if previously_defined { + let name = String::from_utf8(id).unwrap(); + warn!("Duplicated macro definition: {}", name); + return Err(ParseError::Continue); + } + // NOTE: Unwrapping, here and above, is safe, because the // identifier of a token comes straight from clang, and we // enforce utf8 there, so we should have already panicked at diff --git a/tests/expectations/tests/macro-redef.rs b/tests/expectations/tests/macro-redef.rs new file mode 100644 index 0000000000..881a44ae87 --- /dev/null +++ b/tests/expectations/tests/macro-redef.rs @@ -0,0 +1,9 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +pub const FOO: ::std::os::raw::c_uint = 4; +pub const BAR: ::std::os::raw::c_uint = 5; +pub const BAZ: ::std::os::raw::c_uint = 6; diff --git a/tests/headers/macro-redef.h b/tests/headers/macro-redef.h new file mode 100644 index 0000000000..0180d2ab09 --- /dev/null +++ b/tests/headers/macro-redef.h @@ -0,0 +1,5 @@ +#define FOO 4 +#define BAR (1 + FOO) +#undef FOO +#define FOO 5 +#define BAZ (1 + FOO)