Skip to content
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

Cease to modify bindgen output. #1456

Merged
merged 10 commits into from
Feb 26, 2025
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
39 changes: 37 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions book/src/tutorial.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tutorial

If you're here, you want to call some C++ from Rust, right?
If you're here, you want to call some C++ from Rust, right? (If you want to do things the other way round, read the [Rust calls](rust_calls.md) page.)

Let's assume you're calling into some _existing_ C++ code.

Expand All @@ -17,8 +17,6 @@ The rest of this 'getting started' section assumes Cargo - if you're using somet

First, add `autocxx` *and `cxx`* to your `dependencies` and `autocxx-build` to your `build-dependencies` in your `Cargo.toml`. **You must specify both.**



```toml
[dependencies]
autocxx = "0.28.0"
Expand Down
5 changes: 5 additions & 0 deletions demo/src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
#include <stdint.h>
#include <string>

/// A C++ goat.
class Goat {
public:
/// Goat constructor.
Goat() : horns(0) {}
/// Add a horn to the goat.
void add_a_horn();
/// Describe the goat.
std::string describe() const;
private:
uint32_t horns;
};

/// Do some maths!
inline uint32_t DoMath(uint32_t a) {
return a * 3;
}
Expand Down
5 changes: 3 additions & 2 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ log = "0.4"
proc-macro2 = "1.0.11"
quote = "1.0"
indoc = "1.0"
autocxx-bindgen = { version = "=0.72.0", default-features = false, features = ["logging", "which-rustfmt"] }
#autocxx-bindgen = { git = "/~https://github.com/adetaylor/rust-bindgen", branch = "switch-from-attributes-to-callbacks", default-features = false, features = ["logging", "which-rustfmt"] }
autocxx-bindgen = { version = "=0.73.0", default-features = false, features = ["logging", "which-rustfmt"] }
#autocxx-bindgen = { git = "/~https://github.com/adetaylor/rust-bindgen", branch = "all-modules-raw-line", default-features = false, features = ["logging", "which-rustfmt"] }
itertools = "0.10.3"
cc = { version = "1.0", optional = true }
# Note: Keep the patch-level version of cxx-gen and cxx in sync.
Expand All @@ -47,6 +47,7 @@ serde_json = { version = "1.0", optional = true }
miette = "5"
thiserror = "1"
regex = "1.5"
regex_static = "0.1"
indexmap = "1.8"
prettyplease = { version = "0.2.6", features = ["verbatim"] }
rustversion = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion engine/src/conversion/analysis/constructor_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn decorate_struct(
) -> Result<Box<dyn Iterator<Item = Api<FnPhase>>>, ConvertErrorWithContext> {
let pod = fn_struct.pod;
let is_abstract = matches!(pod.kind, TypeKind::Abstract);
let constructor_and_allocator_deps = if is_abstract || pod.is_generic {
let constructor_and_allocator_deps = if is_abstract || pod.num_generics > 0 {
Vec::new()
} else {
constructors_and_allocators_by_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub(super) fn find_constructors_present(
PodAnalysis {
bases,
field_info,
is_generic: false,
num_generics: 0usize,
in_anonymous_namespace: false,
..
},
Expand Down
11 changes: 4 additions & 7 deletions engine/src/conversion/analysis/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,9 @@ impl<'a> FnAnalyzer<'a> {
apis.iter()
.filter_map(|api| match api {
Api::Struct {
analysis:
PodAnalysis {
is_generic: true, ..
},
analysis: PodAnalysis { num_generics, .. },
..
} => Some(api.name().clone()),
} if *num_generics > 0 => Some(api.name().clone()),
_ => None,
})
.collect()
Expand Down Expand Up @@ -2224,11 +2221,11 @@ fn constructor_with_suffix<'a>(rust_name: &'a str, nested_type_ident: &str) -> O
impl Api<FnPhase> {
pub(crate) fn name_for_allowlist(&self) -> QualifiedName {
match &self {
Api::Function { analysis, .. } => match analysis.kind {
Api::Function { fun, analysis, .. } => match analysis.kind {
FnKind::Method { ref impl_for, .. } => impl_for.clone(),
FnKind::TraitMethod { ref impl_for, .. } => impl_for.clone(),
FnKind::Function => {
QualifiedName::new(self.name().get_namespace(), make_ident(&analysis.rust_name))
QualifiedName::new(self.name().get_namespace(), fun.ident.clone())
}
},
Api::RustSubclassFn { subclass, .. } => subclass.0.name.clone(),
Expand Down
2 changes: 1 addition & 1 deletion engine/src/conversion/analysis/pod/byvalue_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl ByValueChecker {
}
_ => None,
},
TypedefKind::Use(_, ref ty) => match **ty {
TypedefKind::Use(ref ty) => match **ty {
crate::minisyn::Type(Type::Path(ref typ)) => {
let target_tn = QualifiedName::from_type_path(typ);
known_types().consider_substitution(&target_tn)
Expand Down
6 changes: 3 additions & 3 deletions engine/src/conversion/analysis/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) struct PodAnalysis {
/// std::unique_ptr<A> it would just be std::unique_ptr.
pub(crate) field_definition_deps: HashSet<QualifiedName>,
pub(crate) field_info: Vec<FieldInfo>,
pub(crate) is_generic: bool,
pub(crate) num_generics: usize,
pub(crate) in_anonymous_namespace: bool,
}

Expand Down Expand Up @@ -188,7 +188,7 @@ fn analyze_struct(
.filter(|base| config.is_on_allowlist(&base.to_cpp_name()))
.cloned()
.collect();
let is_generic = !details.item.generics.params.is_empty();
let num_generics = details.item.generics.params.len();
let in_anonymous_namespace = name
.name
.ns_segment_iter()
Expand All @@ -203,7 +203,7 @@ fn analyze_struct(
field_deps,
field_definition_deps,
field_info,
is_generic,
num_generics,
in_anonymous_namespace,
},
})))
Expand Down
22 changes: 17 additions & 5 deletions engine/src/conversion/analysis/type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl<'a> TypeConverter<'a> {
) -> Result<Annotated<Type>, ConvertErrorFromCpp> {
// First we try to spot if these are the special marker paths that
// bindgen uses to denote references or other things.
// TODO the next two lines can be removed
if let Some(ty) = unwrap_has_unused_template_param(&typ) {
self.convert_type(ty.clone(), ns, ctx)
} else if let Some(ty) = unwrap_has_opaque(&typ) {
Expand Down Expand Up @@ -277,7 +278,8 @@ impl<'a> TypeConverter<'a> {
ctx: &TypeConversionContext,
) -> Result<Annotated<Type>, ConvertErrorFromCpp> {
// First, qualify any unqualified paths.
if typ.path.segments.iter().next().unwrap().ident != "root" {
let first_seg = &typ.path.segments.iter().next().unwrap().ident;
if first_seg != "root" && first_seg != "output" {
let ty = QualifiedName::from_type_path(&typ);
// If the type looks like it is unqualified, check we know it
// already, and if not, qualify it according to the current
Expand All @@ -289,7 +291,7 @@ impl<'a> TypeConverter<'a> {
return Err(ConvertErrorFromCpp::UnsupportedBuiltInType(ty));
}
if !self.types_found.contains(&ty) {
typ.path.segments = std::iter::once("root")
typ.path.segments = std::iter::once("output")
.chain(ns.iter())
.map(|s| {
let i = make_ident(s);
Expand All @@ -313,7 +315,7 @@ impl<'a> TypeConverter<'a> {
// Now convert this type itself.
deps.insert(original_tn.clone());
// First let's see if this is a typedef.
let (typ, tn) = match self.resolve_typedef(&original_tn)? {
let (mut typ, tn) = match self.resolve_typedef(&original_tn)? {
None => (typ, original_tn),
Some(Type::Path(resolved_tp)) => {
let resolved_tn = QualifiedName::from_type_path(resolved_tp);
Expand Down Expand Up @@ -350,7 +352,17 @@ impl<'a> TypeConverter<'a> {
}
substitute_type
}
None => typ,
None => {
// Pass through as-is, but replacing `root` with `output`
// in the first path element. We don't just create a whole
// new `TypePath` because that would discard any generics.
if let Some(first_seg) = typ.path.segments.get_mut(0) {
if first_seg.ident == "root" {
first_seg.ident = make_ident("output").0;
}
}
typ
}
};

let mut extra_apis = ApiVec::new();
Expand Down Expand Up @@ -726,7 +738,7 @@ impl TypedefTarget for TypedefAnalysis {
fn get_target(&self) -> Option<&Type> {
Some(match self.kind {
TypedefKind::Type(ref ty) => &ty.ty,
TypedefKind::Use(_, ref ty) => ty,
TypedefKind::Use(ref ty) => ty,
})
}
}
Expand Down
9 changes: 4 additions & 5 deletions engine/src/conversion/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use autocxx_bindgen::callbacks::{Explicitness, Layout, SpecialMemberKind, Virtualness};
use autocxx_bindgen::callbacks::{Explicitness, SpecialMemberKind, Virtualness};

use syn::{
punctuated::Punctuated,
Expand All @@ -16,8 +16,8 @@ use syn::{
use crate::types::{make_ident, Namespace, QualifiedName};
use crate::{
minisyn::{
Attribute, FnArg, Ident, ItemConst, ItemEnum, ItemStruct, ItemType, ItemUse, Pat,
ReturnType, Type, Visibility,
Attribute, FnArg, Ident, ItemConst, ItemEnum, ItemStruct, ItemType, Pat, ReturnType, Type,
Visibility,
},
parse_callbacks::CppOriginalName,
};
Expand Down Expand Up @@ -51,7 +51,6 @@ pub(crate) enum TypeKind {
#[derive(Debug)]
pub(crate) struct StructDetails {
pub(crate) item: ItemStruct,
pub(crate) layout: Option<Layout>,
pub(crate) has_rvalue_reference_fields: bool,
}

Expand Down Expand Up @@ -204,7 +203,7 @@ impl AnalysisPhase for NullPhase {

#[derive(Clone, Debug)]
pub(crate) enum TypedefKind {
Use(ItemUse, Box<Type>),
Use(Box<Type>),
Type(ItemType),
}

Expand Down
Loading
Loading