Skip to content

report constants and variables in ParseCallbacks::new_item_found #3249

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ typedef enum NamedEnum AliasOfNamedEnum;
// Functions

void named_function();

// Constants

const int MOD = 998244353;

// Variable

const char* name;
76 changes: 76 additions & 0 deletions bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ fn test_item_discovery_callback_c() {
553,
),
),
(
DiscoveredItemId::new(30),
ItemExpectations::new(
DiscoveredItem::Constant {
final_name: "MOD".to_string(),
},
36,
11,
596,
),
),
(
DiscoveredItemId::new(34),
ItemExpectations::new(
DiscoveredItem::Variable {
final_name: "name".to_string(),
},
40,
13,
639,
),
),
]);
test_item_discovery_callback(
"/tests/parse_callbacks/item_discovery_callback/header_item_discovery.h", &expected);
Expand Down Expand Up @@ -280,6 +302,12 @@ fn compare_item_info(
DiscoveredItem::Method { .. } => {
compare_method_info(&expected_item.item, &generated_item.0)
}
DiscoveredItem::Constant { .. } => {
compare_constant_info(&expected_item.item, &generated_item.0)
}
DiscoveredItem::Variable { .. } => {
compare_variable_info(&expected_item.item, &generated_item.0)
}
};

if is_a_match {
Expand Down Expand Up @@ -508,3 +536,51 @@ pub fn compare_method_info(
}
true
}

pub fn compare_constant_info(
expected_item: &DiscoveredItem,
generated_item: &DiscoveredItem,
) -> bool {
let DiscoveredItem::Constant {
final_name: expected_final_name,
} = expected_item
else {
unreachable!()
};

let DiscoveredItem::Constant {
final_name: generated_final_name,
} = generated_item
else {
unreachable!()
};

if !compare_names(expected_final_name, generated_final_name) {
return false;
}
true
}

pub fn compare_variable_info(
expected_item: &DiscoveredItem,
generated_item: &DiscoveredItem,
) -> bool {
let DiscoveredItem::Variable {
final_name: expected_final_name,
} = expected_item
else {
unreachable!()
};

let DiscoveredItem::Variable {
final_name: generated_final_name,
} = generated_item
else {
unreachable!()
};

if !compare_names(expected_final_name, generated_final_name) {
return false;
}
true
}
12 changes: 12 additions & 0 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ pub enum DiscoveredItem {
/// Type to which this method belongs.
parent: DiscoveredItemId,
}, // modules, etc.

/// A constant.
Constant {
/// The final name of the generated binding
final_name: String,
},

/// A variable.
Variable {
/// The final name of the generated binding
final_name: String,
},
}

/// Relevant information about a type to which new derive attributes will be added using
Expand Down
12 changes: 12 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,12 @@ impl CodeGenerator for Var {
let ty = var_ty.to_rust_ty_or_opaque(ctx, &());

if let Some(val) = self.val() {
utils::call_discovered_item_callback(ctx, item, || {
DiscoveredItem::Constant {
final_name: canonical_name.clone(),
}
});

match *val {
VarType::Bool(val) => {
result.push(quote! {
Expand Down Expand Up @@ -778,6 +784,12 @@ impl CodeGenerator for Var {
}
}
} else {
utils::call_discovered_item_callback(ctx, item, || {
DiscoveredItem::Variable {
final_name: canonical_name.clone(),
}
});

let symbol: &str = self.link_name().unwrap_or_else(|| {
let link_name =
self.mangled_name().unwrap_or_else(|| self.name());
Expand Down
Loading