Skip to content

Make clang::Cursor::fallible_semantic_parent make ffi call #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Copy link
Contributor Author

@malisas malisas Nov 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, semantic_parent.unwrap() is now None. Therefore true can only be returned if tu is None or tu.fallible_semantic_parent() is None. tu will never be None because it is a Cursor type, so the first check is now unnecessary.

}

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