Skip to content

Stricter C99 warnings, strict prototypes for primitives list #11861

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
Jan 13, 2023

Conversation

MisterDA
Copy link
Contributor

@MisterDA MisterDA commented Jan 3, 2023

This PR is a simplified version of #11763, where we don't use the actual types of the primitives when generating the list of primitives, because we end up casting the primitives to a default type anyway (as noted by David Allsopp).
I've retained some of the changes I made to the Makefile, because I think they're easier to understand than the current version.

cc @dra27 @xavierleroy, reviewers of the previous PR.

@shindere
Copy link
Contributor

shindere commented Jan 10, 2023 via email

@shindere
Copy link
Contributor

One other possible way to deal with the LC_* variables would be to add
something like this e.g. to Makefile.comon:

# Avoid funny character set dependencies
unexport LC_ALL
LC_COLLATE=C
LC_NUMERIC=C
export LC_COLLATE LC_NUMERIC

(borrowed from the Makefile of the Linux kernel so I guess it's safe to use)

@xavierleroy
Copy link
Contributor

I've retained some of the changes I made to the Makefile, because I think they're easier to understand than the current version.

Could you please remove those changes? Three reasons:
1- these changes are orthogonal to the purpose of this PR, namely modernizing the C code of the runtime system;
2- these changes are problematic, as @shindere outlined;
3- there's too much churn in Makefiles right now, so let's not change something that is not broken, merely hard to read.

Without those Makefile changes I'll be happy to approve and merge this PR.

@shindere
Copy link
Contributor

shindere commented Jan 11, 2023 via email

@xavierleroy
Copy link
Contributor

Isn't this a bit contradictory with #11844? ;-)

Well, there's a budget for Makefile churn, and I'm happier to spend it on #11844 than on this PR. Plus, I'm pretty sure #11844 doesn't change the build itself, just what is displayed during the build, while I'm less sure here.

Fedora is considering moving all its packages to C99 and enabling
stricter C99 warnings. OCaml has already moved to a compiler requiring
C11 features, let's enable these warnings on GCC-compatible compilers.

Some of the warnings on the following article are already covered by
-Wall and -Werror.

Reference: [Modernizing Fedora's C code][1].

[1]: https://lwn.net/Articles/913505/
It is always possible to cast a function pointer to another function
pointer type, and cast again to the original type before calling the
pointed function. I pick the base

    typedef value (*c_primitive)(void);

to store the functions in the array, and cast them back in the
interpreter according to the arity described by the opcode.

Some references:
- [Function pointers in C][1]
- [Function pointer casts][2]

[1]: https://frama-c.com/2013/08/24/Function-pointers-in-C.html
[2]: https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

The primitives function pointers can be stored as simple void
pointers:

(Comment by Xavier Leroy: ocaml#11763 (comment))

> Note that ISO C forbids casting a function pointer to type void *,
> but we've been doing this for a very long time, and all C compilers
> that I know of are happy with this.

This is documented by C11 Standard J.5.7, Function pointer casts.

The prototypes in `prims.c` and the headers differ, but:

(Comment by Xavier Leroy: ocaml#11763 (comment))

> On the one hand, it's not legal C11 to declare value f(void) a
> function f that is defined in another compilation unit as value
> f(value), say; while I think it is correct to declare it value f(),
> like we currently do. On the other hand, this is no worse than the
> many other "not legal C11" things we already do.
@MisterDA MisterDA force-pushed the stricter-c99-primitives-void branch from 484021b to 8db1817 Compare January 12, 2023 14:06
@MisterDA
Copy link
Contributor Author

Thank you @shindere for the nice review, I've added your suggestions for the commit message. I like to keep longer comments and references in the commit message as not to add too much info in the code.

Following Xavier's message I've removed the commits touching the Makefile from this PR. Someone might want to revisit the idea later.

This might be good to go now?

Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

I'm in a merging mood right now so I decided to interpret the following words of Xavier

Without those Makefile changes I'll be happy to approve and merge this PR.

as saying that I can approve on his behalf now that the changes he requested have been performed.

@gasche gasche merged commit 0509300 into ocaml:trunk Jan 13, 2023
@MisterDA MisterDA deleted the stricter-c99-primitives-void branch January 13, 2023 08:29
dra27 pushed a commit to dra27/ocaml that referenced this pull request Jul 29, 2024
…void

Stricter C99 warnings, strict prototypes for primitives list

(cherry picked from commit 0509300)
dra27 pushed a commit to dra27/ocaml that referenced this pull request Jul 29, 2024
…void

Stricter C99 warnings, strict prototypes for primitives list

(cherry picked from commit 0509300)
dra27 pushed a commit to dra27/ocaml that referenced this pull request Jul 29, 2024
…void

Stricter C99 warnings, strict prototypes for primitives list

(cherry picked from commit 0509300)
dra27 pushed a commit to dra27/ocaml that referenced this pull request Jul 30, 2024
…void

Stricter C99 warnings, strict prototypes for primitives list

(cherry picked from commit 0509300)
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.

4 participants