Excessive/inconsistent reporting with -Wimplicit-void-ptr-cast
This issue appeared after pull request: https://github.com/llvm/llvm-project/pull/136855 I have noticed excessive reporting with constant NULL pointer for the new C++ compatibly warnings implemented in https://github.com/llvm/llvm-project/commit/15321d2c9e686b382262339fa17c5445b1b2609f.
Please see the following code:
__attribute__((address_space(0))) char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (__attribute__((address_space(0))) void*)0;
return (c > 0) & (b != 0);
}
When compiled with following line:
clang -target amdgcn -Wimplicit-void-ptr-cast /testcode/llvm-clean/test.c -o a.out
The following warnings are produced:
/testcode/llvm-clean/test.c:11:7: warning: implicit conversion when assigning to '__attribute__((address_space(21))) char *' from type '__attribute__((address_space(21))) void *' is not permitted in C++ [-Wimplicit-void-ptr-cast]
11 | c = (__attribute__((address_space(21))) void*)0x25;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/testcode/llvm-clean/test.c:13:7: warning: implicit conversion when assigning to '__attribute__((address_space(0))) char *' from type '__attribute__((address_space(0))) void *' is not permitted in C++ [-Wimplicit-void-ptr-cast]
13 | b = (__attribute__((address_space(0))) void*)0;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However when i compile slightly different code:
__attribute__((address_space(0))) char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (void*)0;
return (c > 0) & (b != 0);
}
or
char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (void*)0;
return (c > 0) & (b != 0);
}
no warnings regarding assignment of variable b are generated.
Please note in C++ mode all 3 assignments are invalid and thus should be generating the exactly same diagnostics.
Please also note the behavior for non-null constant c is consistent.
I believe this issue should be addressed in the way either NULL assignment never generate a warning or always generate a warning. After some deliberations i believe it should always generate a warning meaning cases 2&3 returning insufficient diagnostics.
PS: Sorry for not giving godbolt examples but godbolt does not seems support amdgcn target :( PPS: All testing initially was done with https://github.com/llvm/llvm-project/commit/74593f667823025580e046e56d48d7a9899b8956
@llvm/issue-subscribers-clang-frontend
Author: None (earnol)
Please see the following code:
__attribute__((address_space(0))) char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (__attribute__((address_space(0))) void*)0;
return (c > 0) & (b != 0);
}
When compiled with following line:
clang -target amdgcn -Wimplicit-void-ptr-cast /testcode/llvm-clean/test.c -o a.out
The following warnings are produced:
/testcode/llvm-clean/test.c:11:7: warning: implicit conversion when assigning to '__attribute__((address_space(21))) char *' from type '__attribute__((address_space(21))) void *' is not permitted in C++ [-Wimplicit-void-ptr-cast]
11 | c = (__attribute__((address_space(21))) void*)0x25;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/testcode/llvm-clean/test.c:13:7: warning: implicit conversion when assigning to '__attribute__((address_space(0))) char *' from type '__attribute__((address_space(0))) void *' is not permitted in C++ [-Wimplicit-void-ptr-cast]
13 | b = (__attribute__((address_space(0))) void*)0;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However when i compile slightly different code:
__attribute__((address_space(0))) char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (void*)0;
return (c > 0) & (b != 0);
}
or
char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (void*)0;
return (c > 0) & (b != 0);
}
no warnings regarding assignment of variable b are generated.
Please note in C++ mode all 3 assignments are invalid and thus should be generating the exactly same diagnostics.
Please also note the behavior for non-null constant c is consistent.
I believe this issue should be addressed in the way either NULL assignment never generate a warning or always generate a warning. After some deliberations i believe it should always generate a warning meaning cases 2&3 returning insufficient diagnostics.
PS: Sorry for not giving godbolt examples but godbolt does not seems support amdgcn target :( PPS: All testing initially was done with https://github.com/llvm/llvm-project/commit/74593f667823025580e046e56d48d7a9899b8956
@llvm/issue-subscribers-c
Author: None (earnol)
Please see the following code:
__attribute__((address_space(0))) char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (__attribute__((address_space(0))) void*)0;
return (c > 0) & (b != 0);
}
When compiled with following line:
clang -target amdgcn -Wimplicit-void-ptr-cast /testcode/llvm-clean/test.c -o a.out
The following warnings are produced:
/testcode/llvm-clean/test.c:11:7: warning: implicit conversion when assigning to '__attribute__((address_space(21))) char *' from type '__attribute__((address_space(21))) void *' is not permitted in C++ [-Wimplicit-void-ptr-cast]
11 | c = (__attribute__((address_space(21))) void*)0x25;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/testcode/llvm-clean/test.c:13:7: warning: implicit conversion when assigning to '__attribute__((address_space(0))) char *' from type '__attribute__((address_space(0))) void *' is not permitted in C++ [-Wimplicit-void-ptr-cast]
13 | b = (__attribute__((address_space(0))) void*)0;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However when i compile slightly different code:
__attribute__((address_space(0))) char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (void*)0;
return (c > 0) & (b != 0);
}
or
char *b;
__attribute__((address_space(21))) char *c;
int main(int argc, char *argv[])
{
c = (__attribute__((address_space(21))) void*)0x25;
b = (void*)0;
return (c > 0) & (b != 0);
}
no warnings regarding assignment of variable b are generated.
Please note in C++ mode all 3 assignments are invalid and thus should be generating the exactly same diagnostics.
Please also note the behavior for non-null constant c is consistent.
I believe this issue should be addressed in the way either NULL assignment never generate a warning or always generate a warning. After some deliberations i believe it should always generate a warning meaning cases 2&3 returning insufficient diagnostics.
PS: Sorry for not giving godbolt examples but godbolt does not seems support amdgcn target :( PPS: All testing initially was done with https://github.com/llvm/llvm-project/commit/74593f667823025580e046e56d48d7a9899b8956
The issue has to do with checking for null pointer constants specifically: https://github.com/llvm/llvm-project/blob/acc40ed0a747ad8f5dbb93788da7ce104c36c0a3/clang/lib/Sema/SemaExpr.cpp#L9815
We check the pointer conversion, do an implicit cast, and then return they're compatible before getting to the call to CheckAssignmentConstraints() further down. That is the call which determines we've hit the conversion we need to diagnose.
@AaronBallman (and @earnol), there is one piece missing in this issue as far as I can tell.
The original problem we saw downstream (IMHO) was about different behaviors depending on address spaces. Probably due to nullptr:s in the non-default address space isn't detected as null pointers by clang.
If we look at this example
void nullptr_in_addrspace_0()
{
char *b;
b = (void *)0;
}
#define as7 __attribute__((address_space(7)))
void nullptr_in_addrspace_7()
{
as7 char *b;
b = (as7 void *)0;
}
we get the implicit-void-ptr-cast warning for the nullptr_in_addrspace_7 function.
I thought that this issue was about that problem, and the question if the warning shouldn't be omitted in both functions (or none of them) to get a consistent behavior. But maybe that is related to if address zero is allowed or not in the non-default address space?
Our downstream issue that is linked to this ticket is at least about the problem with non-default addressspaces and void ptr cast. So I kind of expected that this issue would be about that problem as well. But the examples in this issue, as well as the test cases added in the fix (https://github.com/llvm/llvm-project/pull/138271) all seem to be about null pointers in address space 0. That was kind of a surprise to me.
Should we perhaps write a new issue about the non-default-address-space problems?
I feel like I must be missing something, but it's early enough in the day I could just not be fully awake yet. ;-)
The current behavior is what I would expect with trunk: https://godbolt.org/z/3n3z8e3aj Specifically: the code is acceptable in C, so we don't warn there. The code is ill-formed in C++, so we issue an error there. So in C, with -Wc++-compat, we diagnose as a C++ compatibility issue.
It sounds to me like you expect the C behavior in C++, and therefore you don't think this should get a warning under -Wc++-compat? Or am I misunderstanding?
The logic behind -Wc++-compat is fairly simple. If the same code compilation in C++ mode generate an error for being non-C++ compliant, it should generate a warning.
In C++ mode both cases of null pointer assignments:
- with address spaces attribute
- without address space attribute
generate an error when being assigned to non-void pointer. If there is a mismatch in address space another error is generated, but this case is invalid too.
Thus, while initial issue was created about inconsistency the actual desired outcome is to follow logic highlighted by the italic font. It also means your example for function nullptr_in_addrspace_7 should generate a warning as it is generating an error in C++ compilation mode.
The example which should NOT generate warning is:
#define as7 __attribute__((address_space(7)))
void nullptr_in_addrspace_7_cpp()
{
as7 char *b;
b = (as7 char *)0;
}
and as i observe it works as expected and does not produce any warnings.
Since C23 standard there is a compatible solution use of nullptr. Please refer to https://en.cppreference.com/w/c/language/nullptr. And according to my tests:
#define as7 __attribute__((address_space(7)))
void nullptr_in_addrspace_7_cpp()
{
as7 char *b;
b = nullptr;
}
also does not produce any warning as expected.
It was not clear to me if nullptr was some kind of special case that would hide the warning or not.
Apparently the fix was to also give the warning for address space zero, while previously we only got the warning for nullptr_in_addrspace_7 when looking at https://godbolt.org/z/PjqvMTc63
When I checked with godbolt this morning trunk must have been behind a bit, so it did not include the fix from https://github.com/llvm/llvm-project/pull/138271 . That is why I still thought that there was an inconsistency. Now when "trunk" has moved forward a bit I see that the behavior is the same regardless of address space. So now it make more sense and the behavior is consistent regardless of address spaces for the given example.