[cfe-commits] r39621 - in /cfe/cfe/trunk: Lex/LiteralSupport.cpp include/clang/Basic/DiagnosticKinds.def include/clang/Lex/LiteralSupport.h

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:46:20 PDT 2007


Author: clattner
Date: Wed Jul 11 11:46:19 2007
New Revision: 39621

URL: http://llvm.org/viewvc/llvm-project?rev=39621&view=rev
Log:
Add support for binary literals:
http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html#Binary-constants

Modified:
    cfe/cfe/trunk/Lex/LiteralSupport.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/cfe/trunk/include/clang/Lex/LiteralSupport.h

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

==============================================================================
--- cfe/cfe/trunk/Lex/LiteralSupport.cpp (original)
+++ cfe/cfe/trunk/Lex/LiteralSupport.cpp Wed Jul 11 11:46:19 2007
@@ -215,6 +215,7 @@
       DigitsBegin = s;
       s = SkipHexDigits(s);
       if (s == ThisTokEnd) {
+        // Done.
       } else if (*s == '.') {
         s++;
         saw_period = true;
@@ -237,6 +238,19 @@
         Diag(TokLoc, diag::err_hexconstant_requires_exponent);
         return;
       }
+    } else if (*s == 'b' || *s == 'B') {
+      // 0b101010 is a GCC extension.
+      ++s;
+      radix = 2;
+      DigitsBegin = s;
+      s = SkipBinaryDigits(s);
+      if (s == ThisTokEnd) {
+        // Done.
+      } else if (isxdigit(*s)) {
+        Diag(TokLoc, diag::err_invalid_binary_digit, std::string(s, s+1));
+        return;
+      }
+      PP.Diag(TokLoc, diag::ext_binary_literal);
     } else {
       // For now, the radix is set to 8. If we discover that we have a
       // floating point constant, the radix will change to 10. Octal floating

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=39621&r1=39620&r2=39621&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:46:19 2007
@@ -530,6 +530,10 @@
      "integer constant is so large that it is unsigned")
 DIAG(err_exponent_has_no_digits, ERROR,
      "exponent has no digits")
+DIAG(ext_binary_literal, EXTENSION,
+     "binary integer literals are an extension")
+DIAG(err_invalid_binary_digit, ERROR,
+     "invalid digit '%0' in binary constant")
 DIAG(err_invalid_octal_digit, ERROR,
      "invalid digit '%0' in octal constant")
 DIAG(err_invalid_decimal_digit, ERROR,

Modified: cfe/cfe/trunk/include/clang/Lex/LiteralSupport.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/LiteralSupport.h?rev=39621&r1=39620&r2=39621&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/LiteralSupport.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/LiteralSupport.h Wed Jul 11 11:46:19 2007
@@ -97,6 +97,15 @@
       ptr++;
     return ptr;
   }
+  
+  /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
+  /// Return a pointer to the first non-binary digit or End.
+  const char *SkipBinaryDigits(const char *ptr) {
+    while (ptr != ThisTokEnd && (*ptr == '0' || *ptr == '1'))
+      ptr++;
+    return ptr;
+  }
+  
 };
 
 /// CharLiteralParser - Perform interpretation and semantic analysis of a





More information about the cfe-commits mailing list