Skip to content

Make clang::Cursor::specialized return an Option #191

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 3 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl Cursor {

/// Is the referent a template specialization?
pub fn is_template(&self) -> bool {
self.specialized().is_valid()
self.specialized().is_some()
}

/// Is the referent a fully specialized template specialization without any
Expand Down Expand Up @@ -287,10 +287,15 @@ impl Cursor {

/// Given that this cursor points to a template specialization, get a cursor
/// pointing to the template definition that is being specialized.
pub fn specialized(&self) -> Cursor {
pub fn specialized(&self) -> Option<Cursor> {
unsafe {
Cursor {
x: clang_getSpecializedCursorTemplate(self.x),
let clang_specialized = clang_getSpecializedCursorTemplate(self.x);
if clang_isInvalid(clang_getCursorKind(clang_specialized)) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe:

let ret = Cursor {
    x: clang_getSpecializedCursorTemplate(self.x),
};

if ret.is_valid() { Some(ret) } else { None }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about doing that, but it made me feel uncomfortable to have a Cursor object that didn't necessarily correspond to a real cursor. Also, I misunderstood what @fitzgen said in his comment above. But I agree that it's better to reuse the definition of is_valid. Let me make that change.

Some(Cursor {
x: clang_specialized,
})
} else {
None
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ impl CompInfo {
}
};

ci.ref_template = Item::parse(cursor.specialized(), None, ctx).ok();
ci.ref_template = cursor.specialized()
.and_then(|c| Item::parse(c, None, ctx).ok());

let mut maybe_anonymous_struct_field = None;
cursor.visit(|cur, _other| {
Expand Down