Skip to content

clang::Cursor::args should return an Option<Vec<Cursor>> #207

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 2 commits into from
Nov 18, 2016

Conversation

Natim
Copy link

@Natim Natim commented Nov 4, 2016

Attempt to fix #130

@@ -387,16 +387,26 @@ impl Cursor {

/// Given that this cursor's referent is a function, return cursors to its
/// parameters.
pub fn args(&self) -> Vec<Cursor> {
pub fn args(&self) -> Option<Vec<Cursor>> {
// XXX: We might want to use and keep num_args
Copy link
Author

Choose a reason for hiding this comment

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

This is an option, do you think it would be better than not using num_args?

Copy link
Contributor

Choose a reason for hiding this comment

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

We use an iterator pattern somewhere else (for comment children and attributes), so something like that can also work, and it'd be better IMO.

If you want to give a crack at it, that'd be awesome.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you elaborate on this, please? Does this mean we should be returning an iterator instead of a Vec? Or that there's some sort of iterator that we can "build on" to write this method?

Copy link
Contributor

Choose a reason for hiding this comment

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

Returning an iterator is definitely preferable, since it's more efficient. You can implement something like TypeTemplateArgIterator (in that same file). That being said, only a Vec is also fine if you prefer :)

@@ -150,7 +150,7 @@ impl FunctionSig {
CXCursor_CXXMethod => {
// For CXCursor_FunctionDecl, cursor.args() is the reliable way
// to get parameter names and types.
cursor.args()
cursor.args().expect("It cannot be None because we are in a method/function")
Copy link
Author

@Natim Natim Nov 4, 2016

Choose a reason for hiding this comment

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

Is expect the right way to handle this there?
Can we rely on the fact that cursor.args() is now None in case it is not a function or method?

Copy link
Contributor

Choose a reason for hiding this comment

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

So I'd expect we might want to test this with stuff like variadic functions, but if all tests are passing seems like we can keep it, and then if it fails in some weird situation returning an empty vector in the case it's none to preser behavior is a really easy change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, could you write a test with a template and a variadic function here? Otherwise I think yeah, we should be pretty confident.

@fitzgen
Copy link
Member

fitzgen commented Nov 14, 2016

@Natim, everything going OK? Have any questions we can help answer or clarifications we can make?

@Natim
Copy link
Author

Natim commented Nov 15, 2016

I think I understood we should write a test for that. I hope to find some bandwith do to it with @glasserc later this week.

@bors-servo
Copy link

☔ The latest upstream changes (presumably #204) made this pull request unmergeable. Please resolve the merge conflicts.

@Natim
Copy link
Author

Natim commented Nov 17, 2016

We tried to work on a test, writing this hpp file:

template <typename T>
class VariadicFunctionObject {
public:
    int add_em_up(T count,...);
};

And hoping to get this RS file:

/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct VariadicFunctionObject<T> {
    pub _address: u8,
    pub _phantom_0: ::std::marker::PhantomData<T>,
    #[inline]
    pub unsafe fn add_em_up(&mut self) {
        RealAbstractionWithTonsOfMethods_bar1(&mut *self)
    }
}

But instead we get:

/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct VariadicFunctionObject<T> {
    pub _address: u8,
    pub _phantom_0: ::std::marker::PhantomData<T>,
}

Even without our patch we get the last one so I guess rust-bindgen is not handling variadic function yet.

Can you provide some guidance there?

@emilio
Copy link
Contributor

emilio commented Nov 18, 2016

Yeah, I don't think it would be possible to autogenerate those in rust, mainly because those functions don't always have symbols until they're instantiated. In general any method for a template class can't be automatically generated.

We could generate those for specializations though, but specializations should be in rust too.

@glasserc
Copy link
Contributor

Hmm, so then is this what you meant by "could you write a test with a template and a variadic function here?" Or should the test have the a template and a variadic function but not one inside the other?

@emilio
Copy link
Contributor

emilio commented Nov 18, 2016

Yeah, you should be able to add a test with that, and ensure we're not crashing, the output doesn't need to change :)

@Natim
Copy link
Author

Natim commented Nov 18, 2016

So we can just add the two file in the comment I made?

@emilio
Copy link
Contributor

emilio commented Nov 18, 2016

Yup! That'd be fine :)

@emilio
Copy link
Contributor

emilio commented Nov 18, 2016

Sorry for being confusing, totally my fault :(

@Natim
Copy link
Author

Natim commented Nov 18, 2016

No worries, let us know what you think is the next step then 👍

@Natim Natim force-pushed the 130-cursor-args-return-vector branch from d41c2eb to 3f0f868 Compare November 18, 2016 18:25
@emilio
Copy link
Contributor

emilio commented Nov 18, 2016

Looks good!

@bors-servo r+

@bors-servo
Copy link

📌 Commit 3f0f868 has been approved by emilio

@bors-servo
Copy link

⌛ Testing commit 3f0f868 with merge 1a8a2ac...

bors-servo pushed a commit that referenced this pull request Nov 18, 2016
clang::Cursor::args should return an Option<Vec<Cursor>>

Attempt to fix #130
@bors-servo
Copy link

☀️ Test successful - status-travis

@bors-servo bors-servo merged commit 3f0f868 into rust-lang:master Nov 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang::Cursor::args should return an Option<Vec<Cursor>>
6 participants