[cfe-commits] r70329 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticLexKinds.td lib/Lex/LiteralSupport.cpp test/Lexer/constants.c

Chris Lattner sabre at nondot.org
Tue Apr 28 14:51:46 PDT 2009


Author: lattner
Date: Tue Apr 28 16:51:46 2009
New Revision: 70329

URL: http://llvm.org/viewvc/llvm-project?rev=70329&view=rev
Log:
Implement -Wfour-char-constants, which is an extension, not an extwarn,
and apparently not part of -Wall

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/lib/Lex/LiteralSupport.cpp
    cfe/trunk/test/Lexer/constants.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=70329&r1=70328&r2=70329&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Apr 28 16:51:46 2009
@@ -33,7 +33,7 @@
 def : DiagGroup<"format-security">;
 def : DiagGroup<"format=2">;
 def : DiagGroup<"format">;
-def : DiagGroup<"four-char-constants">;
+def FourByteMultiChar : DiagGroup<"four-char-constants">;
 
 
 def : DiagGroup<"init-self">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=70329&r1=70328&r2=70329&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Tue Apr 28 16:51:46 2009
@@ -53,6 +53,9 @@
   "invalid argument to convert to character">;
 def ext_multichar_character_literal : ExtWarn<
   "multi-character character constant">, InGroup<MultiChar>;
+def ext_four_char_character_literal : Extension<
+  "multi-character character constant">, InGroup<FourByteMultiChar>;
+  
 
 // Literal
 def ext_nonstandard_escape : Extension<

Modified: cfe/trunk/lib/Lex/LiteralSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/LiteralSupport.cpp?rev=70329&r1=70328&r2=70329&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/LiteralSupport.cpp (original)
+++ cfe/trunk/lib/Lex/LiteralSupport.cpp Tue Apr 28 16:51:46 2009
@@ -631,19 +631,19 @@
 
   // FIXME: The "Value" is an uint64_t so we can handle char literals of 
   // upto 64-bits.
-  assert(PP.getTargetInfo().getIntWidth() <= 64 &&
-         "Assumes sizeof(int) on target is <= 64");
-  assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
-         "Assumes sizeof(wchar) on target is <= 64");
   // FIXME: This extensively assumes that 'char' is 8-bits.
   assert(PP.getTargetInfo().getCharWidth() == 8 &&
          "Assumes char is 8 bits");
+  assert(PP.getTargetInfo().getIntWidth() <= 64 &&
+         (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
+         "Assumes sizeof(int) on target is <= 64 and a multiple of char");
+  assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
+         "Assumes sizeof(wchar) on target is <= 64");
 
   // This is what we will use for overflow detection 
   llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
   
-  bool isFirstChar = true;
-  bool isMultiChar = false;
+  unsigned NumCharsSoFar = 0;
   while (begin[0] != '\'') {
     uint64_t ResultChar;
     if (begin[0] != '\\')     // If this is a normal character, consume it.
@@ -653,33 +653,33 @@
 
     // If this is a multi-character constant (e.g. 'abc'), handle it.  These are
     // implementation defined (C99 6.4.4.4p10).
-    if (!isFirstChar) {
-      // If this is the second character being processed, do special handling.
-      if (!isMultiChar) {
-        isMultiChar = true;
-      
-        // Warn about discarding the top bits for multi-char wide-character
-        // constants (L'abcd').
-        if (IsWide)
-          PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
-        else
-          PP.Diag(Loc, diag::ext_multichar_character_literal);
-      }
-
+    if (NumCharsSoFar) {
       if (IsWide) {
         // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
         LitVal = 0;
       } else {
         // Narrow character literals act as though their value is concatenated
-        // in this implementation.
-        if ((LitVal.shl(8)).lshr(8) != LitVal)
+        // in this implementation, but warn on overflow.
+        if (LitVal.countLeadingZeros() < 8)
           PP.Diag(Loc, diag::warn_char_constant_too_large);
         LitVal <<= 8;
       }
     }
     
     LitVal = LitVal + ResultChar;
-    isFirstChar = false;
+    ++NumCharsSoFar;
+  }
+
+  // If this is the second character being processed, do special handling.
+  if (NumCharsSoFar > 1) {
+    // Warn about discarding the top bits for multi-char wide-character
+    // constants (L'abcd').
+    if (IsWide)
+      PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
+    else if (NumCharsSoFar != 4)
+      PP.Diag(Loc, diag::ext_multichar_character_literal);
+    else
+      PP.Diag(Loc, diag::ext_four_char_character_literal);
   }
 
   // Transfer the value from APInt to uint64_t
@@ -689,7 +689,7 @@
   // 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:
   // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
-  if (!IsWide && !isMultiChar && (Value & 128) &&
+  if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
       PP.getTargetInfo().isCharSigned())
     Value = (signed char)Value;
 }

Modified: cfe/trunk/test/Lexer/constants.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/constants.c?rev=70329&r1=70328&r2=70329&view=diff

==============================================================================
--- cfe/trunk/test/Lexer/constants.c (original)
+++ cfe/trunk/test/Lexer/constants.c Tue Apr 28 16:51:46 2009
@@ -16,14 +16,20 @@
 
 
 char c[] = {
-  'df',  // expected-warning {{multi-character character constant}}
+  'df',   // expected-warning {{multi-character character constant}}
   '\t',
   '\\
 t',
-  '??!'  // expected-warning {{trigraph converted to '|' character}}
+  '??!',  // expected-warning {{trigraph converted to '|' character}}
+  'abcd'  // expected-warning {{multi-character character constant}}
 };
 
 
 #pragma GCC diagnostic ignored "-Wmultichar"
 
 char d = 'df'; // no warning.
+char e = 'abcd';  // still warn: expected-warning {{multi-character character constant}}
+
+#pragma GCC diagnostic ignored "-Wfour-char-constants"
+
+char f = 'abcd';  // ignored.





More information about the cfe-commits mailing list