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

zuo: fix some compiler warnings #4971

Closed
wants to merge 3 commits into from

Conversation

benknoble
Copy link
Contributor

I figured a patch gave a better starting point for discussion, but I admit I'm not confident in some of these changes and I'm opening this mostly to generate conversation.

For the unused parameters, I considered introducing

#define UNUSED(x) (void)x

but didn't know if that fit the project style. I certainly didn't think we want to deal with compiler-specific attributes like __unused.


I often compile with a CFLAGS that looks like

-Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wshadow -Wpointer-arith -Wcast-qual -pedantic -O2 -std=c11

(I actually typically include -Wmissing-prototypes but Zuo produces many warnings from that which are probably harmless.) These changes cover the generated warnings (mostly shadowed variables, unused function arguments, and missing void parameter lists). Interesting cases are in the commit message.

./zuo build.zuo check passes after all these changes are compiled.

Here's the remaining warning:

zuo.c:979:21: warning: comparison of integers of different signs: 'zuo_int32_t' (aka 'int') and 'unsigned long long' [-Wsign-compare]
      for (i = 0; i < len / sizeof(zuo_int32_t); i++)
                  ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~

My understanding of C and integer arithmetic is woefully incomplete, so I'm not sure if casting (on which side of <?) is appropriate or if there is a better solution.

benknoble and others added 2 commits April 14, 2024 17:33
I often compile with a CFLAGS that looks like

-Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wshadow -Wpointer-arith -Wcast-qual -pedantic -O2 -std=c11

(I actually typically include -Wmissing-prototypes but Zuo produces many
warnings from that which are probably harmless.) These changes cover the
generated warnings (mostly shadowed variables, unused function
arguments, and missing void parameter lists). Here's a selection of
interesting cases:
- zuo_read_fail{,2} takes an "s" that is never used. That did _not_
  appear to be the result of compliance with a prototype whose
  implementation was spread across ifdefs, so unless there's some stack
  magic actually reading it, we can remove it. Similar arguments apply
  to zuo_envvars_block's who parameter.
- dispatch_primitive0 _presumably_ uses args when invoking the proc, but
  it might be reliance on stack magic? In any case, the (void)args;
  statement marks them used for the compiler (as in the other cases),
  but the (void) parameter list in the prototype of the case may be
  inappropriate? Idem. for zuo_primitive0.
- zuo_make_void obviously ignores any arguments (like Racket's void); by
  contrast, zuo_shell_to_strings_c needs to conform to a common
  prototype even when all parameters are not needed, so we mark those as
  "used". (The (void) cast idiom is essentially a no-op at any
  optimization level.)
- a few const qualifiers were reintroduced where a cast would have
  dropped them. I am not certain this is correct, but the prototypes of
  the procedures called with the cast values seemed to expect the
  constness.

./zuo build.zuo check passes after all these changes are compiled.

Here's the remaining warning:
zuo.c:979:21: warning: comparison of integers of different signs: 'zuo_int32_t' (aka 'int') and 'unsigned long long' [-Wsign-compare]
      for (i = 0; i < len / sizeof(zuo_int32_t); i++)
                  ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~

My understanding of C and integer arithmetic is woefully incomplete, so
I'm not sure if casting (on which side of <?) is appropriate or if there
is a better solution.
@mflatt
Copy link
Member

mflatt commented Apr 16, 2024

Thanks! These changes look like a good idea to me. I agree about UNUSED, but the style would be to use ZUO_UNUSED.

When I tried compiling with the flags you list, I saw many more warnings on various platforms, so I've pushed another commit to address those. Warnings from -Wmissing-prototypes repersent places where I meant to use static, so those changes are in the extra commit, too. And there was one place where zuo.c didn't compile with old MSVC, so I fixed that. (As far as I can tell, everything still compiles with C89, too.)

@benknoble
Copy link
Contributor Author

Thanks!

  • I saw the cast on the arithmetic warning: I suppose there we're truncating the sizeof's unsigned long long? I guess I would need to stare at that code and the standard a little better to understand it.
  • RE: my commit's comment on primitive dispatching and stack magic, I think I finally realized that those procedures implement differently primitive calling conventions (where 0 means "no arguments). That explains the unused warning better.

@LiberalArtist
Copy link
Contributor

Maybe it would be worth adding a CI action to test these flags, either here or in the Zuo repository.

@benknoble
Copy link
Contributor Author

I'll push a first draft of a CI workflow for that when I get an internet connection for my laptop. It could use some improvements (such as running on a matrix of build platforms).

This is based on the compiler flags and checks used for 8ae70ca (zuo:
fix some compiler warnings, 2024-04-14). There's probably room for more
CI for Zuo (_e.g._, different platforms, ./configure + make, etc.).

Suggested by Philip McGrath <philip@philipmcgrath.com>
@mflatt
Copy link
Member

mflatt commented Apr 20, 2024

I think CI is better (only) at the https://github.com/racket/zuo repo. I'd like to merge this change there, first, anyway.

@benknoble, I could create a new PR at the racket/zuo repo, but I'll leave it to you if you'd prefer to create it. Let me know.

@benknoble
Copy link
Contributor Author

Ah, I misunderstood the preferred direction. I will get this opened over there instead.

@benknoble benknoble closed this Apr 20, 2024
@shhyou shhyou added ci Issue related to the Continuous Integration system build Build failures, Makefiles, Zuo scripts, autoconf, building instructions, etc. labels Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Build failures, Makefiles, Zuo scripts, autoconf, building instructions, etc. ci Issue related to the Continuous Integration system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants