Skip to content

Commit 4094396

Browse files
committed
Implement DotAttribute for a bunch of types
This gives us more debug information in the emitted graphviz dot files.
1 parent 28c7508 commit 4094396

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

src/ir/function.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Intermediate representation for C/C++ functions and methods.
22
33
use super::context::{BindgenContext, ItemId};
4+
use super::dot::DotAttributes;
45
use super::item::Item;
56
use super::traversal::{Trace, Tracer};
67
use super::ty::TypeKind;
78
use clang;
89
use clang_sys::CXCallingConv;
910
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
11+
use std::io;
1012
use syntax::abi;
1113

1214
/// A function declaration, with a signature, arguments, and argument names.
@@ -59,6 +61,18 @@ impl Function {
5961
}
6062
}
6163

64+
impl DotAttributes for Function {
65+
fn dot_attributes<W>(&self, _ctx: &BindgenContext, out: &mut W) -> io::Result<()>
66+
where W: io::Write
67+
{
68+
if let Some(ref mangled) = self.mangled_name {
69+
try!(writeln!(out, "<tr><td>mangled name</td><td>{}</td></tr>", mangled));
70+
}
71+
72+
Ok(())
73+
}
74+
}
75+
6276
/// A function signature.
6377
#[derive(Debug)]
6478
pub struct FunctionSig {

src/ir/item.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -920,13 +920,12 @@ impl DotAttributes for Item {
920920
fn dot_attributes<W>(&self, ctx: &BindgenContext, out: &mut W) -> io::Result<()>
921921
where W: io::Write
922922
{
923-
writeln!(out,
924-
"<tr><td>{:?}</td></tr>
925-
<tr><td>name</td><td>{}</td></tr>
926-
<tr><td>kind</td><td>{}</td></tr>",
927-
self.id,
928-
self.name(ctx).get(),
929-
self.kind.kind_name())
923+
try!(writeln!(out,
924+
"<tr><td>{:?}</td></tr>
925+
<tr><td>name</td><td>{}</td></tr>",
926+
self.id,
927+
self.name(ctx).get()));
928+
self.kind.dot_attributes(ctx, out)
930929
}
931930
}
932931

src/ir/item_kind.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Different variants of an `Item` in our intermediate representation.
22
3+
use std::io;
4+
use super::context::BindgenContext;
5+
use super::dot::DotAttributes;
36
use super::function::Function;
47
use super::module::Module;
58
use super::ty::Type;
@@ -39,7 +42,7 @@ impl ItemKind {
3942
ItemKind::Type(..) => "Type",
4043
ItemKind::Function(..) => "Function",
4144
ItemKind::Var(..) => "Var"
42-
}
45+
}
4346
}
4447

4548
/// Is this a module?
@@ -122,3 +125,18 @@ impl ItemKind {
122125
self.as_var().expect("Not a var")
123126
}
124127
}
128+
129+
impl DotAttributes for ItemKind {
130+
fn dot_attributes<W>(&self, ctx: &BindgenContext, out: &mut W) -> io::Result<()>
131+
where W: io::Write
132+
{
133+
try!(writeln!(out, "<tr><td>kind</td><td>{}</td></tr>", self.kind_name()));
134+
135+
match *self {
136+
ItemKind::Module(ref module) => module.dot_attributes(ctx, out),
137+
ItemKind::Type(ref ty) => ty.dot_attributes(ctx, out),
138+
ItemKind::Function(ref func) => func.dot_attributes(ctx, out),
139+
ItemKind::Var(ref var) => var.dot_attributes(ctx, out),
140+
}
141+
}
142+
}

src/ir/module.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Intermediate representation for modules (AKA C++ namespaces).
22
3+
use std::io;
34
use super::context::{BindgenContext, ItemId};
5+
use super::dot::DotAttributes;
46
use clang;
57
use parse::{ClangSubItemParser, ParseError, ParseResult};
68
use parse_one;
@@ -56,6 +58,16 @@ impl Module {
5658
}
5759
}
5860

61+
impl DotAttributes for Module {
62+
fn dot_attributes<W>(&self, _ctx: &BindgenContext, out: &mut W) -> io::Result<()>
63+
where W: io::Write
64+
{
65+
writeln!(out,
66+
"<tr><td>ModuleKind</td><td>{:?}</td></tr>",
67+
self.kind)
68+
}
69+
}
70+
5971
impl ClangSubItemParser for Module {
6072
fn parse(cursor: clang::Cursor,
6173
ctx: &mut BindgenContext)

src/ir/ty.rs

+57
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::comp::CompInfo;
44
use super::context::{BindgenContext, ItemId};
55
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
6+
use super::dot::DotAttributes;
67
use super::enum_ty::Enum;
78
use super::function::FunctionSig;
89
use super::int::IntKind;
@@ -12,6 +13,7 @@ use super::objc::ObjCInterface;
1213
use super::traversal::{Trace, Tracer};
1314
use clang::{self, Cursor};
1415
use parse::{ClangItemParser, ParseError, ParseResult};
16+
use std::io;
1517
use std::mem;
1618

1719
/// Template declaration related methods.
@@ -384,6 +386,61 @@ impl Type {
384386
}
385387
}
386388

389+
impl DotAttributes for Type {
390+
fn dot_attributes<W>(&self, ctx: &BindgenContext, out: &mut W) -> io::Result<()>
391+
where W: io::Write
392+
{
393+
if let Some(ref layout) = self.layout {
394+
try!(writeln!(out,
395+
"<tr><td>size</td><td>{}</td></tr>
396+
<tr><td>align</td><td>{}</td></tr>",
397+
layout.size,
398+
layout.align));
399+
if layout.packed {
400+
try!(writeln!(out, "<tr><td>packed</td><td>true</td></tr>"));
401+
}
402+
}
403+
404+
if self.is_const {
405+
try!(writeln!(out, "<tr><td>const</td><td>true</td></tr>"));
406+
}
407+
408+
self.kind.dot_attributes(ctx, out)
409+
}
410+
}
411+
412+
impl DotAttributes for TypeKind {
413+
fn dot_attributes<W>(&self, _ctx: &BindgenContext, out: &mut W) -> io::Result<()>
414+
where W: io::Write
415+
{
416+
write!(out,
417+
"<tr><td>TypeKind</td><td>{}</td></tr>",
418+
match *self {
419+
TypeKind::Void => "Void",
420+
TypeKind::NullPtr => "NullPtr",
421+
TypeKind::Comp(..) => "Comp",
422+
TypeKind::Int(..) => "Int",
423+
TypeKind::Float(..) => "Float",
424+
TypeKind::Complex(..) => "Complex",
425+
TypeKind::Alias(..) => "Alias",
426+
TypeKind::TemplateAlias(..) => "TemplateAlias",
427+
TypeKind::Array(..) => "Array",
428+
TypeKind::Function(..) => "Function",
429+
TypeKind::Enum(..) => "Enum",
430+
TypeKind::Pointer(..) => "Pointer",
431+
TypeKind::BlockPointer => "BlockPointer",
432+
TypeKind::Reference(..) => "Reference",
433+
TypeKind::TemplateInstantiation(..) => "TemplateInstantiation",
434+
TypeKind::ResolvedTypeRef(..) => "ResolvedTypeRef",
435+
TypeKind::Named => "Named",
436+
TypeKind::ObjCInterface(..) => "ObjCInterface",
437+
TypeKind::UnresolvedTypeRef(..) => {
438+
unreachable!("there shouldn't be any more of these anymore")
439+
}
440+
})
441+
}
442+
}
443+
387444
#[test]
388445
fn is_invalid_named_type_valid() {
389446
let ty = Type::new(Some("foo".into()), None, TypeKind::Named, false);

src/ir/var.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! Intermediate representation of variables.
22
33
use super::context::{BindgenContext, ItemId};
4+
use super::dot::DotAttributes;
45
use super::function::cursor_mangling;
56
use super::int::IntKind;
67
use super::item::Item;
78
use super::ty::{FloatKind, TypeKind};
89
use cexpr;
910
use clang;
1011
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
12+
use std::io;
1113
use std::num::Wrapping;
1214

1315
/// The type for a constant variable.
@@ -84,6 +86,22 @@ impl Var {
8486
}
8587
}
8688

89+
impl DotAttributes for Var {
90+
fn dot_attributes<W>(&self, _ctx: &BindgenContext, out: &mut W) -> io::Result<()>
91+
where W: io::Write
92+
{
93+
if self.is_const {
94+
try!(writeln!(out, "<tr><td>const</td><td>true</td></tr>"));
95+
}
96+
97+
if let Some(ref mangled) = self.mangled_name {
98+
try!(writeln!(out, "<tr><td>mangled name</td><td>{}</td></tr>", mangled));
99+
}
100+
101+
Ok(())
102+
}
103+
}
104+
87105
impl ClangSubItemParser for Var {
88106
fn parse(cursor: clang::Cursor,
89107
ctx: &mut BindgenContext)

0 commit comments

Comments
 (0)