Skip to content

Commit 7615250

Browse files
author
bors-servo
authored
Auto merge of #356 - fitzgen:libclang-is-my-enemy, r=emilio
Only load libclang once, and don't unload it See the first commit for details. Second commit is just `cargo fmt`. r? @emilio
2 parents 96e5d77 + c109c73 commit 7615250

File tree

9 files changed

+108
-86
lines changed

9 files changed

+108
-86
lines changed

libbindgen/src/clang.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl Cursor {
351351
/// Is the referent an inlined function?
352352
pub fn is_inlined_function(&self) -> bool {
353353
clang_Cursor_isFunctionInlined::is_loaded() &&
354-
unsafe { clang_Cursor_isFunctionInlined(self.x) != 0 }
354+
unsafe { clang_Cursor_isFunctionInlined(self.x) != 0 }
355355
}
356356

357357
/// Get the width of this cursor's referent bit field, or `None` if the
@@ -404,7 +404,7 @@ impl Cursor {
404404
/// being aliased.
405405
pub fn typedef_type(&self) -> Option<Type> {
406406
let inner = Type {
407-
x: unsafe { clang_getTypedefDeclUnderlyingType(self.x) }
407+
x: unsafe { clang_getTypedefDeclUnderlyingType(self.x) },
408408
};
409409

410410
if inner.is_valid() { Some(inner) } else { None }
@@ -603,9 +603,7 @@ impl Type {
603603

604604
/// Get a raw display name for this type.
605605
pub fn spelling(&self) -> String {
606-
unsafe {
607-
cxstring_into_string(clang_getTypeSpelling(self.x))
608-
}
606+
unsafe { cxstring_into_string(clang_getTypeSpelling(self.x)) }
609607
}
610608

611609
/// Is this type const qualified?
@@ -879,9 +877,7 @@ impl Comment {
879877
/// Given that this comment is the start or end of an HTML tag, get its tag
880878
/// name.
881879
pub fn get_tag_name(&self) -> String {
882-
unsafe {
883-
cxstring_into_string(clang_HTMLTagComment_getTagName(self.x))
884-
}
880+
unsafe { cxstring_into_string(clang_HTMLTagComment_getTagName(self.x)) }
885881
}
886882

887883
/// Given that this comment is an HTML start tag, get its attributes.
@@ -964,9 +960,7 @@ impl File {
964960
if self.x.is_null() {
965961
return None;
966962
}
967-
Some(unsafe {
968-
cxstring_into_string(clang_getFileName(self.x))
969-
})
963+
Some(unsafe { cxstring_into_string(clang_getFileName(self.x)) })
970964
}
971965
}
972966

@@ -1117,8 +1111,8 @@ impl TranslationUnit {
11171111
num_tokens as usize);
11181112
for &token in token_array.iter() {
11191113
let kind = clang_getTokenKind(token);
1120-
let spelling = cxstring_into_string(
1121-
clang_getTokenSpelling(self.x, token));
1114+
let spelling =
1115+
cxstring_into_string(clang_getTokenSpelling(self.x, token));
11221116

11231117
tokens.push(Token {
11241118
kind: kind,
@@ -1175,7 +1169,9 @@ impl TranslationUnit {
11751169
// NB: cexpr is not too happy about comments inside
11761170
// expressions, so we strip them down here.
11771171
CXToken_Comment => return None,
1178-
_ => panic!("Found unexpected token kind: {:?}", token.kind),
1172+
_ => {
1173+
panic!("Found unexpected token kind: {:?}", token.kind)
1174+
}
11791175
};
11801176

11811177
Some(token::Token {
@@ -1207,8 +1203,8 @@ impl Diagnostic {
12071203
pub fn format(&self) -> String {
12081204
unsafe {
12091205
let opts = clang_defaultDiagnosticDisplayOptions();
1210-
cxstring_into_string(
1211-
clang_formatDiagnostic(self.x, opts)) }
1206+
cxstring_into_string(clang_formatDiagnostic(self.x, opts))
1207+
}
12121208
}
12131209

12141210
/// What is the severity of this diagnostic message?
@@ -1312,7 +1308,8 @@ impl EvalResult {
13121308
// `CXType_Unexposed` from evaluation.
13131309
let mut found_cant_eval = false;
13141310
cursor.visit(|c| {
1315-
if c.kind() == CXCursor_TypeRef && c.cur_type().kind() == CXType_Unexposed {
1311+
if c.kind() == CXCursor_TypeRef &&
1312+
c.cur_type().kind() == CXType_Unexposed {
13161313
found_cant_eval = true;
13171314
CXChildVisit_Break
13181315
} else {

libbindgen/src/codegen/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> {
3939
let root_ident = ctx.rust_ident(&root);
4040

4141
let super_ = aster::AstBuilder::new().id("super");
42-
let supers = module
43-
.ancestors(ctx)
42+
let supers = module.ancestors(ctx)
4443
.filter(|id| ctx.resolve_item(*id).is_module())
4544
.map(|_| super_.clone())
4645
.chain(iter::once(super_));
@@ -309,7 +308,8 @@ impl CodeGenerator for Module {
309308
item: &Item) {
310309
debug!("<Module as CodeGenerator>::codegen: item = {:?}", item);
311310

312-
let codegen_self = |result: &mut CodegenResult, found_any: &mut bool| {
311+
let codegen_self = |result: &mut CodegenResult,
312+
found_any: &mut bool| {
313313
for child in self.children() {
314314
if whitelisted_items.contains(child) {
315315
*found_any = true;
@@ -742,7 +742,8 @@ impl CodeGenerator for CompInfo {
742742
fn $fn_name() {
743743
assert_eq!($size_of_expr, $size);
744744
assert_eq!($align_of_expr, $align);
745-
}).unwrap();
745+
})
746+
.unwrap();
746747
result.push(item);
747748
}
748749
return;
@@ -1196,7 +1197,10 @@ impl CodeGenerator for CompInfo {
11961197

11971198
if ctx.options().codegen_config.constructors {
11981199
for sig in self.constructors() {
1199-
Method::new(MethodKind::Constructor, *sig, /* const */ false)
1200+
Method::new(MethodKind::Constructor,
1201+
*sig,
1202+
/* const */
1203+
false)
12001204
.codegen_method(ctx,
12011205
&mut methods,
12021206
&mut method_names,
@@ -1342,7 +1346,8 @@ impl MethodCodegen for Method {
13421346
// return-type = void.
13431347
if self.is_constructor() {
13441348
fndecl.inputs.remove(0);
1345-
fndecl.output = ast::FunctionRetTy::Ty(quote_ty!(ctx.ext_cx(), Self));
1349+
fndecl.output =
1350+
ast::FunctionRetTy::Ty(quote_ty!(ctx.ext_cx(), Self));
13461351
}
13471352

13481353
let sig = ast::MethodSig {
@@ -1353,8 +1358,8 @@ impl MethodCodegen for Method {
13531358
constness: respan(ctx.span(), ast::Constness::NotConst),
13541359
};
13551360

1356-
let mut exprs =
1357-
helpers::ast_ty::arguments_from_signature(&signature, ctx);
1361+
let mut exprs = helpers::ast_ty::arguments_from_signature(&signature,
1362+
ctx);
13581363

13591364
let mut stmts = vec![];
13601365

@@ -1661,7 +1666,8 @@ impl CodeGenerator for Enum {
16611666
Entry::Occupied(ref entry) => {
16621667
if is_rust_enum {
16631668
let variant_name = ctx.rust_mangle(variant.name());
1664-
let mangled_name = if is_toplevel || enum_ty.name().is_some() {
1669+
let mangled_name = if is_toplevel ||
1670+
enum_ty.name().is_some() {
16651671
variant_name
16661672
} else {
16671673
let parent_name = parent_canonical_name.as_ref()
@@ -1762,7 +1768,9 @@ impl ToRustTy for Type {
17621768
TypeKind::Void => raw_type(ctx, "c_void"),
17631769
// TODO: we should do something smart with nullptr, or maybe *const
17641770
// c_void is enough?
1765-
TypeKind::NullPtr => raw_type(ctx, "c_void").to_ptr(true, ctx.span()),
1771+
TypeKind::NullPtr => {
1772+
raw_type(ctx, "c_void").to_ptr(true, ctx.span())
1773+
}
17661774
TypeKind::Int(ik) => {
17671775
match ik {
17681776
IntKind::Bool => aster::ty::TyBuilder::new().bool(),

libbindgen/src/ir/annotations.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ impl Annotations {
139139
"hide" => self.hide = true,
140140
"nocopy" => self.disallow_copy = true,
141141
"replaces" => {
142-
self.use_instead_of =
143-
Some(attr.value.split("::")
144-
.map(Into::into)
145-
.collect())
146-
},
142+
self.use_instead_of = Some(attr.value
143+
.split("::")
144+
.map(Into::into)
145+
.collect())
146+
}
147147
"private" => {
148148
self.private_fields = Some(attr.value != "false")
149149
}

libbindgen/src/ir/comp.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,7 @@ impl CompInfo {
533533
warn!("warning: Template parameter is not a type");
534534
}
535535

536-
if specialization {
537-
args
538-
} else {
539-
vec![]
540-
}
536+
if specialization { args } else { vec![] }
541537
}
542538
};
543539

@@ -717,16 +713,15 @@ impl CompInfo {
717713

718714
// NB: This gets us an owned `Function`, not a
719715
// `FunctionSig`.
720-
let signature =
721-
Item::parse(cur, Some(potential_id), ctx)
722-
.expect("CXXMethod");
716+
let signature = Item::parse(cur, Some(potential_id), ctx)
717+
.expect("CXXMethod");
723718

724719
match cur.kind() {
725720
CXCursor_Constructor => {
726721
ci.constructors.push(signature);
727722
}
728723
// TODO(emilio): Bind the destructor?
729-
CXCursor_Destructor => {},
724+
CXCursor_Destructor => {}
730725
CXCursor_CXXMethod => {
731726
let is_const = cur.method_is_const();
732727
let method_kind = if is_static {
@@ -737,9 +732,8 @@ impl CompInfo {
737732
MethodKind::Normal
738733
};
739734

740-
let method = Method::new(method_kind,
741-
signature,
742-
is_const);
735+
let method =
736+
Method::new(method_kind, signature, is_const);
743737

744738
ci.methods.push(method);
745739
}

libbindgen/src/ir/enum_ty.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ impl Enum {
4949
}
5050

5151
let declaration = ty.declaration().canonical();
52-
let repr = declaration.enum_type().and_then(|et| {
53-
Item::from_ty(&et, None, None, ctx).ok()
54-
});
52+
let repr = declaration.enum_type()
53+
.and_then(|et| Item::from_ty(&et, None, None, ctx).ok());
5554
let mut variants = vec![];
5655

5756
let is_signed = match repr {

libbindgen/src/ir/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl FunctionSig {
188188
let is_method = cursor.kind() == CXCursor_CXXMethod;
189189
let is_constructor = cursor.kind() == CXCursor_Constructor;
190190
if (is_constructor || is_method) &&
191-
cursor.lexical_parent() != cursor.semantic_parent() {
191+
cursor.lexical_parent() != cursor.semantic_parent() {
192192
// Only parse constructors once.
193193
return Err(ParseError::Continue);
194194
}

libbindgen/src/ir/item.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ pub struct ItemAncestorsIter<'a, 'b>
9191
seen: DebugOnlyItemSet,
9292
}
9393

94-
impl <'a, 'b> ItemAncestorsIter<'a, 'b>
95-
where 'b: 'a
94+
impl<'a, 'b> ItemAncestorsIter<'a, 'b>
95+
where 'b: 'a,
9696
{
9797
fn new(ctx: &'a BindgenContext<'b>, item: ItemId) -> Self {
9898
ItemAncestorsIter {
@@ -606,9 +606,7 @@ impl Item {
606606
}
607607

608608
/// Get the target item id for name generation.
609-
fn name_target(&self,
610-
ctx: &BindgenContext)
611-
-> ItemId {
609+
fn name_target(&self, ctx: &BindgenContext) -> ItemId {
612610
let mut targets_seen = DebugOnlyItemSet::new();
613611
let mut item = self;
614612

@@ -673,7 +671,7 @@ impl Item {
673671
func.name() == func_name
674672
})
675673
.position(|m| m.signature() == self.id())
676-
})
674+
});
677675
}
678676
}
679677

@@ -682,9 +680,7 @@ impl Item {
682680
}
683681

684682
/// Get this item's base name (aka non-namespaced name).
685-
fn base_name(&self,
686-
ctx: &BindgenContext)
687-
-> String {
683+
fn base_name(&self, ctx: &BindgenContext) -> String {
688684
if let Some(path) = self.annotations().use_instead_of() {
689685
return path.last().unwrap().clone();
690686
}
@@ -737,8 +733,7 @@ impl Item {
737733
ctx: &BindgenContext,
738734
opt: &NameOptions)
739735
-> String {
740-
let target =
741-
ctx.resolve_item(self.name_target(ctx));
736+
let target = ctx.resolve_item(self.name_target(ctx));
742737

743738
// Short-circuit if the target has an override, and just use that.
744739
if let Some(path) = target.annotations.use_instead_of() {
@@ -1080,7 +1075,7 @@ impl ClangItemParser for Item {
10801075
}
10811076

10821077
if let Some(ty) =
1083-
ctx.builtin_or_resolved_ty(id, parent_id, ty, location) {
1078+
ctx.builtin_or_resolved_ty(id, parent_id, ty, location) {
10841079
return Ok(ty);
10851080
}
10861081

@@ -1098,10 +1093,9 @@ impl ClangItemParser for Item {
10981093
};
10991094

11001095
if valid_decl {
1101-
if let Some(&(_, item_id)) =
1102-
ctx.currently_parsed_types
1103-
.iter()
1104-
.find(|&&(d, _)| d == declaration_to_look_for) {
1096+
if let Some(&(_, item_id)) = ctx.currently_parsed_types
1097+
.iter()
1098+
.find(|&&(d, _)| d == declaration_to_look_for) {
11051099
debug!("Avoiding recursion parsing type: {:?}", ty);
11061100
return Ok(item_id);
11071101
}
@@ -1272,9 +1266,9 @@ impl ItemCanonicalPath for Item {
12721266
.filter(|item| item.is_module() || item.id() == target.id())
12731267
.map(|item| {
12741268
ctx.resolve_item(item.name_target(ctx))
1275-
.name(ctx)
1276-
.within_namespaces()
1277-
.get()
1269+
.name(ctx)
1270+
.within_namespaces()
1271+
.get()
12781272
})
12791273
.collect();
12801274
path.reverse();

0 commit comments

Comments
 (0)