]> granicus.if.org Git - clang/commitdiff
[MSVC Compat] Permit conversions from pointer-to-function to pointer-to-object iff...
authorDavid Majnemer <david.majnemer@gmail.com>
Sat, 31 Oct 2015 08:42:14 +0000 (08:42 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Sat, 31 Oct 2015 08:42:14 +0000 (08:42 +0000)
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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaOverload.cpp
test/SemaCXX/MicrosoftCompatibility-cxx98.cpp
test/SemaCXX/MicrosoftExtensions.cpp

index 788c5789f85a25ffe2242fa166968a362b463ade..97910798f1ea5e5d5ee713ddc5088199ce327e7e 100644 (file)
@@ -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<ImplicitConversionFloatingPointToBool>;
+def ext_ms_impcast_fn_obj : ExtWarn<
+  "implicit conversion between pointer-to-function and pointer-to-object is a "
+  "Microsoft extension">,
+  InGroup<MicrosoftCast>, DefaultError, SFINAEFailure;
 
 def warn_impcast_pointer_to_bool : Warning<
     "address of%select{| function| array}0 '%1' will always evaluate to "
index 084e25a1cbbfebf45133bc9b59c1ee7213562489..52c0beac9ca69b8f00a63fab8a102b8a2dc1cbce 100644 (file)
@@ -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<ObjCObjectPointerType>()) {
index 626381b35a2cd6a7adc2d3f0ab2d1cbf593099d0..85123b54b4d08673c76ab14aa217545c2f636346 100644 (file)
@@ -12,3 +12,12 @@ void (*PR23733_1)() = static_cast<FnPtrTy>((void *)0); // expected-warning {{sta
 void (*PR23733_2)() = FnPtrTy((void *)0);
 void (*PR23733_3)() = (FnPtrTy)((void *)0);
 void (*PR23733_4)() = reinterpret_cast<FnPtrTy>((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}}
+}
index 3d11c20148aad0611e23913f96f7b23327742952..22cf2be7c1ac927b19ee21f0d90a58e291b09836 100644 (file)
@@ -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;