Author: Alexey Kreshchuk Date: 2022-11-23T18:43:06Z New Revision: 2cea4c239570c37f46ad0003b3d41d9473aca60f
URL: https://github.com/llvm/llvm-project/commit/2cea4c239570c37f46ad0003b3d41d9473aca60f DIFF: https://github.com/llvm/llvm-project/commit/2cea4c239570c37f46ad0003b3d41d9473aca60f.diff LOG: 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 Added: Modified: clang/lib/Sema/SemaFixItUtils.cpp clang/test/FixIt/fixit-function-call.cpp Removed: ################################################################################ diff --git a/clang/lib/Sema/SemaFixItUtils.cpp b/clang/lib/Sema/SemaFixItUtils.cpp index 2910a56f866bb..2c85a53194301 100644 --- a/clang/lib/Sema/SemaFixItUtils.cpp +++ b/clang/lib/Sema/SemaFixItUtils.cpp @@ -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) { diff --git a/clang/test/FixIt/fixit-function-call.cpp b/clang/test/FixIt/fixit-function-call.cpp index 273e4a41ec8dd..88f636ea5859d 100644 --- a/clang/test/FixIt/fixit-function-call.cpp +++ b/clang/test/FixIt/fixit-function-call.cpp @@ -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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits