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

Functional C macros are not expanded. #753

Open
nicokoch opened this Issue Jun 16, 2017 · 13 comments

Comments

Projects
None yet
5 participants
@nicokoch

nicokoch commented Jun 16, 2017

So I'm generating bindings to <linux/uinput>. This works pretty well, but two c macros in particular do not produce any bindings in the output. Reduced input below:

Input C/C++ Header

#define UI_DEV_CREATE		_IO(UINPUT_IOCTL_BASE, 1)
#define UI_DEV_DESTROY		_IO(UINPUT_IOCTL_BASE, 2)

Bindgen Invocation

let bindings = bindgen::Builder::default()
        .no_unstable_rust()
        .header("wrapper/wrapper.h")
        .expect("Unable to generate bindings").

wrapper.h looks like this:

#include <linux/uinput.h>

Actual Results

No constant UI_DEV_CREATE or UI_DEV_DESTROY generated in output.

Expected Results

Constants UI_DEV_CREATE and UI_DEV_DESTROY generated in output.

I'm not really that familiar with the kernel macro _IO and not sure if this is technically even possible, but I figured to report just to get a more professional opinion. If it's not possible, workarounds welcome.

@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

How is _IO defined? Grepping define _IO in my /usr/include/linux dir didn't yield anything.

@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

As in... If we don't know how _IO is defined, how could we generate a sensible constant?

@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

That being said, even with that it doesn't work (huh, I'd swear we knew how to deal with macro expansions):

#define _IO(a_) (a_)

#define UI_DEV_CREATE _IO(1)
#define UI_DEV_DESTROY _IO(2)

@emilio emilio added the enhancement label Jun 16, 2017

@emilio emilio changed the title Nested c macro does not generate any output Functional C macros are not expanded. Jun 16, 2017

@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

This is probably a bug for rust-cexpr. Indeed, it's a dupe of jethrogb/rust-cexpr#3.

@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

I guess I can try to add to libclang a way to get whether the macro definition belongs to a functional macro...

@nicokoch

This comment has been minimized.

nicokoch commented Jun 16, 2017

If it helps any:
_IO is defined in uapi/asm-generic/ioctl.h as:
#define _IO(type,nr) _IOC(_IOC_NONE,(type),(nr),0)
and _IOC in turn is defined as:

#define _IOC(dir,type,nr,size) \
      (((dir)  << _IOC_DIRSHIFT) | \
      ((type) << _IOC_TYPESHIFT) | \
      ((nr)   << _IOC_NRSHIFT) | \
      ((size) << _IOC_SIZESHIFT))
@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

Ok, so libclang provides a clang_Cursor_isMacroFunctionLike API. Perhaps we could use that.

@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

I've asked @jethrogb about what the best API for this would be. It shouldn't be a big deal to support it in bindgen once cexpr support is there.

@nicokoch

This comment has been minimized.

nicokoch commented Jun 16, 2017

Meanwhile, I used the following in my wrapper.h to workaround the issue:

#include <linux/uinput.h>

const __u64 _UI_DEV_CREATE = UI_DEV_CREATE;
#undef UI_DEV_CREATE
const __u64 UI_DEV_CREATE = _UI_DEV_CREATE;

const __u64 _UI_DEV_DESTROY = UI_DEV_DESTROY;
#undef UI_DEV_DESTROY
const __u64 UI_DEV_DESTROY = _UI_DEV_DESTROY;
@emilio

This comment has been minimized.

Collaborator

emilio commented Jun 16, 2017

Yeah, note that that would only work in libclang 3.9+ though.

@jethrogb

This comment has been minimized.

Contributor

jethrogb commented Jul 26, 2017

Current state: C preprocessor needed. @emilio can you add "help wanted" label?

@clia

This comment has been minimized.

clia commented Oct 22, 2018

How about the progress of this?
I'm generating the PostgreSQL header files using bindgen, but in the output files there're a lot of macros being lost.

@jethrogb

This comment has been minimized.

Contributor

jethrogb commented Oct 22, 2018

@clia The state is unchanged as of my latest comment #753 (comment). This issue is marked "help wanted" and waiting for someone (anyone) to implement this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment