From: David Majnemer Date: Sat, 31 Oct 2015 08:42:14 +0000 (+0000) Subject: [MSVC Compat] Permit conversions from pointer-to-function to pointer-to-object iff... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b9eb8456fe0f69cba2b2e30dd20b051e293bde9e;p=clang [MSVC Compat] Permit conversions from pointer-to-function to pointer-to-object iff -fms-compatibility We permit implicit conversion from pointer-to-function to pointer-to-object when -fms-extensions is specified. This is rather unfortunate, move this into -fms-compatibility and only permit it within system headers unless -Wno-error=microsoft-cast is specified. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251738 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 788c5789f8..97910798f1 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2635,6 +2635,10 @@ def warn_impcast_null_pointer_to_integer : Warning< def warn_impcast_floating_point_to_bool : Warning< "implicit conversion turns floating-point number into bool: %0 to %1">, InGroup; +def ext_ms_impcast_fn_obj : ExtWarn< + "implicit conversion between pointer-to-function and pointer-to-object is a " + "Microsoft extension">, + InGroup, DefaultError, SFINAEFailure; def warn_impcast_pointer_to_bool : Warning< "address of%select{| function| array}0 '%1' will always evaluate to " diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 084e25a1cb..52c0beac9c 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -2104,7 +2104,7 @@ bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, } // MSVC allows implicit function to void* type conversion. - if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && + if (getLangOpts().MSVCCompat && FromPointeeType->isFunctionType() && ToPointeeType->isVoidType()) { ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, ToPointeeType, @@ -2666,6 +2666,14 @@ bool Sema::CheckPointerConversion(Expr *From, QualType ToType, // The conversion was successful. Kind = CK_DerivedToBase; } + + if (!IsCStyleOrFunctionalCast && FromPointeeType->isFunctionType() && + ToPointeeType->isVoidType()) { + assert(getLangOpts().MSVCCompat && + "this should only be possible with MSVCCompat!"); + Diag(From->getExprLoc(), diag::ext_ms_impcast_fn_obj) + << From->getSourceRange(); + } } } else if (const ObjCObjectPointerType *ToPtrType = ToType->getAs()) { diff --git a/test/SemaCXX/MicrosoftCompatibility-cxx98.cpp b/test/SemaCXX/MicrosoftCompatibility-cxx98.cpp index 626381b35a..85123b54b4 100644 --- a/test/SemaCXX/MicrosoftCompatibility-cxx98.cpp +++ b/test/SemaCXX/MicrosoftCompatibility-cxx98.cpp @@ -12,3 +12,12 @@ void (*PR23733_1)() = static_cast((void *)0); // expected-warning {{sta void (*PR23733_2)() = FnPtrTy((void *)0); void (*PR23733_3)() = (FnPtrTy)((void *)0); void (*PR23733_4)() = reinterpret_cast((void *)0); + +long function_prototype(int a); +long (*function_ptr)(int a); + +void function_to_voidptr_conv() { + void *a1 = function_prototype; // expected-warning {{implicit conversion between pointer-to-function and pointer-to-object is a Microsoft extension}} + void *a2 = &function_prototype; // expected-warning {{implicit conversion between pointer-to-function and pointer-to-object is a Microsoft extension}} + void *a3 = function_ptr; // expected-warning {{implicit conversion between pointer-to-function and pointer-to-object is a Microsoft extension}} +} diff --git a/test/SemaCXX/MicrosoftExtensions.cpp b/test/SemaCXX/MicrosoftExtensions.cpp index 3d11c20148..22cf2be7c1 100644 --- a/test/SemaCXX/MicrosoftExtensions.cpp +++ b/test/SemaCXX/MicrosoftExtensions.cpp @@ -153,16 +153,6 @@ static void static_func() // expected-warning {{redeclaring non-static 'static_f extern const int static_var; // expected-note {{previous declaration is here}} static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}} -long function_prototype(int a); -long (*function_ptr)(int a); - -void function_to_voidptr_conv() { - void *a1 = function_prototype; - void *a2 = &function_prototype; - void *a3 = function_ptr; -} - - void pointer_to_integral_type_conv(char* ptr) { char ch = (char)ptr; short sh = (short)ptr;