Do not suggest taking the address of a const pointer to get void*

It's more likely the user needs a const cast, but probably not sure
enough that we should suggest that either - so err on the side of
caution and offer no suggestion.

Fixes pr58958

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D138426
This commit is contained in:
Alexey Kreshchuk 2022-11-22 23:47:20 +00:00 committed by David Blaikie
parent f286af29d8
commit 2cea4c2395
2 changed files with 26 additions and 1 deletions

View File

@ -124,7 +124,7 @@ bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
// Check if the pointer to the argument needs to be passed:
// (type -> type *) or (type & -> type *).
if (isa<PointerType>(ToQTy)) {
if (const auto *ToPtrTy = dyn_cast<PointerType>(ToQTy)) {
bool CanConvert = false;
OverloadFixItKind FixKind = OFIK_TakeAddress;
@ -132,6 +132,10 @@ bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
if (!Expr->isLValue() || Expr->getObjectKind() != OK_Ordinary)
return false;
// Do no take address of const pointer to get void*
if (isa<PointerType>(FromQTy) && ToPtrTy->isVoidPointerType())
return false;
CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy, S,
Begin, VK_PRValue);
if (CanConvert) {

View File

@ -115,4 +115,25 @@ void dbcaller(A *ptra, B *ptrb, C &c, B &refb) {
u(c);
}
void accept_void(void*);
void issue58958(const char* a, volatile char * v, const volatile char * cv) {
// CHECK: no matching function for call to 'accept_void'
// CHECK-NOT: take the address of the argument with &
accept_void(a);
// CHECK: no matching function for call to 'accept_void'
// CHECK-NOT: take the address of the argument with &
accept_void(v);
// CHECK: no matching function for call to 'accept_void'
// CHECK-NOT: take the address of the argument with &
accept_void(cv);
char b;
// CHECK: no matching function for call to 'accept_void'
// CHECK: take the address of the argument with &
accept_void(b);
// CHECK-NOT: no matching function for call to 'accept_void'
// CHECK-NOT: take the address of the argument with &
accept_void(&b);
}
// CHECK: errors generated