[cfe-commits] r39406 - in /cfe/cfe/trunk: Lex/PPExpressions.cpp include/clang/Basic/DiagnosticKinds.def test/Preprocessor/expr_usual_conversions.c

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:44:02 PDT 2007


Author: clattner
Date: Wed Jul 11 11:44:02 2007
New Revision: 39406

URL: http://llvm.org/viewvc/llvm-project?rev=39406&view=rev
Log:
Warn when performing 'usual' conversions that require a sign change.  This
implements test/Preprocessor/expr_usual_conversions.c, which produces this
output:

expr_usual_conversions.c:5:10: warning: left side of operator converted from negative value to unsigned: -42 to 18446744073709551574
#if (-42 + 0U) / -2
         ^
expr_usual_conversions.c:5:16: warning: right side of operator converted from negative value to unsigned: -2 to 18446744073709551614
#if (-42 + 0U) / -2
               ^

Added:
    cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c   (with props)
Modified:
    cfe/cfe/trunk/Lex/PPExpressions.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/cfe/trunk/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/PPExpressions.cpp?rev=39406&r1=39405&r2=39406&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/PPExpressions.cpp (original)
+++ cfe/cfe/trunk/Lex/PPExpressions.cpp Wed Jul 11 11:44:02 2007
@@ -430,9 +430,19 @@
     // either operand is unsigned.  Don't do this for x and y in "x ? y : z".
     APSInt Res(LHS.getBitWidth());
     if (Operator != tok::question) {
-      if (RHS.isUnsigned()) LHS.setIsUnsigned(true);
-      RHS.setIsUnsigned(LHS.isUnsigned());
-      Res.setIsUnsigned(LHS.isUnsigned());
+      Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
+      // If this just promoted something from signed to unsigned, and if the
+      // value was negative, warn about it.
+      if (ValueLive && Res.isUnsigned()) {
+        if (!LHS.isUnsigned() && LHS.isNegative())
+          PP.Diag(OpToken, diag::warn_pp_convert_lhs_to_positive,
+                  LHS.toString(10, true) + " to " + LHS.toString(10, false));
+        if (!RHS.isUnsigned() && RHS.isNegative())
+          PP.Diag(OpToken, diag::warn_pp_convert_rhs_to_positive,
+                  RHS.toString(10, true) + " to " + RHS.toString(10, false));
+      }
+      LHS.setIsUnsigned(Res.isUnsigned());
+      RHS.setIsUnsigned(Res.isUnsigned());
     }
     
     // FIXME: All of these should detect and report overflow??

Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=39406&r1=39405&r2=39406&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:44:02 2007
@@ -116,6 +116,10 @@
      "invalid string literal, ignoring final '\\'")
 DIAG(warn_pp_expr_overflow, WARNING,
      "integer overflow in preprocessor expression")
+DIAG(warn_pp_convert_lhs_to_positive, WARNING,
+     "left side of operator converted from negative value to unsigned: %s")
+DIAG(warn_pp_convert_rhs_to_positive, WARNING,
+     "right side of operator converted from negative value to unsigned: %s")
 
 DIAG(ext_pp_import_directive, EXTENSION,
      "#import is a language extension")

Added: cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c?rev=39406&view=auto

==============================================================================
--- cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c (added)
+++ cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c Wed Jul 11 11:44:02 2007
@@ -0,0 +1,8 @@
+// RUN: clang %s -E  2>&1 | grep warning | wc -l | grep 2
+
+#define INTMAX_MIN (-9223372036854775807LL -1)
+
+#if (-42 + 0U) / -2
+foo
+#endif
+

Propchange: cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/test/Preprocessor/expr_usual_conversions.c

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision





More information about the cfe-commits mailing list