Skip to content

Handle re-declaration of a global variable. Fix #47 #63

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 2 commits into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 25 additions & 5 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ struct CodegenResult {
items: Vec<P<ast::Item>>,
saw_union: bool,
items_seen: HashSet<ItemId>,
/// The set of generated function names, needed because in C/C++ is legal to
/// The set of generated function/var names, needed because in C/C++ is legal to
/// do something like:
///
/// ```
/// extern "C" {
/// void foo();
/// extern int bar;
/// }
///
/// extern "C" {
/// void foo();
/// extern int bar;
/// }
/// ```
///
/// Being these two different declarations.
functions_seen: HashSet<String>,
vars_seen: HashSet<String>,
}

impl CodegenResult {
Expand All @@ -62,6 +65,7 @@ impl CodegenResult {
saw_union: false,
items_seen: Default::default(),
functions_seen: Default::default(),
vars_seen: Default::default(),
}
}

Expand All @@ -85,6 +89,14 @@ impl CodegenResult {
self.functions_seen.insert(name.into());
}

fn seen_var(&self, name: &str) -> bool {
self.vars_seen.contains(name)
}

fn saw_var(&mut self, name: &str) {
self.vars_seen.insert(name.into());
}

fn inner<F>(&mut self, cb: F) -> Vec<P<ast::Item>>
where F: FnOnce(&mut Self)
{
Expand Down Expand Up @@ -265,23 +277,31 @@ impl CodeGenerator for Var {
ctx: &BindgenContext,
result: &mut CodegenResult,
item: &Item) {
let name = item.canonical_name(ctx);
let canonical_name = item.canonical_name(ctx);

// TODO: Maybe warn here if there's a type mismatch
Copy link
Contributor

Choose a reason for hiding this comment

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

Clang already does it, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your comment.
Do you mean the TODO is not necessary?
I guess it would result in a compile error in the C or C++ code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, from what I remember bindgen prints clang warnings, so there is no need to do an other warning

if result.seen_var(&canonical_name) {
return;
}
result.saw_var(&canonical_name);

let ty = self.ty().to_rust_ty(ctx);

if let Some(val) = self.val() {
let const_item = aster::AstBuilder::new().item().pub_().const_(name)
let const_item = aster::AstBuilder::new().item().pub_()
.const_(canonical_name)
.expr().int(val).build(ty);
result.push(const_item)
} else {
let mut attrs = vec![];
if let Some(mangled) = self.mangled_name() {
attrs.push(attributes::link_name(mangled));
} else if name != self.name() {
} else if canonical_name != self.name() {
attrs.push(attributes::link_name(self.name()));
}

let item = ast::ForeignItem {
ident: ctx.rust_ident_raw(&name),
ident: ctx.rust_ident_raw(&canonical_name),
attrs: attrs,
node: ast::ForeignItemKind::Static(ty, !self.is_const()),
id: ast::DUMMY_NODE_ID,
Expand Down
10 changes: 10 additions & 0 deletions tests/expectations/decl_extern_int_twice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


extern "C" {
#[link_name = "foo"]
pub static mut foo: ::std::os::raw::c_int;
}
2 changes: 2 additions & 0 deletions tests/headers/decl_extern_int_twice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extern int foo;
extern int foo;