Skip to content

Commit 9db4d1f

Browse files
author
bors-servo
authored
Auto merge of #158 - ajnirp:143-comment-get-tag-attr, r=emilio
143 comment get tag attr Fixes #143
2 parents 1460787 + 6298736 commit 9db4d1f

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/clang.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,17 +822,29 @@ impl Comment {
822822

823823
/// Given that this comment is an HTML start tag, get the `idx`th
824824
/// attribute's name.
825-
pub fn get_tag_attr_name(&self, idx: c_uint) -> String {
826-
unsafe {
827-
String_ { x: clang_HTMLStartTag_getAttrName(self.x, idx) }.to_string()
825+
pub fn get_tag_attr_name(&self, idx: c_uint) -> Option<String> {
826+
if idx < self.get_num_tag_attrs() {
827+
None
828+
} else {
829+
unsafe {
830+
Some(String_ {
831+
x: clang_HTMLStartTag_getAttrName(self.x, idx)
832+
}.to_string())
833+
}
828834
}
829835
}
830836

831837
/// Given that this comment is an HTML start tag, get the `idx`th
832838
/// attribute's value.
833-
pub fn get_tag_attr_value(&self, idx: c_uint) -> String {
834-
unsafe {
835-
String_ { x: clang_HTMLStartTag_getAttrValue(self.x, idx) }.to_string()
839+
pub fn get_tag_attr_value(&self, idx: c_uint) -> Option<String> {
840+
if idx < self.get_num_tag_attrs() {
841+
None
842+
} else {
843+
unsafe {
844+
Some(String_ {
845+
x: clang_HTMLStartTag_getAttrValue(self.x, idx)
846+
}.to_string())
847+
}
836848
}
837849
}
838850
}

src/ir/annotations.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,20 @@ impl Annotations {
134134
if comment.kind() == CXComment_HTMLStartTag &&
135135
comment.get_tag_name() == "div" &&
136136
comment.get_num_tag_attrs() > 1 &&
137-
comment.get_tag_attr_name(0) == "rustbindgen" {
137+
comment.get_tag_attr_name(0).as_ref().map(|s| s.as_str())
138+
== Some("rustbindgen") {
138139
*matched = true;
139140
for i in 0..comment.get_num_tag_attrs() {
140-
let value = comment.get_tag_attr_value(i);
141-
let name = comment.get_tag_attr_name(i);
142-
match name.as_str() {
141+
let value_opt = comment.get_tag_attr_value(i);
142+
match comment.get_tag_attr_name(i).unwrap().as_str() {
143143
"opaque" => self.opaque = true,
144144
"hide" => self.hide = true,
145145
"nocopy" => self.disallow_copy = true,
146-
"replaces" => self.use_instead_of = Some(value),
147-
"private" => self.private_fields = Some(value != "false"),
148-
"accessor"
149-
=> self.accessor_kind = Some(parse_accessor(&value)),
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)),
150151
_ => {},
151152
}
152153
}

0 commit comments

Comments
 (0)