[PATCH] D42561: [PR36008] Avoid -Wsign-compare warning for enum constants in typeof expressions

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 25 16:32:45 PST 2018


arphaman created this revision.
arphaman added a reviewer: lebedev.ri.

This patch looks through typeof type at the original expression when diagnosing -Wsign-compare to avoid an unfriendly diagnostic.


Repository:
  rC Clang

https://reviews.llvm.org/D42561

Files:
  lib/Sema/SemaChecking.cpp
  test/Sema/compare.c


Index: test/Sema/compare.c
===================================================================
--- test/Sema/compare.c
+++ test/Sema/compare.c
@@ -391,3 +391,15 @@
 void test12(unsigned a) {
   if (0 && -1 > a) { }
 }
+
+// PR36008
+
+enum PR36008EnumTest {
+    kPR36008Value = 0,
+};
+
+void pr36008(enum PR36008EnumTest lhs) {
+  __typeof__(lhs) x = lhs;
+  __typeof__(kPR36008Value) y = (kPR36008Value);
+  if (x == y) x = y; // no warning
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -8955,6 +8955,11 @@
   LHS = LHS->IgnoreParenImpCasts();
   RHS = RHS->IgnoreParenImpCasts();
 
+  if (!S.getLangOpts().CPlusPlus) {
+    if (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(RHS->getType()))
+      RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
+  }
+
   // Check to see if one of the (unmodified) operands is of different
   // signedness.
   Expr *signedOperand, *unsignedOperand;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42561.131522.patch
Type: text/x-patch
Size: 1023 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180126/edcfb3f7/attachment.bin>


More information about the cfe-commits mailing list