diff options
author | Craig Topper <craig.topper@gmail.com> | 2016-01-28 05:22:54 +0000 |
---|---|---|
committer | Craig Topper <craig.topper@gmail.com> | 2016-01-28 05:22:54 +0000 |
commit | d125a97ae8abfc7855b23b5c0d2653a8ef3fba82 (patch) | |
tree | 6dbe01e01aade1e5cdcd788821e987774908021a /lib/Lex/LiteralSupport.cpp | |
parent | 257517ab0bc2d7578e991aac0087bfbb94800504 (diff) | |
download | clang-d125a97ae8abfc7855b23b5c0d2653a8ef3fba82.tar.gz |
[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
Diffstat (limited to 'lib/Lex/LiteralSupport.cpp')
-rw-r--r-- | lib/Lex/LiteralSupport.cpp | 108 |
1 files changed, 47 insertions, 61 deletions
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) { |