Skip to content

Commit fc0f896

Browse files
committed
Manual fixups, some of them pretty lame, and don't let rustfmt rewrap comments.
1 parent 7baa9ac commit fc0f896

File tree

8 files changed

+325
-241
lines changed

8 files changed

+325
-241
lines changed

rustfmt.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
max_width = 80
22
format_strings = false
3-
wrap_comments = true
43
fn_brace_style = "SameLineWhere"
54
item_brace_style = "SameLineWhere"
65
struct_lit_multiline_style = "ForceMulti"

src/clang.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,12 @@ pub fn kind_to_str(x: Enum_CXCursorKind) -> &'static str {
12591259
CXCursor_TemplateTemplateParameter => "TemplateTemplateParameter",
12601260
CXCursor_FunctionTemplate => "FunctionTemplate",
12611261
CXCursor_ClassTemplate => "ClassTemplate",
1262-
CXCursor_ClassTemplatePartialSpecialization => "ClassTemplatePartialSpecialization",
1262+
CXCursor_ClassTemplatePartialSpecialization => {
1263+
// FIXME: Ugly hack for rustfmt, should go away!
1264+
//
1265+
// I plan to convert this into an enum right away anyway, though.
1266+
return "ClassTemplatePartialSpecialization";
1267+
}
12631268
CXCursor_NamespaceAlias => "NamespaceAlias",
12641269
CXCursor_UsingDirective => "UsingDirective",
12651270
CXCursor_UsingDeclaration => "UsingDeclaration",

src/codegen/mod.rs

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
mod helpers;
22

3+
34
use aster;
5+
46
use ir::annotations::FieldAccessorKind;
57
use ir::comp::{CompInfo, CompKind, Field, Method};
6-
78
use ir::context::BindgenContext;
89
use ir::enum_ty::Enum;
910
use ir::function::{Function, FunctionSig};
@@ -16,13 +17,12 @@ use ir::ty::{Type, TypeKind};
1617
use ir::type_collector::{ItemSet, TypeCollector};
1718
use ir::var::Var;
1819
use self::helpers::{BlobTyBuilder, attributes};
20+
1921
use std::borrow::Cow;
2022
use std::collections::HashSet;
2123
use std::collections::hash_map::{Entry, HashMap};
2224
use std::mem;
23-
2425
use std::ops;
25-
2626
use syntax::abi::Abi;
2727
use syntax::ast;
2828
use syntax::codemap::{Span, respan};
@@ -40,8 +40,7 @@ struct CodegenResult {
4040
saw_union: bool,
4141
items_seen: HashSet<ItemId>,
4242
/// The set of generated function/var names, needed because in C/C++ is
43-
/// legal to
44-
/// do something like:
43+
/// legal to do something like:
4544
///
4645
/// ```c++
4746
/// extern "C" {
@@ -400,7 +399,8 @@ impl CodeGenerator for Type {
400399
if template_arg.is_named() {
401400
let name = template_arg.name().unwrap();
402401
if name.contains("typename ") {
403-
error!("Item contained `typename`'d template param: {:?}", item);
402+
error!("Item contained `typename`'d template \
403+
parameter: {:?}", item);
404404
return;
405405
}
406406
generics =
@@ -541,18 +541,25 @@ impl<'a> Bitfield<'a> {
541541
pub fn $getter_name(&self) -> $field_type {
542542
unsafe {
543543
::std::mem::transmute(
544-
((self.$field_ident & ($mask as $bitfield_type)) >> $offset)
545-
as $int_type)
544+
(
545+
(self.$field_ident &
546+
($mask as $bitfield_type))
547+
>> $offset
548+
) as $int_type
549+
)
546550
}
547551
}
548552

549553
#[inline]
550554
pub fn $setter_name(&mut self, val: $field_type) {
551555
self.$field_ident &= !($mask as $bitfield_type);
552-
self.$field_ident |= (val as $int_type as $bitfield_type << $offset) & ($mask as $bitfield_type);
556+
self.$field_ident |=
557+
(val as $int_type as $bitfield_type << $offset) &
558+
($mask as $bitfield_type);
553559
}
554560
}
555-
).unwrap();
561+
)
562+
.unwrap();
556563

557564
let items = match item.unwrap().node {
558565
ast::ItemKind::Impl(_, _, _, _, _, items) => items,
@@ -639,6 +646,7 @@ impl CodeGenerator for CompInfo {
639646
};
640647

641648
// Generate the vtable from the method list if appropriate.
649+
//
642650
// TODO: I don't know how this could play with virtual methods that are
643651
// not in the list of methods found by us, we'll see. Also, could the
644652
// order of the vtable pointers vary?
@@ -677,8 +685,9 @@ impl CodeGenerator for CompInfo {
677685
continue;
678686
}
679687

680-
for (i, ty) in applicable_template_args.iter().enumerate() {
681-
if base_ty.signature_contains_named_type(ctx, ctx.resolve_type(*ty)) {
688+
for (i, ty_id) in applicable_template_args.iter().enumerate() {
689+
let template_arg_ty = ctx.resolve_type(*ty_id);
690+
if base_ty.signature_contains_named_type(ctx, template_arg_ty) {
682691
template_args_used[i] = true;
683692
}
684693
}
@@ -758,8 +767,9 @@ impl CodeGenerator for CompInfo {
758767
continue;
759768
}
760769

761-
for (i, ty) in applicable_template_args.iter().enumerate() {
762-
if field_ty.signature_contains_named_type(ctx, ctx.resolve_type(*ty)) {
770+
for (i, ty_id) in applicable_template_args.iter().enumerate() {
771+
let template_arg = ctx.resolve_type(*ty_id);
772+
if field_ty.signature_contains_named_type(ctx, template_arg) {
763773
template_args_used[i] = true;
764774
}
765775
}
@@ -841,7 +851,8 @@ impl CodeGenerator for CompInfo {
841851
}
842852

843853
#[inline]
844-
pub unsafe fn $mutable_getter_name(&mut self) -> &mut $ty {
854+
pub unsafe fn $mutable_getter_name(&mut self)
855+
-> &mut $ty {
845856
&mut self.$field_name
846857
}
847858
}
@@ -931,9 +942,12 @@ impl CodeGenerator for CompInfo {
931942
if !template_args_used[i] {
932943
let name = ctx.resolve_type(*ty).name().unwrap();
933944
let ident = ctx.rust_ident(name);
945+
let phantom = quote_ty!(ctx.ext_cx(),
946+
::std::marker::PhantomData<$ident>);
934947
let field =
935-
StructFieldBuilder::named(format!("_phantom_{}", i)).pub_()
936-
.build_ty(quote_ty!(ctx.ext_cx(), ::std::marker::PhantomData<$ident>));
948+
StructFieldBuilder::named(format!("_phantom_{}", i))
949+
.pub_()
950+
.build_ty(phantom);
937951
fields.push(field)
938952
}
939953
}
@@ -972,16 +986,18 @@ impl CodeGenerator for CompInfo {
972986
// affect layout, so we're bad and pray to the gods for avoid sending
973987
// all the tests to shit when parsing things like max_align_t.
974988
if self.found_unknown_attr() {
975-
warn!("Type {} has an unkown attribute that may affect layout", canonical_name);
989+
warn!("Type {} has an unkown attribute that may affect layout",
990+
canonical_name);
976991
}
992+
977993
if applicable_template_args.is_empty() && !self.found_unknown_attr() {
978994
for var in self.inner_vars() {
979995
ctx.resolve_item(*var).codegen(ctx, result, &());
980996
}
981997

982998
if let Some(layout) = layout {
983-
let fn_name =
984-
ctx.rust_ident_raw(&format!("bindgen_test_layout_{}", canonical_name));
999+
let fn_name = format!("bindgen_test_layout_{}", canonical_name);
1000+
let fn_name = ctx.rust_ident_raw(&fn_name);
9851001
let ident = ctx.rust_ident_raw(&canonical_name);
9861002
let size_of_expr =
9871003
quote_expr!(ctx.ext_cx(), ::std::mem::size_of::<$ident>());
@@ -1107,6 +1123,7 @@ impl MethodCodegen for Method {
11071123
};
11081124

11091125
assert!(!fndecl.inputs.is_empty());
1126+
11101127
// FIXME: use aster here.
11111128
fndecl.inputs[0] = ast::Arg {
11121129
ty: P(ast::Ty {
@@ -1123,9 +1140,11 @@ impl MethodCodegen for Method {
11231140
}),
11241141
pat: P(ast::Pat {
11251142
id: ast::DUMMY_NODE_ID,
1126-
node: ast::PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Immutable),
1127-
respan(ctx.span(), ctx.ext_cx().ident_of("self")),
1128-
None),
1143+
node: ast::PatKind::Ident(
1144+
ast::BindingMode::ByValue(ast::Mutability::Immutable),
1145+
respan(ctx.span(), ctx.ext_cx().ident_of("self")),
1146+
None
1147+
),
11291148
span: ctx.span(),
11301149
}),
11311150
id: ast::DUMMY_NODE_ID,
@@ -1223,7 +1242,8 @@ impl CodeGenerator for Enum {
12231242
}
12241243
}
12251244
None => {
1226-
warn!("Guessing type of enum! Forward declarations of enums shouldn't be legal!");
1245+
warn!("Guessing type of enum! Forward declarations of enums \
1246+
shouldn't be legal!");
12271247
IntKind::Int
12281248
}
12291249
};
@@ -1272,8 +1292,7 @@ impl CodeGenerator for Enum {
12721292
// Only to avoid recomputing every time.
12731293
enum_canonical_name: &str,
12741294
// May be the same as "variant" if it's because the
1275-
// enum
1276-
// is unnamed and we still haven't seen the value.
1295+
// enum is unnamed and we still haven't seen the value.
12771296
variant_name: &str,
12781297
referenced_name: &str,
12791298
enum_rust_ty: P<ast::Ty>,
@@ -1342,9 +1361,11 @@ impl CodeGenerator for Enum {
13421361
.canonical_name(ctx));
13431362
}
13441363

1364+
let parent_name = parent_canonical_name.as_ref()
1365+
.unwrap();
1366+
13451367
Cow::Owned(
1346-
format!("{}_{}", parent_canonical_name.as_ref().unwrap(),
1347-
variant_name))
1368+
format!("{}_{}", parent_name, variant_name))
13481369
};
13491370

13501371
add_constant(enum_ty,
@@ -1464,13 +1485,15 @@ impl ToRustTy for Type {
14641485
let mut inner_ty = inner.to_rust_ty(ctx).unwrap();
14651486

14661487
if let ast::TyKind::Path(_, ref mut path) = inner_ty.node {
1488+
let template_args = template_args.iter()
1489+
.map(|arg| arg.to_rust_ty(ctx))
1490+
.collect();
1491+
14671492
path.segments.last_mut().unwrap().parameters =
14681493
ast::PathParameters::AngleBracketed(
14691494
ast::AngleBracketedParameterData {
14701495
lifetimes: vec![],
1471-
types: P::from_vec(template_args.iter().map(|arg| {
1472-
arg.to_rust_ty(ctx)
1473-
}).collect()),
1496+
types: P::from_vec(template_args),
14741497
bindings: P::from_vec(vec![]),
14751498
}
14761499
);
@@ -1498,7 +1521,8 @@ impl ToRustTy for Type {
14981521
Some(layout) => BlobTyBuilder::new(layout).build(),
14991522
None => {
15001523
warn!("Couldn't compute layout for a type with non \
1501-
type template params or opaque, expect dragons!");
1524+
type template params or opaque, expect \
1525+
dragons!");
15021526
aster::AstBuilder::new().ty().unit()
15031527
}
15041528
};
@@ -1558,11 +1582,14 @@ impl ToRustTy for FunctionSig {
15581582
let arg_item = ctx.resolve_item(ty);
15591583
let arg_ty = arg_item.kind().expect_type();
15601584

1561-
// From the C90 standard (http://c0x.coding-guidelines.com/6.7.5.3.html)
1562-
// 1598 - A declaration of a parameter as “array of type” shall be
1563-
// adjusted to “qualified pointer to type”, where the type qualifiers
1564-
// (if any) are those specified within the [ and ] of the array type
1565-
// derivation.
1585+
// From the C90 standard[1]:
1586+
//
1587+
// A declaration of a parameter as "array of type" shall be
1588+
// adjusted to "qualified pointer to type", where the type
1589+
// qualifiers (if any) are those specified within the [ and ] of
1590+
// the array type derivation.
1591+
//
1592+
// [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
15661593
let arg_ty = if let TypeKind::Array(t, _) = *arg_ty.kind() {
15671594
t.to_rust_ty(ctx).to_ptr(arg_ty.is_const(), ctx.span())
15681595
} else {
@@ -1678,8 +1705,7 @@ pub fn codegen(context: &mut BindgenContext) -> Vec<P<ast::Item>> {
16781705
context.options().whitelisted_vars.is_empty() {
16791706
for (_item_id, item) in context.items() {
16801707
// Non-toplevel item parents are the responsible one for
1681-
// generating
1682-
// them.
1708+
// generating them.
16831709
if item.is_toplevel(context) {
16841710
item.codegen(context, &mut result, &());
16851711
}

0 commit comments

Comments
 (0)