@@ -101,7 +101,9 @@ impl Cursor {
101
101
/// See documentation for `lexical_parent` for details on semantic vs
102
102
/// lexical parents.
103
103
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
+ } ;
105
107
if sp == * self || !sp. is_valid ( ) {
106
108
return None ;
107
109
}
@@ -113,11 +115,7 @@ impl Cursor {
113
115
/// See documentation for `lexical_parent` for details on semantic vs
114
116
/// lexical parents.
115
117
pub fn semantic_parent ( & self ) -> Cursor {
116
- unsafe {
117
- Cursor {
118
- x : clang_getCursorSemanticParent ( self . x ) ,
119
- }
120
- }
118
+ self . fallible_semantic_parent ( ) . unwrap ( )
121
119
}
122
120
123
121
/// Return the number of template arguments used by this cursor's referent,
@@ -157,17 +155,18 @@ impl Cursor {
157
155
158
156
/// Is the referent a top level construct?
159
157
pub fn is_toplevel ( & self ) -> bool {
160
- let mut semantic_parent = self . semantic_parent ( ) ;
158
+ let mut semantic_parent = self . fallible_semantic_parent ( ) ;
161
159
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 ( ) ;
166
165
}
167
166
168
167
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 ( )
171
170
}
172
171
173
172
/// Get the kind of referent this cursor is pointing to.
0 commit comments