[cfe-commits] r138031 - in /cfe/trunk: lib/Lex/LiteralSupport.cpp test/Lexer/constants.c

Craig Topper craig.topper at gmail.com
Thu Aug 18 20:20:12 PDT 2011


Author: ctopper
Date: Thu Aug 18 22:20:12 2011
New Revision: 138031

URL: http://llvm.org/viewvc/llvm-project?rev=138031&view=rev
Log:
Warn about and truncate UCNs that are too big for their character literal type.

Modified:
    cfe/trunk/lib/Lex/LiteralSupport.cpp
    cfe/trunk/test/Lexer/constants.c

Modified: cfe/trunk/lib/Lex/LiteralSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/LiteralSupport.cpp?rev=138031&r1=138030&r2=138031&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/LiteralSupport.cpp (original)
+++ cfe/trunk/lib/Lex/LiteralSupport.cpp Thu Aug 18 22:20:12 2011
@@ -786,6 +786,7 @@
     if (begin[0] != '\\')     // If this is a normal character, consume it.
       ResultChar = *begin++;
     else {                    // Otherwise, this is an escape character.
+      unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
       // Check for UCN.
       if (begin[1] == 'u' || begin[1] == 'U') {
         uint32_t utf32 = 0;
@@ -796,9 +797,12 @@
           HadError = 1;
         }
         ResultChar = utf32;
+        if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
+          PP.Diag(Loc, diag::warn_ucn_escape_too_large);
+          ResultChar &= ~0U >> (32-CharWidth);
+        }
       } else {
         // Otherwise, this is a non-UCN escape character.  Process it.
-        unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
         ResultChar = ProcessCharEscape(begin, end, HadError,
                                        FullSourceLoc(Loc,PP.getSourceManager()),
                                        CharWidth, &PP.getDiagnostics());
@@ -843,10 +847,6 @@
   // Transfer the value from APInt to uint64_t
   Value = LitVal.getZExtValue();
 
-  if (((isWide() && PP.getLangOptions().ShortWChar) || isUTF16()) &&
-      Value > 0xFFFF)
-    PP.Diag(Loc, diag::warn_ucn_escape_too_large);
-
   // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
   // if 'char' is signed for this target (C99 6.4.4.4p10).  Note that multiple
   // character constants are not sign extended in the this implementation:

Modified: cfe/trunk/test/Lexer/constants.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/constants.c?rev=138031&r1=138030&r2=138031&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/constants.c (original)
+++ cfe/trunk/test/Lexer/constants.c Thu Aug 18 22:20:12 2011
@@ -65,3 +65,5 @@
 
 // PR7888
 double g = 1e100000000; // expected-warning {{too large}}
+
+char h = '\u1234'; // expected-warning {{character unicode escape sequence too long for its type}}





More information about the cfe-commits mailing list