[clang] 51d7f64 - Don't emit pointer to int cast warnings under -Wmicrosoft-cast

Arthur Eubanks via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 9 13:27:59 PDT 2020


Author: Arthur Eubanks
Date: 2020-03-09T13:27:46-07:00
New Revision: 51d7f645448b49c7d3b029a60532bac019da1348

URL: https://github.com/llvm/llvm-project/commit/51d7f645448b49c7d3b029a60532bac019da1348
DIFF: https://github.com/llvm/llvm-project/commit/51d7f645448b49c7d3b029a60532bac019da1348.diff

LOG: Don't emit pointer to int cast warnings under -Wmicrosoft-cast

Summary:
MSVC also warns on this:
$ cat /tmp/a.c
int f(void* p) { return (int) p; }

$ cl /c /tmp/a.c
C:/src/tmp/a.c(1): warning C4311: 'type cast': pointer truncation from
'void *' to 'int'

Warnings originally added in https://reviews.llvm.org/D72231.

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D75643

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaCast.cpp
    clang/test/Sema/MicrosoftExtensions.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6c73917e2eac..f4ed6b2e649e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3682,9 +3682,6 @@ def warn_void_pointer_to_int_cast : Warning<
 def warn_void_pointer_to_enum_cast : Warning<
   warn_void_pointer_to_int_cast.Text>,
   InGroup<VoidPointerToEnumCast>;
-def ext_ms_pointer_to_int_cast : ExtWarn<
-  "cast to smaller integer type %1 from %0 is a Microsoft extension">,
-InGroup<MicrosoftCast>;
 
 def warn_attribute_ignored_for_field_of_type : Warning<
   "%0 attribute ignored for field of type %1">,

diff  --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index 8edff2439e0d..2d0a2298329e 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -2778,9 +2778,7 @@ void CastOperation::CheckCStyleCast() {
       // is undefined. The result need not be in the range of values of any
       // integer type.
       unsigned Diag;
-      if (Self.getLangOpts().MicrosoftExt)
-        Diag = diag::ext_ms_pointer_to_int_cast;
-      else if (SrcType->isVoidPointerType())
+      if (SrcType->isVoidPointerType())
         Diag = DestType->isEnumeralType() ? diag::warn_void_pointer_to_enum_cast
                                           : diag::warn_void_pointer_to_int_cast;
       else if (DestType->isEnumeralType())

diff  --git a/clang/test/Sema/MicrosoftExtensions.c b/clang/test/Sema/MicrosoftExtensions.c
index 8e7087a57991..166ba7f6c0a2 100644
--- a/clang/test/Sema/MicrosoftExtensions.c
+++ b/clang/test/Sema/MicrosoftExtensions.c
@@ -1,5 +1,4 @@
-// RUN: %clang_cc1 -triple i686-windows %s -fsyntax-only -Wno-unused-value -Wmicrosoft -verify -fms-extensions
-
+// RUN: %clang_cc1 -triple i686-windows %s -fsyntax-only -Wno-unused-value -Wno-pointer-to-int-cast -Wmicrosoft -verify -fms-extensions
 
 struct A
 {
@@ -93,13 +92,13 @@ enum : long long {  // expected-warning{{enumeration types with a fixed underlyi
 };
 
 void pointer_to_integral_type_conv(char* ptr) {
-   char ch = (char)ptr; // expected-warning{{cast to smaller integer type 'char' from 'char *' is a Microsoft extension}}
-   short sh = (short)ptr; // expected-warning{{cast to smaller integer type 'short' from 'char *' is a Microsoft extension}}
-   ch = (char)ptr; // expected-warning{{cast to smaller integer type 'char' from 'char *' is a Microsoft extension}}
-   sh = (short)ptr; // expected-warning{{cast to smaller integer type 'short' from 'char *' is a Microsoft extension}}
+  char ch = (char)ptr;
+  short sh = (short)ptr;
+  ch = (char)ptr;
+  sh = (short)ptr;
 
-   // This is valid ISO C.
-   _Bool b = (_Bool)ptr;
+  // This is valid ISO C.
+  _Bool b = (_Bool)ptr;
 }
 
 typedef struct {


        


More information about the cfe-commits mailing list