Skip to content

-Wnull-pointer-arithmetic and -Wno-gnu #54444

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

Closed
nickdesaulniers opened this issue Mar 18, 2022 · 5 comments
Closed

-Wnull-pointer-arithmetic and -Wno-gnu #54444

nickdesaulniers opened this issue Mar 18, 2022 · 5 comments
Assignees
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party

Comments

@nickdesaulniers
Copy link
Member

The following code w/ ToT LLVM (clang-15) produces the following warnings:

void *x(void) {
    return (char*)0 + 42;
}

void *y(void) {
    return (void*)0 + 42;
}
<source>:2:21: warning: arithmetic on a null pointer treated as a cast from integer to pointer is a GNU extension [-Wnull-pointer-arithmetic]
    return (char*)0 + 42;
           ~~~~~~~~ ^
<source>:6:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
    return (void*)0 + 42;
           ~~~~~~~~ ^

for the Linux kernel, we're using -std=gnu11 -Wno-gnu, so we should not be observing ANY warnings that we're using GNU C extensions.

I suspect that these two checks should be split into two different named command line flags, such that the one pertaining to GNU C extensions is placed within the -Wgnu flag group, such that -Wno-gnu implicitly disables it.

cc @AaronBallman @andykaylor

(link #4440)

As reported in this thread.

@nickdesaulniers nickdesaulniers added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Mar 18, 2022
@efriedma-quic
Copy link
Collaborator

See https://reviews.llvm.org/D37042 for why this warning exists in its current form.

@andykaylor
Copy link
Contributor

Handling this is beyond my knowledge of the front end diagnostics infrastructure. I had even forgotten that I was the person who added this warning. Based on the discussion in the review, it sounds like marking the warning specifically as a GNU extension will cause it to be an error with -pedantic-errors, but not doing so is the reason it isn't suppressed by -Wno-gnu. Is that right?

@AaronBallman Can you help with this?

@AaronBallman
Copy link
Collaborator

I think the issue is that the GNU extension diagnostic was added to the -Wnull-pointer-arithmetic group, which is a part of -Wextra and not -Wgnu. I spotted some other diagnostics in the same boat, so I'll try to get this fixed up.

@nickdesaulniers
Copy link
Member Author

@AaronBallman AaronBallman added the confirmed Verified by a second party label Mar 22, 2022
@AaronBallman
Copy link
Collaborator

This should now be fixed in c3fe8dd.

igormunkin added a commit to luavela/luavela that referenced this issue Apr 14, 2024
Clang 15 changes the behaviour of -Wnull-pointer-arithmetics to control
-Wgnu-null-pointer-arithmetics either. Since uJIT is built with -Wextra
(including -Wnull-pointer-arithmetics) and -Werror flags being set in
CMAKE_C_FLAGS list, compilation of src/utils/lj_alloc.c fails due to
NULL pointer arithmetics in TOP_FOOT_SIZE constant definition.
Furthermore, -Wno-gnu flag to silence GNU extension diagnostics for
pointer arithmetic is allowed since Clang 15 (llvm/llvm-project#54444).

Considering everything above, tweak the CMAKE_C_FLAGS list for Clang
toolchain only for Clang 15.0.0 and later.

It's worth to mention, that fixing the root cause of the warning is also
an option to fix the issue, but it's better to be in sync with the LuaJIT
upstream at this point

Signed-off-by: Igor Munkin <[email protected]>
igormunkin added a commit to luavela/luavela that referenced this issue Apr 14, 2024
Clang 15 changes the behaviour of -Wnull-pointer-arithmetics to control
-Wgnu-null-pointer-arithmetics either. Since uJIT is built with -Wextra
(including -Wnull-pointer-arithmetics) and -Werror flags being set in
CMAKE_C_FLAGS list, compilation of src/utils/lj_alloc.c fails due to
NULL pointer arithmetics in TOP_FOOT_SIZE constant definition.
Furthermore, -Wno-gnu flag to silence GNU extension diagnostics for
pointer arithmetic is allowed since Clang 15 (llvm/llvm-project#54444).

Considering everything above, tweak the CMAKE_C_FLAGS list for Clang
toolchain only for Clang 15.0.0 and later.

It's worth to mention, that fixing the root cause of the warning is also
an option to fix the issue, but it's better to be in sync with the LuaJIT
upstream at this point.

Signed-off-by: Igor Munkin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

5 participants