summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCraig Topper <craig.topper@gmail.com>2016-01-28 05:22:54 +0000
committerCraig Topper <craig.topper@gmail.com>2016-01-28 05:22:54 +0000
commit3efc7c0c464c317b0f88cbf26bc9c53b5d6414de (patch)
treed840e32fbc3aeb90e5faeeb581dcbc960dca40c0
parentcac5589ba9038e537941368ae0187156cb73dfc4 (diff)
downloadbcm5719-llvm-3efc7c0c464c317b0f88cbf26bc9c53b5d6414de.tar.gz
bcm5719-llvm-3efc7c0c464c317b0f88cbf26bc9c53b5d6414de.zip
[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. llvm-svn: 259022
-rw-r--r--clang/include/clang/Lex/LiteralSupport.h1
-rw-r--r--clang/lib/Lex/LiteralSupport.cpp108
2 files changed, 48 insertions, 61 deletions
diff --git a/clang/include/clang/Lex/LiteralSupport.h b/clang/include/clang/Lex/LiteralSupport.h
index d568614e2ae..dbe9f82374d 100644
--- a/clang/include/clang/Lex/LiteralSupport.h
+++ b/clang/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/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp
index 5b1c49344e8..fb9d63b940b 100644
--- a/clang/lib/Lex/LiteralSupport.cpp
+++ b/clang/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) {
OpenPOWER on IntegriCloud