[PATCH] D75708: Add warnings for casting ptr -> smaller int for C++ in Microsoft mode
Arthur Eubanks via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 5 13:44:35 PST 2020
aeubanks created this revision.
aeubanks added a reviewer: rnk.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Adds warnings to groups recently added in https://reviews.llvm.org/D72231.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D75708
Files:
clang/lib/Sema/SemaCast.cpp
clang/test/SemaCXX/MicrosoftExtensions.cpp
Index: clang/test/SemaCXX/MicrosoftExtensions.cpp
===================================================================
--- clang/test/SemaCXX/MicrosoftExtensions.cpp
+++ clang/test/SemaCXX/MicrosoftExtensions.cpp
@@ -200,10 +200,10 @@
static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
void pointer_to_integral_type_conv(char* ptr) {
- char ch = (char)ptr;
- short sh = (short)ptr;
- ch = (char)ptr;
- sh = (short)ptr;
+ char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
+ short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
+ ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
+ sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
// These are valid C++.
bool b = (bool)ptr;
Index: clang/lib/Sema/SemaCast.cpp
===================================================================
--- clang/lib/Sema/SemaCast.cpp
+++ clang/lib/Sema/SemaCast.cpp
@@ -2204,13 +2204,19 @@
// C++ 5.2.10p4: A pointer can be explicitly converted to any integral
// type large enough to hold it; except in Microsoft mode, where the
// integral type size doesn't matter (except we don't allow bool).
- bool MicrosoftException = Self.getLangOpts().MicrosoftExt &&
- !DestType->isBooleanType();
if ((Self.Context.getTypeSize(SrcType) >
- Self.Context.getTypeSize(DestType)) &&
- !MicrosoftException) {
- msg = diag::err_bad_reinterpret_cast_small_int;
- return TC_Failed;
+ Self.Context.getTypeSize(DestType))) {
+ bool MicrosoftException =
+ Self.getLangOpts().MicrosoftExt && !DestType->isBooleanType();
+ if (MicrosoftException) {
+ unsigned Diag = SrcType->isVoidPointerType()
+ ? diag::warn_void_pointer_to_int_cast
+ : diag::warn_pointer_to_int_cast;
+ Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange;
+ } else {
+ msg = diag::err_bad_reinterpret_cast_small_int;
+ return TC_Failed;
+ }
}
Kind = CK_PointerToIntegral;
return TC_Success;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75708.248596.patch
Type: text/x-patch
Size: 2329 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200305/70e1d7ee/attachment.bin>
More information about the cfe-commits
mailing list