Skip to content

Commit bfc0290

Browse files
committed
make fallible_semantic_parent do ffi call and validation
1 parent b47f405 commit bfc0290

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/clang.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ impl Cursor {
101101
/// See documentation for `lexical_parent` for details on semantic vs
102102
/// lexical parents.
103103
pub fn fallible_semantic_parent(&self) -> Option<Cursor> {
104-
let sp = self.semantic_parent();
104+
let sp = unsafe {
105+
Cursor { x: clang_getCursorSemanticParent(self.x) }
106+
};
105107
if sp == *self || !sp.is_valid() {
106108
return None;
107109
}
@@ -113,11 +115,7 @@ impl Cursor {
113115
/// See documentation for `lexical_parent` for details on semantic vs
114116
/// lexical parents.
115117
pub fn semantic_parent(&self) -> Cursor {
116-
unsafe {
117-
Cursor {
118-
x: clang_getCursorSemanticParent(self.x),
119-
}
120-
}
118+
self.fallible_semantic_parent().unwrap()
121119
}
122120

123121
/// Return the number of template arguments used by this cursor's referent,
@@ -157,17 +155,18 @@ impl Cursor {
157155

158156
/// Is the referent a top level construct?
159157
pub fn is_toplevel(&self) -> bool {
160-
let mut semantic_parent = self.semantic_parent();
158+
let mut semantic_parent = self.fallible_semantic_parent();
161159

162-
while semantic_parent.kind() == CXCursor_Namespace ||
163-
semantic_parent.kind() == CXCursor_NamespaceAlias ||
164-
semantic_parent.kind() == CXCursor_NamespaceRef {
165-
semantic_parent = semantic_parent.semantic_parent();
160+
while semantic_parent.is_some() &&
161+
(semantic_parent.unwrap().kind() == CXCursor_Namespace ||
162+
semantic_parent.unwrap().kind() == CXCursor_NamespaceAlias ||
163+
semantic_parent.unwrap().kind() == CXCursor_NamespaceRef) {
164+
semantic_parent = semantic_parent.unwrap().fallible_semantic_parent();
166165
}
167166

168167
let tu = self.translation_unit();
169-
// Yes, the second can happen with, e.g., macro definitions.
170-
semantic_parent == tu || semantic_parent == tu.semantic_parent()
168+
// Yes, this can happen with, e.g., macro definitions.
169+
semantic_parent == tu.fallible_semantic_parent()
171170
}
172171

173172
/// Get the kind of referent this cursor is pointing to.

0 commit comments

Comments
 (0)