From 5f92d86a3c88c47817e061bb2870e917757a738c Mon Sep 17 00:00:00 2001 From: Ethan Glasser-Camp Date: Wed, 2 Nov 2016 12:41:35 -0400 Subject: [PATCH 1/3] Make clang::Cursor::specialized return an Option Fixes #122. --- src/clang.rs | 12 ++++++++---- src/ir/comp.rs | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/clang.rs b/src/clang.rs index d69c8b14b5..bbe0648d0f 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -187,7 +187,7 @@ impl Cursor { /// Is the referent a template specialization? pub fn is_template(&self) -> bool { - self.specialized().is_valid() + self.specialized().map_or(false, |c| c.is_valid()) } /// Is the referent a fully specialized template specialization without any @@ -287,11 +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 { + if !self.is_valid() { + return None; + } + unsafe { - Cursor { + Some(Cursor { x: clang_getSpecializedCursorTemplate(self.x), - } + }) } } diff --git a/src/ir/comp.rs b/src/ir/comp.rs index d55c24ca16..91be2ecac5 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -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| { From 08a34a1eb5b8b21981cde35af1897cc9269e5a8e Mon Sep 17 00:00:00 2001 From: Ethan Glasser-Camp Date: Wed, 2 Nov 2016 13:05:22 -0400 Subject: [PATCH 2/3] Check resulting Cursor instead of the existing one Thanks @fitzgen for the correction. This also allows us to simplify the is_template method. Thanks @emilio for the suggestion. --- src/clang.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/clang.rs b/src/clang.rs index bbe0648d0f..3f23cbb606 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -187,7 +187,7 @@ impl Cursor { /// Is the referent a template specialization? pub fn is_template(&self) -> bool { - self.specialized().map_or(false, |c| c.is_valid()) + self.specialized().is_some() } /// Is the referent a fully specialized template specialization without any @@ -288,14 +288,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) -> Option { - if !self.is_valid() { - return None; - } - unsafe { - Some(Cursor { - x: clang_getSpecializedCursorTemplate(self.x), - }) + let clang_specialized = clang_getSpecializedCursorTemplate(self.x); + if clang_isInvalid(clang_getCursorKind(clang_specialized)) == 0 { + Some(Cursor { + x: clang_specialized, + }) + } else { + None + } } } From 94136d22aae3b92ef66f7673641f7935b76fd98d Mon Sep 17 00:00:00 2001 From: Ethan Glasser-Camp Date: Wed, 2 Nov 2016 20:01:50 -0400 Subject: [PATCH 3/3] Reuse Cursor::is_valid() Thanks @emilio for the suggestion. --- src/clang.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/clang.rs b/src/clang.rs index 3f23cbb606..f8aac1c1ff 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -289,14 +289,10 @@ impl Cursor { /// pointing to the template definition that is being specialized. pub fn specialized(&self) -> Option { unsafe { - let clang_specialized = clang_getSpecializedCursorTemplate(self.x); - if clang_isInvalid(clang_getCursorKind(clang_specialized)) == 0 { - Some(Cursor { - x: clang_specialized, - }) - } else { - None - } + let ret = Cursor { + x: clang_getSpecializedCursorTemplate(self.x) + }; + if ret.is_valid() { Some(ret) } else { None } } }