]> granicus.if.org Git - clang/commitdiff
[PR36008] Avoid -Wsign-compare warning for enum constants in
authorAlex Lorenz <arphaman@gmail.com>
Wed, 7 Feb 2018 20:45:39 +0000 (20:45 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Wed, 7 Feb 2018 20:45:39 +0000 (20:45 +0000)
typeof expressions

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

rdar://36588828

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324514 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 31c1b8913b2d7122613ed4227232e883858071a9..8f6eef5984355ea340940fab6b88cd8ddd028979 100644 (file)
@@ -8955,6 +8955,16 @@ static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
   LHS = LHS->IgnoreParenImpCasts();
   RHS = RHS->IgnoreParenImpCasts();
 
+  if (!S.getLangOpts().CPlusPlus) {
+    // Avoid warning about comparison of integers with different signs when
+    // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of
+    // the type of `E`.
+    if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType()))
+      LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
+    if (const auto *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;
index 7cd8adab8921935311dfaf8e6b5f717e269cdf3a..b2b486f59f8783d47af881de2b7ee9941fdfe6a0 100644 (file)
@@ -391,3 +391,16 @@ typedef char two_chars[2];
 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
+  if (y == x) y = x; // no warning
+}