Skip to content

Commit

Permalink
Don't emit pointer to int cast warnings under -Wmicrosoft-cast
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aeubanks committed Mar 9, 2020
1 parent 1f5b471 commit 51d7f64
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
3 changes: 0 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -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">,
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Sema/SemaCast.cpp
Expand Up @@ -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())
Expand Down
15 changes: 7 additions & 8 deletions 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
{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 51d7f64

Please sign in to comment.