llvm.org GIT mirror llvm / 589b088
[MC] Fix floating-point literal lexing. This patch has three related fixes to improve float literal lexing: 1. Make AsmLexer::LexDigit handle floats without a decimal point more consistently. 2. Make AsmLexer::LexFloatLiteral print an error for floats which are apparently missing an "e". 3. Make APFloat::convertFromString use binutils-compatible exponent parsing. Together, this fixes some cases where a float would be incorrectly rejected, fixes some cases where the compiler would crash, and improves diagnostics in some cases. Patch by Brandon Jones. Differential Revision: https://reviews.llvm.org/D57321 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357214 91177308-0d34-0410-b5e6-96231b3b80d8 Eli Friedman 4 months ago
4 changed file(s) with 88 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
6060 return (unsigned char)*CurPtr++;
6161 }
6262
63 /// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
64 ///
6563 /// The leading integral digit sequence and dot should have already been
6664 /// consumed, some or all of the fractional digit sequence *can* have been
6765 /// consumed.
7068 while (isDigit(*CurPtr))
7169 ++CurPtr;
7270
73 // Check for exponent; we intentionally accept a slighlty wider set of
74 // literals here and rely on the upstream client to reject invalid ones (e.g.,
75 // "1e+").
76 if (*CurPtr == 'e' || *CurPtr == 'E') {
77 ++CurPtr;
71 if (*CurPtr == '-' || *CurPtr == '+')
72 return ReturnError(CurPtr, "Invalid sign in float literal");
73
74 // Check for exponent
75 if ((*CurPtr == 'e' || *CurPtr == 'E')) {
76 ++CurPtr;
77
7878 if (*CurPtr == '-' || *CurPtr == '+')
7979 ++CurPtr;
80
8081 while (isDigit(*CurPtr))
8182 ++CurPtr;
8283 }
144145 // Disambiguate a .1243foo identifier from a floating literal.
145146 while (isDigit(*CurPtr))
146147 ++CurPtr;
147 if (*CurPtr == 'e' || *CurPtr == 'E' ||
148 !IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
148
149 if (!IsIdentifierChar(*CurPtr, AllowAtInIdentifier) ||
150 *CurPtr == 'e' || *CurPtr == 'E')
149151 return LexFloatLiteral();
150152 }
151153
325327 unsigned Radix = doHexLookAhead(CurPtr, 10, LexMasmIntegers);
326328 bool isHex = Radix == 16;
327329 // Check for floating point literals.
328 if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
329 ++CurPtr;
330 if (!isHex && (*CurPtr == '.' || *CurPtr == 'e' || *CurPtr == 'E')) {
331 if (*CurPtr == '.')
332 ++CurPtr;
330333 return LexFloatLiteral();
331334 }
332335
197197 const unsigned int overlargeExponent = 24000; /* FIXME. */
198198 StringRef::iterator p = begin;
199199
200 assert(p != end && "Exponent has no digits");
200 // Treat no exponent as 0 to match binutils
201 if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) {
202 return 0;
203 }
201204
202205 isNegative = (*p == '-');
203206 if (*p == '-' || *p == '+') {
2626
2727 # CHECK: .long 1067928519
2828 .float 1.307
29
29
3030 # CHECK: .quad 4617315517961601024
3131 # CHECK: .quad 4597526701198935065
3232 # CHECK: .quad -4600933674317040845
4747 .double 1.e5
4848 # CHECK: .quad 4611686018427387904
4949 .double 2.
50 # CHECK: .quad 4611686018427387904
51 .double 2.e
52 # CHECK: .quad 4611686018427387904
53 .double 2.e+
54 # CHECK: .quad 4611686018427387904
55 .double 2.e-
56 # CHECK: .quad -4615288898129284301
57 .double -1.2e
58 # CHECK: .quad 4621819117588971520
59 .double 1e1
60 # CHECK: .quad 4591870180066957722
61 .double 1e-1
5062
51 // APFloat should reject these with an error, not crash:
52 //.double -1.2e+
53 //.double -1.2e
63
64 # CHECK: .quad -4570379565595099136
65 .double -1.2E3
66 # CHECK: .quad -4690170861623122860
67 .double -1.2E-5
68 # CHECK: .quad -4465782973978902528
69 .double -1.2E+10
70 # CHECK: .quad 4681608360884174848
71 .double 1E5
72 # CHECK: .quad 4681608360884174848
73 .double 1.E5
74 # CHECK: .quad 4611686018427387904
75 .double 2.E
76 # CHECK: .quad 4611686018427387904
77 .double 2.E+
78 # CHECK: .quad 4611686018427387904
79 .double 2.E-
80 # CHECK: .quad -4615288898129284301
81 .double -1.2E
82 # CHECK: .quad 4621819117588971520
83 .double 1E1
84 # CHECK: .quad 4591870180066957722
85 .double 1E-1
86
87
88 #CHECK-ERROR: unexpected token in '.double' directive
89 .double 1e1e
90 # CHECK-ERROR: Invalid sign in float literal
91 .double 2.+1
5492
5593 # CHECK: .long 1310177520
5694 .float 0x12f7.1ep+17
82120
83121 # CHECK-ERROR: error: invalid hexadecimal floating-point constant: expected exponent part 'p'
84122 .float 0x1.2
123
866866 EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble(), "002.05000e12").convertToDouble());
867867 EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble(), "002.05000e+12").convertToDouble());
868868 EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble(), "002.05000e-12").convertToDouble());
869
870 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e").convertToDouble());
871 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "+1e").convertToDouble());
872 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-1e").convertToDouble());
873
874 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.e").convertToDouble());
875 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "+1.e").convertToDouble());
876 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-1.e").convertToDouble());
877
878 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e").convertToDouble());
879 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), "+.1e").convertToDouble());
880 EXPECT_EQ(-0.1, APFloat(APFloat::IEEEdouble(), "-.1e").convertToDouble());
881
882 EXPECT_EQ(1.1, APFloat(APFloat::IEEEdouble(), "1.1e").convertToDouble());
883 EXPECT_EQ(1.1, APFloat(APFloat::IEEEdouble(), "+1.1e").convertToDouble());
884 EXPECT_EQ(-1.1, APFloat(APFloat::IEEEdouble(), "-1.1e").convertToDouble());
885
886 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e+").convertToDouble());
887 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e-").convertToDouble());
888
889 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e").convertToDouble());
890 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e+").convertToDouble());
891 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e-").convertToDouble());
892
893 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e").convertToDouble());
894 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e+").convertToDouble());
895 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e-").convertToDouble());
869896
870897 // These are "carefully selected" to overflow the fast log-base
871898 // calculations in APFloat.cpp
11621189 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".e"), "Significand has no digits");
11631190 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.e"), "Significand has no digits");
11641191 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.e"), "Significand has no digits");
1165 }
1166
1167 TEST(APFloatTest, StringDecimalExponentDeath) {
1168 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1e"), "Exponent has no digits");
1169 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+1e"), "Exponent has no digits");
1170 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-1e"), "Exponent has no digits");
1171
1172 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.e"), "Exponent has no digits");
1173 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+1.e"), "Exponent has no digits");
1174 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-1.e"), "Exponent has no digits");
1175
1176 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e"), "Exponent has no digits");
1177 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.1e"), "Exponent has no digits");
1178 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.1e"), "Exponent has no digits");
1179
1180 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.1e"), "Exponent has no digits");
1181 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+1.1e"), "Exponent has no digits");
1182 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-1.1e"), "Exponent has no digits");
1183
1184
1185 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1e+"), "Exponent has no digits");
1186 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1e-"), "Exponent has no digits");
1187
1188 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e"), "Exponent has no digits");
1189 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e+"), "Exponent has no digits");
1190 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e-"), "Exponent has no digits");
1191
1192 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0e"), "Exponent has no digits");
1193 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0e+"), "Exponent has no digits");
1194 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0e-"), "Exponent has no digits");
11951192 }
11961193
11971194 TEST(APFloatTest, StringHexadecimalDeath) {