diff options
author | Malisa Smith <malisa.tsmith@gmail.com> | 2016-10-30 14:05:37 -0700 |
---|---|---|
committer | Malisa Smith <malisa.tsmith@gmail.com> | 2016-11-06 16:17:19 -0800 |
commit | bfc02909c399f646987f025abf8bce770cf3d581 (patch) | |
tree | 2b23878920550562062a5e27c9c0a7d5f44ce477 | |
parent | b47f405c0d1ee4235f380d787e603687c8cd57e4 (diff) |
make fallible_semantic_parent do ffi call and validation
-rwxr-xr-x | src/clang.rs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/clang.rs b/src/clang.rs index 0bfd78ef..3ea4ad2f 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -101,7 +101,9 @@ impl Cursor { /// See documentation for `lexical_parent` for details on semantic vs /// lexical parents. pub fn fallible_semantic_parent(&self) -> Option<Cursor> { - let sp = self.semantic_parent(); + let sp = unsafe { + Cursor { x: clang_getCursorSemanticParent(self.x) } + }; if sp == *self || !sp.is_valid() { return None; } @@ -113,11 +115,7 @@ impl Cursor { /// See documentation for `lexical_parent` for details on semantic vs /// lexical parents. pub fn semantic_parent(&self) -> Cursor { - unsafe { - Cursor { - x: clang_getCursorSemanticParent(self.x), - } - } + self.fallible_semantic_parent().unwrap() } /// Return the number of template arguments used by this cursor's referent, @@ -157,17 +155,18 @@ impl Cursor { /// Is the referent a top level construct? pub fn is_toplevel(&self) -> bool { - let mut semantic_parent = self.semantic_parent(); + let mut semantic_parent = self.fallible_semantic_parent(); - while semantic_parent.kind() == CXCursor_Namespace || - semantic_parent.kind() == CXCursor_NamespaceAlias || - semantic_parent.kind() == CXCursor_NamespaceRef { - semantic_parent = semantic_parent.semantic_parent(); + while semantic_parent.is_some() && + (semantic_parent.unwrap().kind() == CXCursor_Namespace || + semantic_parent.unwrap().kind() == CXCursor_NamespaceAlias || + semantic_parent.unwrap().kind() == CXCursor_NamespaceRef) { + semantic_parent = semantic_parent.unwrap().fallible_semantic_parent(); } let tu = self.translation_unit(); - // Yes, the second can happen with, e.g., macro definitions. - semantic_parent == tu || semantic_parent == tu.semantic_parent() + // Yes, this can happen with, e.g., macro definitions. + semantic_parent == tu.fallible_semantic_parent() } /// Get the kind of referent this cursor is pointing to. |