Skip to content

Handle macro redefinition a bit more graciously. #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2016
Merged
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
12 changes: 7 additions & 5 deletions src/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/expectations/tests/macro-redef.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


pub const FOO: ::std::os::raw::c_uint = 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, FOO would be 5 in the bindings. We want the last definition, not the first.

Also, file a follow up issue to track not generating bindings for macros that have been #undefed.

pub const BAR: ::std::os::raw::c_uint = 5;
pub const BAZ: ::std::os::raw::c_uint = 6;
5 changes: 5 additions & 0 deletions tests/headers/macro-redef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define FOO 4
#define BAR (1 + FOO)
#undef FOO
#define FOO 5
#define BAZ (1 + FOO)