From 56eb15a1c71061043d50aa669407816bc08dfb5d Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sat, 22 Feb 2020 10:37:41 +0100 Subject: [PATCH] [Sema] Fix pointer-to-int-cast diagnostic for _Bool The diagnostic added in D72231 also shows a diagnostic when casting to a _Bool. This is unwanted. This patch removes the diagnostic for _Bool types. Differential Revision: https://reviews.llvm.org/D74860 --- clang/lib/Sema/SemaCast.cpp | 3 ++- clang/test/Sema/MicrosoftExtensions.c | 2 +- clang/test/Sema/cast.c | 8 ++++---- clang/test/SemaCXX/cstyle-cast.cpp | 5 +++++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp index a89cc4b..2b3b60e 100644 --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -2764,7 +2764,8 @@ void CastOperation::CheckCStyleCast() { } if ((Self.Context.getTypeSize(SrcType) > - Self.Context.getTypeSize(DestType))) { + Self.Context.getTypeSize(DestType)) && + !DestType->isBooleanType()) { // C 6.3.2.3p6: Any pointer type may be converted to an integer type. // Except as previously specified, the result is implementation-defined. // If the result cannot be represented in the integer type, the behavior diff --git a/clang/test/Sema/MicrosoftExtensions.c b/clang/test/Sema/MicrosoftExtensions.c index 6e784bf..8e7087a 100644 --- a/clang/test/Sema/MicrosoftExtensions.c +++ b/clang/test/Sema/MicrosoftExtensions.c @@ -99,7 +99,7 @@ void pointer_to_integral_type_conv(char* ptr) { sh = (short)ptr; // expected-warning{{cast to smaller integer type 'short' from 'char *' is a Microsoft extension}} // This is valid ISO C. - _Bool b = (_Bool)ptr; // expected-warning{{cast to smaller integer type '_Bool' from 'char *' is a Microsoft extension}} + _Bool b = (_Bool)ptr; } typedef struct { diff --git a/clang/test/Sema/cast.c b/clang/test/Sema/cast.c index f16064d..0c4fc7d 100644 --- a/clang/test/Sema/cast.c +++ b/clang/test/Sema/cast.c @@ -151,7 +151,7 @@ void testCDouble(CDouble v) { } void testVoidPtr(VoidPtr v) { - (void) (Bool) v; // expected-warning{{cast to smaller integer type 'Bool' (aka '_Bool') from 'VoidPtr' (aka 'void *')}} + (void)(Bool) v; (void) (Int) v; // expected-warning{{cast to smaller integer type 'Int' (aka 'int') from 'VoidPtr' (aka 'void *')}} (void) (Long) v; (void) (VoidPtr) v; @@ -160,12 +160,12 @@ void testVoidPtr(VoidPtr v) { // from other -Wpointer-to-int-cast warnings. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wvoid-pointer-to-int-cast" - (void) (Bool) v; // no-warning + (void)(Int) v; // no-warning #pragma clang diagnostic pop } void testCharPtr(CharPtr v) { - (void) (Bool) v; // expected-warning{{cast to smaller integer type 'Bool' (aka '_Bool') from 'CharPtr' (aka 'char *')}} + (void)(Bool) v; (void) (Int) v; // expected-warning{{cast to smaller integer type 'Int' (aka 'int') from 'CharPtr' (aka 'char *')}} (void) (Long) v; (void) (VoidPtr) v; @@ -174,7 +174,7 @@ void testCharPtr(CharPtr v) { // from other -Wpointer-to-int-cast warnings. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wvoid-pointer-to-int-cast" - (void) (Bool) v; // expected-warning{{cast to smaller integer type 'Bool' (aka '_Bool') from 'CharPtr' (aka 'char *')}} + (void)(Int) v; // expected-warning{{cast to smaller integer type 'Int' (aka 'int') from 'CharPtr' (aka 'char *')}} #pragma clang diagnostic pop } diff --git a/clang/test/SemaCXX/cstyle-cast.cpp b/clang/test/SemaCXX/cstyle-cast.cpp index 2327d7b..32a6e20 100644 --- a/clang/test/SemaCXX/cstyle-cast.cpp +++ b/clang/test/SemaCXX/cstyle-cast.cpp @@ -178,6 +178,11 @@ void integral_conversion() fnptr fnp = (fnptr)(l); (void)(char)(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}} (void)(long)(fnp); + + (void)(bool)((void*)0); + (void)(bool)((int*)0); + (void)(char)((void*)0); // expected-error {{cast from pointer to smaller type 'char' loses information}} + (void)(char)((int*)0); // expected-error {{cast from pointer to smaller type 'char' loses information}} } void pointer_conversion() -- 2.7.4