[PATCH] D57321: Fix LexFloatLiteral Lexing

Brandon Jones via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 4 05:08:54 PST 2019


BrandonTJones updated this revision to Diff 185029.
BrandonTJones added a comment.

I have fixed a spelling error and updated a comment to better reflect the change made.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57321/new/

https://reviews.llvm.org/D57321

Files:
  llvm/include/llvm/MC/MCParser/AsmLexer.h
  llvm/lib/MC/MCParser/AsmLexer.cpp
  llvm/test/MC/AsmParser/floating-literals.s


Index: llvm/test/MC/AsmParser/floating-literals.s
===================================================================
--- llvm/test/MC/AsmParser/floating-literals.s
+++ llvm/test/MC/AsmParser/floating-literals.s
@@ -50,8 +50,11 @@
 .double 2.
 
 // APFloat should reject these with an error, not crash:
-//.double -1.2e+
-//.double -1.2e
+
+#CHECK-ERROR: unexpected token in '.double' directive
+.double -1.2e+
+#CHECK-ERROR: unexpected token in '.double' directive
+.double -1.2e
 
 # CHECK: .long 1310177520
 .float 0x12f7.1ep+17
Index: llvm/lib/MC/MCParser/AsmLexer.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmLexer.cpp
+++ llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -66,17 +66,19 @@
 /// The leading integral digit sequence and dot should have already been
 /// consumed, some or all of the fractional digit sequence *can* have been
 /// consumed.
-AsmToken AsmLexer::LexFloatLiteral() {
+AsmToken AsmLexer::LexFloatLiteral(bool isDotSeparated) {
   // Skip the fractional digit sequence.
   while (isDigit(*CurPtr))
     ++CurPtr;
 
-  // Check for exponent; we intentionally accept a slighlty wider set of
-  // literals here and rely on the upstream client to reject invalid ones (e.g.,
-  // "1e+").
-  if (*CurPtr == 'e' || *CurPtr == 'E') {
+  // Check for exponent; we enforce rigidity on a correct
+  // literal as the upstream client cannot parse all invalid
+  // forms and crashes. (e.g. 1e+, 1e8e8)
+  if (isDotSeparated && (*CurPtr == 'e' || *CurPtr == 'E') &&
+      (isDigit(getPtr(1)) ||
+       ((getPtr(1) == '-' || getPtr(1) == '+') && isDigit(getPtr(2))))) {
     ++CurPtr;
-    if (*CurPtr == '-' || *CurPtr == '+')
+    if ((*CurPtr == '-' || *CurPtr == '+') && isDigit(CurPtr[1]))
       ++CurPtr;
     while (isDigit(*CurPtr))
       ++CurPtr;
@@ -145,9 +147,13 @@
     // Disambiguate a .1243foo identifier from a floating literal.
     while (isDigit(*CurPtr))
       ++CurPtr;
-    if (*CurPtr == 'e' || *CurPtr == 'E' ||
-        !IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
-      return LexFloatLiteral();
+    if ((!IsIdentifierChar(*CurPtr, AllowAtInIdentifier) &&
+         (*CurPtr != 'e' && *CurPtr != 'E')) ||
+        (!IsIdentifierChar(*CurPtr, AllowAtInIdentifier) &&
+         (*CurPtr == 'e' && *CurPtr == 'E') &&
+         (isDigit(CurPtr[1]) ||
+          ((CurPtr[1] == '-' || CurPtr[1] == '+') && isDigit(CurPtr[2])))))
+      return LexFloatLiteral(false);
   }
 
   while (IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
@@ -326,9 +332,14 @@
     unsigned Radix = doHexLookAhead(CurPtr, 10, LexMasmIntegers);
     bool isHex = Radix == 16;
     // Check for floating point literals.
-    if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
+    if ((!isHex && (*CurPtr == '.' || *CurPtr == 'e')) &&
+        (isDigit(CurPtr[1]) || ((CurPtr[1] == '-' || CurPtr[1] == '+' ||
+                                 (*CurPtr == '.' && CurPtr[1] == 'e')) &&
+                                isDigit(CurPtr[2])))) {
       ++CurPtr;
-      return LexFloatLiteral();
+      if (CurPtr[-1] == '.')
+        return LexFloatLiteral(true);
+      return LexFloatLiteral(false);
     }
 
     StringRef Result(TokStart, CurPtr - TokStart);
Index: llvm/include/llvm/MC/MCParser/AsmLexer.h
===================================================================
--- llvm/include/llvm/MC/MCParser/AsmLexer.h
+++ llvm/include/llvm/MC/MCParser/AsmLexer.h
@@ -62,7 +62,7 @@
   AsmToken LexDigit();
   AsmToken LexSingleQuote();
   AsmToken LexQuote();
-  AsmToken LexFloatLiteral();
+  AsmToken LexFloatLiteral(bool isDotSeperated);
   AsmToken LexHexFloatLiteral(bool NoIntDigits);
 
   StringRef LexUntilEndOfLine();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57321.185029.patch
Type: text/x-patch
Size: 3697 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190204/e084e637/attachment.bin>


More information about the llvm-commits mailing list