From d125a97ae8abfc7855b23b5c0d2653a8ef3fba82 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Thu, 28 Jan 2016 05:22:54 +0000 Subject: [PATCH] [Lex] Share some common code between decimal and octal parsing in NumericLiteralParser. There were a couple slight variations between the two copies that I don't believe were intentional. For example, only one of the paths checked for digit separations proceeding a '.', but I think the lexer itself splits the token if a digit separator proceeds a period. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259022 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Lex/LiteralSupport.h | 1 + lib/Lex/LiteralSupport.cpp | 108 +++++++++++++---------------- 2 files changed, 48 insertions(+), 61 deletions(-) diff --git a/include/clang/Lex/LiteralSupport.h b/include/clang/Lex/LiteralSupport.h index d568614e2a..dbe9f82374 100644 --- a/include/clang/Lex/LiteralSupport.h +++ b/include/clang/Lex/LiteralSupport.h @@ -104,6 +104,7 @@ public: private: void ParseNumberStartingWithZero(SourceLocation TokLoc); + void ParseDecimalOrOctalCommon(SourceLocation TokLoc); static bool isDigitSeparator(char C) { return C == '\''; } diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp index 5b1c49344e..fb9d63b940 100644 --- a/lib/Lex/LiteralSupport.cpp +++ b/lib/Lex/LiteralSupport.cpp @@ -536,34 +536,10 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, s = SkipDigits(s); if (s == ThisTokEnd) { // Done. - } else if (isHexDigit(*s) && !(*s == 'e' || *s == 'E')) { - PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), - diag::err_invalid_digit) << StringRef(s, 1) << 0; - hadError = true; - return; - } else if (*s == '.') { - checkSeparator(TokLoc, s, CSK_AfterDigits); - s++; - saw_period = true; - checkSeparator(TokLoc, s, CSK_BeforeDigits); - s = SkipDigits(s); - } - if ((*s == 'e' || *s == 'E')) { // exponent - checkSeparator(TokLoc, s, CSK_AfterDigits); - const char *Exponent = s; - s++; - saw_exponent = true; - if (*s == '+' || *s == '-') s++; // sign - checkSeparator(TokLoc, s, CSK_BeforeDigits); - const char *first_non_digit = SkipDigits(s); - if (first_non_digit != s) { - s = first_non_digit; - } else { - PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin), - diag::err_exponent_has_no_digits); - hadError = true; + } else { + ParseDecimalOrOctalCommon(TokLoc); + if (hadError) return; - } } } @@ -693,6 +669,49 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, } } +/// ParseDecimalOrOctalCommon - This method is called for decimal or octal +/// numbers. It issues an error for illegal digits, and handles floating point +/// parsing. If it detects a floating point number, the radix is set to 10. +void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc){ + assert((radix == 8 || radix == 10) && "Unexpected radix"); + + // If we have a hex digit other than 'e' (which denotes a FP exponent) then + // the code is using an incorrect base. + if (isHexDigit(*s) && *s != 'e' && *s != 'E') { + PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), + diag::err_invalid_digit) << StringRef(s, 1) << (radix == 8 ? 1 : 0); + hadError = true; + return; + } + + if (*s == '.') { + checkSeparator(TokLoc, s, CSK_AfterDigits); + s++; + radix = 10; + saw_period = true; + checkSeparator(TokLoc, s, CSK_BeforeDigits); + s = SkipDigits(s); // Skip suffix. + } + if (*s == 'e' || *s == 'E') { // exponent + checkSeparator(TokLoc, s, CSK_AfterDigits); + const char *Exponent = s; + s++; + radix = 10; + saw_exponent = true; + if (*s == '+' || *s == '-') s++; // sign + const char *first_non_digit = SkipDigits(s); + if (first_non_digit != s) { + checkSeparator(TokLoc, s, CSK_BeforeDigits); + s = first_non_digit; + } else { + PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), + diag::err_exponent_has_no_digits); + hadError = true; + return; + } + } +} + /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved /// suffixes as ud-suffixes, because the diagnostic experience is better if we /// treat it as an invalid suffix. @@ -843,40 +862,7 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { } } - // If we have a hex digit other than 'e' (which denotes a FP exponent) then - // the code is using an incorrect base. - if (isHexDigit(*s) && *s != 'e' && *s != 'E') { - PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), - diag::err_invalid_digit) << StringRef(s, 1) << 1; - hadError = true; - return; - } - - if (*s == '.') { - s++; - radix = 10; - saw_period = true; - checkSeparator(TokLoc, s, CSK_BeforeDigits); - s = SkipDigits(s); // Skip suffix. - } - if (*s == 'e' || *s == 'E') { // exponent - checkSeparator(TokLoc, s, CSK_AfterDigits); - const char *Exponent = s; - s++; - radix = 10; - saw_exponent = true; - if (*s == '+' || *s == '-') s++; // sign - const char *first_non_digit = SkipDigits(s); - if (first_non_digit != s) { - checkSeparator(TokLoc, s, CSK_BeforeDigits); - s = first_non_digit; - } else { - PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), - diag::err_exponent_has_no_digits); - hadError = true; - return; - } - } + ParseDecimalOrOctalCommon(TokLoc); } static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) { -- 2.50.1