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

Fix a couple of compilation warnings when building with clang #520

Merged
merged 5 commits into from
Nov 6, 2024

Conversation

mrc0mmand
Copy link
Member

@mrc0mmand mrc0mmand commented Nov 4, 2024

There's still a bunch of warnings caused by -Wdeprecated-declarations, but these can be easily suppressed by -Dc_args=-Wno-deprecated-declarations for now.

This PR also tweaks the CI jobs to build with -Werror, so we should be able to catch these kind of issues a bit earlier in the future.

Declaring variables inside switch statement requires a new block (at
least before the C23 extensions were introduced):

$ CC=clang meson build --werror -Dc_args=-Wno-deprecated-declarations
...
$ ninja -C build
ninja: Entering directory `build'
[24/84] Generating glib marshaller header src/polkitagent/polkitagentmarshal_h
INFO: Reading ../src/polkitagent/polkitagentmarshal.list...
[25/84] Generating glib marshaller source src/polkitagent/polkitagentmarshal_c
INFO: Reading ../src/polkitagent/polkitagentmarshal.list...
[53/84] Compiling C object src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendactionpool.c.o
FAILED: src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendactionpool.c.o
clang -Isrc/polkitbackend/libpolkit-backend-1.a.p -Isrc/polkitbackend -I../src/polkitbackend -I. -I.. -Isrc -I../src -Isrc/polkit -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/sysprof-6 -I/usr/include/gio-unix-2.0 -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Werror -std=c99 -O2 -g -D_GNU_SOURCE -include config.h -Waggregate-return -Wdeclaration-after-statement -Wformat=2 -Wimplicit-function-declaration -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wmissing-prototypes -Wstrict-prototypes -Wno-format-y2k -Wno-declaration-after-statement -Wno-deprecated-declarations -fPIC -pthread -DWITH_GZFILEOP -D_POLKIT_COMPILATION -D_POLKIT_BACKEND_COMPILATION '-DPACKAGE_DATA_DIR="/usr/share"' '-DPACKAGE_SYSCONF_DIR="/etc"' -MD -MQ src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendactionpool.c.o -MF src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendactionpool.c.o.d -o src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendactionpool.c.o -c ../src/polkitbackend/polkitbackendactionpool.c
../src/polkitbackend/polkitbackendactionpool.c:264:7: error: label followed by a declaration is a C23 extension [-Werror,-Wc23-extensions]
  264 |       const gchar **dir_names = (const gchar**) g_value_get_boxed (value);
      |       ^
1 error generated.

Follow-up for 9958c25.
Which allows correct type-checking during compilation and avoids errors
when building with clang + -Werror:

[58/71] Compiling C object src/programs/pkexec.p/pkexec.c.o
FAILED: src/programs/pkexec.p/pkexec.c.o
clang -Isrc/programs/pkexec.p -Isrc/programs -I../src/programs -I. -I.. -Isrc/polkitagent -I../src/polkitagent -Isrc -I../src -Isrc/polkit -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/sysprof-6 -I/usr/include/gio-unix-2.0 -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Werror -std=c99 -O2 -g -D_GNU_SOURCE -include config.h -Waggregate-return -Wdeclaration-after-statement -Wformat=2 -Wimplicit-function-declaration -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wmissing-prototypes -Wstrict-prototypes -Wno-format-y2k -Wno-declaration-after-statement -Wno-deprecated-declarations -pthread -DWITH_GZFILEOP -MD -MQ src/programs/pkexec.p/pkexec.c.o -MF src/programs/pkexec.p/pkexec.c.o.d -o src/programs/pkexec.p/pkexec.c.o -c ../src/programs/pkexec.c
../src/programs/pkexec.c:109:25: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
  109 |   s = g_strdup_vprintf (format, var_args);
      |                         ^~~~~~
1 error generated.
[59/71] Compiling C object src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendauthority.c.o
FAILED: src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendauthority.c.o
clang -Isrc/polkitbackend/libpolkit-backend-1.a.p -Isrc/polkitbackend -I../src/polkitbackend -I. -I.. -Isrc -I../src -Isrc/polkit -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/sysprof-6 -I/usr/include/gio-unix-2.0 -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Werror -std=c99 -O2 -g -D_GNU_SOURCE -include config.h -Waggregate-return -Wdeclaration-after-statement -Wformat=2 -Wimplicit-function-declaration -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wmissing-prototypes -Wstrict-prototypes -Wno-format-y2k -Wno-declaration-after-statement -Wno-deprecated-declarations -fPIC -pthread -DWITH_GZFILEOP -D_POLKIT_COMPILATION -D_POLKIT_BACKEND_COMPILATION '-DPACKAGE_DATA_DIR="/usr/share"' '-DPACKAGE_SYSCONF_DIR="/etc"' -MD -MQ src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendauthority.c.o -MF src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendauthority.c.o.d -o src/polkitbackend/libpolkit-backend-1.a.p/polkitbackendauthority.c.o -c ../src/polkitbackend/polkitbackendauthority.c
../src/polkitbackend/polkitbackendauthority.c:1721:31: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
 1721 |   message = g_strdup_vprintf (format, var_args);
      |                               ^~~~~~
1 error generated.
@mrc0mmand mrc0mmand changed the title Fix a couple of compilationq warnings when building with clang Fix a couple of compilation warnings when building with clang Nov 4, 2024
@mrc0mmand mrc0mmand force-pushed the fix-clang-warnings branch 3 times, most recently from 305b3cd to f2e7683 Compare November 4, 2024 13:46
setre*id() functions are declared with `warn_unused_result` which makes
them emit a warning when their result is unused. This warning can be
suppressed by using (void) cast in clang, so do this where appropriate.
As gcc doesn't support this, disable the warning completely when gcc is
used.

See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
Suppress the outstanding -Wdeprecated-declarations warnings for now, so
we don't miss real issues in the future.
These were fixed by dropping mocklibc.
@evverx
Copy link

evverx commented Nov 4, 2024

I think it would be great if it was possible to bring back clang in https://packit.dev/posts/openscanhub-prototype to be able to see all the clang warnings fixed in PRs (when -Werror isn't an option). As far as I can remember it was discussed somewhere but I think clang was turned off to reduce the number of false positives in projects using cleanup attributes.

@jrybar-rh jrybar-rh merged commit fdf9172 into polkit-org:main Nov 6, 2024
33 checks passed
@mrc0mmand mrc0mmand deleted the fix-clang-warnings branch November 6, 2024 16:10
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.

3 participants