Convert various macros to functions#1729
Conversation
Youw
left a comment
There was a problem hiding this comment.
Can you update the PR description with the motivation for this change?
The motivation is the usual one when it comes to C macros: no type safety, easy footguns, etc. etc. Any compiler today will certainly inline a 1 line macro, so its only advantage is gone. |
I totally get that, but I generally encourage people to be disciplined-enough to write such things down (specially in the PR description) for other developers who may have different point of view, or may try to assume things like "maybe this is a preparation for something specific, and not just a refactoring/improvement". |
|
This highlights one more question from my side: should all these functins have a libusb-specific prefix, e.g.: |
Looking at core.c, many do, but not all. I'd leave that for some other PR or discussion... |
|
Ah, one missing file to update. |
That file is not in the libusb repo, it seems to be from here: https://github.com/google/oss-fuzz/blob/c09755737ee2d135fb003b0fb02cc79e5c7c4fdb/projects/libusb/libusb_fuzzer.cc#L21 they include Perhaps this is an argument for not renaming these macros/functions. |
I see.
One possibility is that we merge this PR first and then ask oss-fuzz to update. But then there may be other such cases, so in the end we may have to keep the namaes of such macros/functions. |
|
Let's hear from @sonatique and @tormodvolden as well on this topic. |
We could have functions with new names, and have macros as aliases for functions. This way we don't use UPPER_CAMEL_CASE for function names (directly) and still have backwarc compatibility. |
|
Thanks @seanm : I went through the full diff and the conversion is faithful (the one semantic change,
|
|
|
@seanm : I concur with"not rename" for point 2. |
- For USBI_TRANSFER_TO_TRANSFER_PRIV use void* as the return type since indeed the struct is different for each backend, and not a char* - For USBI_TRANSFER_TO_LIBUSB_TRANSFER, use the size from usbi_backend.transfer_priv_size, just like the inverse macro does. This makes things symetric and makes it possible to convert this to a function since `struct usbi_transfer` is not a macro parameter, but just resolved after preprocessing
cb3adc8 to
a75700d
Compare
Only 1 caller needed to be changed, since it was not acting as a function. Functions provide type safety vs macros, and macros are renowned footguns.
Done. And I think I've done 4 too. |
|
I think strictly correct and compatible way would be to do: seanm#1 |
Macros are well-known footguns. Functions provide type safety.