Skip to content
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

Using LYD_VALUE_GET forces to compile with -fpermissive #2177

Open
maxrossello opened this issue Feb 19, 2024 · 10 comments
Open

Using LYD_VALUE_GET forces to compile with -fpermissive #2177

maxrossello opened this issue Feb 19, 2024 · 10 comments
Labels
is:question Issue is actually a question.

Comments

@maxrossello
Copy link

I'm using this code, that's similar to what I can see in file binary.c:

const struct lyd_value *value = ...
struct lyd_value_binary *data;
LYD_VALUE_GET(value, data);

the gcc compiler says:

error: invalid conversion from ‘void*’ to ‘lyd_value_binary*’ [-fpermissive]

I can't find a solution other than adding -fpermissive (which I don't want) because "data" is dereferenced by the macro to know its size, hence I can't pass it as a void*. On the other side, recent versions of gcc don't allow to manipulate the -fpermissive flag locally by using #pragma GCC diagnostic ignored.

@michalvasko
Copy link
Member

michalvasko commented Feb 19, 2024

What kind of error is that? Why would casting void * to any other kind of pointer be a problem? I believe that is its main purpose, to abstract away from the actual pointer type where needed. So I am not sure how to help you, never seen the error myself.

@michalvasko michalvasko added the is:question Issue is actually a question. label Feb 19, 2024
@maxrossello
Copy link
Author

I agree with you, that was also my understanding from the times of Kernigan and Ritchie. Nevertheless, modern compilers seem not to trust the developer and require an explicit cast, that can be introduced on the left side of assignments in the macro

@maxrossello
Copy link
Author

After a bit of searching, looks like it depends from the fact that I am compiling C++

@michalvasko
Copy link
Member

I see, it is starting to make sense. Yes, I leave out the explicit casts on purpose and leave it up to the implicit cast of C, I did not know C++ does not like that (but it is not surprising). So, it seems the macro should have an explicit cast, perhaps typeof can be used here for that? Please test it (and may even create a PR if you want) and I will then commit it.

@jktjkt
Copy link
Contributor

jktjkt commented Feb 19, 2024

Just FYI, in the C++ bindings for libyang, we hit the same problem and simply reimplemented that wrapper. The code is valid C after all, and reimplementing it was a trivial operation that's safe to do because of ABI guarantees.

@maxrossello
Copy link
Author

yes @jktjkt actually I was basically doing the same in origin as a workaround.

The problem with the proposal I envisioned is that casting the left side of the assignment is not possible because it makes it an rvalue. In order to fix it in the C libyang, an #ifdef __cplusplus would be needed I'm afraid, also because anything else would not compile in plain C

@maxrossello
Copy link
Author

this is not easy to solve, because using a template, albeit under #ifdef __cplusplus, also depends not only on the extern "C" statement in the file itself, but also on those in third party including files that would nest in unknown depth.

For the time being I will keep the local rewrite of the wrapper. Thanks a lot for clarification

@michalvasko
Copy link
Member

Maybe I am missing something but why not use typeof, which is supported by C?

@maxrossello
Copy link
Author

maxrossello commented Feb 19, 2024

Well, according to devdocs it seems it was made official in C23, although it has been a gcc extension for a while.

I also tried to use it anyways, but my gcc complained I was using typeof as an implicitly declared function. I don't know whether there are flags to activate it or if it is because of I am using c++, but it looks anyways too risky in my opinion

@michalvasko
Copy link
Member

I see, okay then. I thought it is a standard (and old) C construct just like sizeof but apparently not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is:question Issue is actually a question.
Projects
None yet
Development

No branches or pull requests

3 participants