Skip to content

Commit a960fb8

Browse files
author
bors-servo
authored
Auto merge of #169 - jeanphilippeD:issue166, r=emilio
Use iterators for comment children and attributes fix #166 Fix #166
2 parents ebfa083 + 91eb86d commit a960fb8

File tree

2 files changed

+64
-51
lines changed

2 files changed

+64
-51
lines changed

src/clang.rs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -791,19 +791,12 @@ impl Comment {
791791
}
792792
}
793793

794-
/// Get the number of children this comment node has.
795-
pub fn num_children(&self) -> c_uint {
796-
unsafe {
797-
clang_Comment_getNumChildren(self.x)
798-
}
799-
}
800-
801-
/// Get this comment's `idx`th child comment
802-
pub fn get_child(&self, idx: c_uint) -> Option<Comment> {
803-
if idx >= self.num_children() {
804-
None
805-
} else {
806-
Some(Comment { x: unsafe { clang_Comment_getChild(self.x, idx) } })
794+
/// Get this comment's children comment
795+
pub fn get_children(&self) -> CommentChildrenIterator {
796+
CommentChildrenIterator {
797+
parent: self.x,
798+
length: unsafe { clang_Comment_getNumChildren(self.x) },
799+
index: 0
807800
}
808801
}
809802

@@ -815,39 +808,63 @@ impl Comment {
815808
}
816809
}
817810

818-
/// Given that this comment is an HTML start tag, get the number of HTML
819-
/// attributes it has.
820-
pub fn get_num_tag_attrs(&self) -> c_uint {
821-
unsafe {
822-
clang_HTMLStartTag_getNumAttrs(self.x)
811+
/// Given that this comment is an HTML start tag, get its attributes.
812+
pub fn get_tag_attrs(&self) -> CommentAttributesIterator {
813+
CommentAttributesIterator {
814+
x: self.x,
815+
length: unsafe { clang_HTMLStartTag_getNumAttrs(self.x) },
816+
index: 0
823817
}
824818
}
819+
}
825820

826-
/// Given that this comment is an HTML start tag, get the `idx`th
827-
/// attribute's name.
828-
pub fn get_tag_attr_name(&self, idx: c_uint) -> Option<String> {
829-
if idx >= self.get_num_tag_attrs() {
830-
None
821+
/// An iterator for a comment's children
822+
pub struct CommentChildrenIterator {
823+
parent: CXComment,
824+
length: c_uint,
825+
index: c_uint
826+
}
827+
828+
impl Iterator for CommentChildrenIterator {
829+
type Item = Comment;
830+
fn next(&mut self) -> Option<Comment> {
831+
if self.index < self.length {
832+
let idx = self.index;
833+
self.index += 1;
834+
Some( Comment { x: unsafe { clang_Comment_getChild(self.parent, idx) } } )
831835
} else {
832-
unsafe {
833-
Some(String_ {
834-
x: clang_HTMLStartTag_getAttrName(self.x, idx)
835-
}.to_string())
836-
}
836+
None
837837
}
838838
}
839+
}
839840

840-
/// Given that this comment is an HTML start tag, get the `idx`th
841-
/// attribute's value.
842-
pub fn get_tag_attr_value(&self, idx: c_uint) -> Option<String> {
843-
if idx >= self.get_num_tag_attrs() {
844-
None
841+
/// An HTML start tag comment attribute
842+
pub struct CommentAttribute {
843+
/// HTML start tag attribute name
844+
pub name: String,
845+
/// HTML start tag attribute value
846+
pub value: String
847+
}
848+
849+
/// An iterator for a comment's attributes
850+
pub struct CommentAttributesIterator {
851+
x: CXComment,
852+
length: c_uint,
853+
index: c_uint
854+
}
855+
856+
impl Iterator for CommentAttributesIterator {
857+
type Item = CommentAttribute;
858+
fn next(&mut self) -> Option<CommentAttribute> {
859+
if self.index < self.length {
860+
let idx = self.index;
861+
self.index += 1;
862+
Some( CommentAttribute {
863+
name: String_ { x: unsafe { clang_HTMLStartTag_getAttrName(self.x, idx) } }.to_string(),
864+
value: String_ { x: unsafe { clang_HTMLStartTag_getAttrValue(self.x, idx) } }.to_string()
865+
})
845866
} else {
846-
unsafe {
847-
Some(String_ {
848-
x: clang_HTMLStartTag_getAttrValue(self.x, idx)
849-
}.to_string())
850-
}
867+
None
851868
}
852869
}
853870
}

src/ir/annotations.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,28 +133,24 @@ impl Annotations {
133133
use clangll::CXComment_HTMLStartTag;
134134
if comment.kind() == CXComment_HTMLStartTag &&
135135
comment.get_tag_name() == "div" &&
136-
comment.get_num_tag_attrs() > 1 &&
137-
comment.get_tag_attr_name(0).as_ref().map(|s| s.as_str())
138-
== Some("rustbindgen") {
136+
comment.get_tag_attrs().next().map_or(false, |attr| attr.name == "rustbindgen") {
139137
*matched = true;
140-
for i in 0..comment.get_num_tag_attrs() {
141-
let value_opt = comment.get_tag_attr_value(i);
142-
match comment.get_tag_attr_name(i).unwrap().as_str() {
138+
for attr in comment.get_tag_attrs() {
139+
match attr.name.as_str() {
143140
"opaque" => self.opaque = true,
144141
"hide" => self.hide = true,
145142
"nocopy" => self.disallow_copy = true,
146-
"replaces" => self.use_instead_of = value_opt,
147-
"private" => self.private_fields = value_opt.map(|v|
148-
v != "false"),
149-
"accessor" => self.accessor_kind = value_opt.map(|v|
150-
parse_accessor(&v)),
143+
"replaces" => self.use_instead_of = Some(attr.value),
144+
"private" => self.private_fields = Some(attr.value != "false"),
145+
"accessor"
146+
=> self.accessor_kind = Some(parse_accessor(&attr.value)),
151147
_ => {},
152148
}
153149
}
154150
}
155151

156-
for i in 0..comment.num_children() {
157-
self.parse(&comment.get_child(i).unwrap(), matched);
152+
for child in comment.get_children() {
153+
self.parse(&child, matched);
158154
}
159155
}
160156
}

0 commit comments

Comments
 (0)