Skip to content

Commit 9773316

Browse files
committed
[C2y] Correctly handle FP-suffixes on prefixed octals (#141230)
Fixes #141230. Currently, prefixed octal literals used with floating-point suffixes are not rejected, causing Clang crash. This patch adds proper handling to reject invalid literals like `0o0.1` or `0.0e1`. No release note because this is fixing an issue with a new change.
1 parent d1a6327 commit 9773316

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

clang/lib/Lex/LiteralSupport.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
14201420
}
14211421

14221422
// Parse a potential octal literal prefix.
1423-
bool SawOctalPrefix = false, IsSingleZero = false;
1423+
bool IsSingleZero = false;
14241424
if ((c1 == 'O' || c1 == 'o') && (s[1] >= '0' && s[1] <= '7')) {
14251425
unsigned DiagId;
14261426
if (LangOpts.C2y)
@@ -1432,14 +1432,26 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
14321432
Diags.Report(TokLoc, DiagId);
14331433
++s;
14341434
DigitsBegin = s;
1435-
SawOctalPrefix = true;
1435+
radix = 8;
1436+
s = SkipOctalDigits(s);
1437+
if (s == ThisTokEnd) {
1438+
// Done
1439+
} else if ((isHexDigit(*s) && *s != 'e' && *s != 'E' && *s != '.') &&
1440+
!isValidUDSuffix(LangOpts, StringRef(s, ThisTokEnd - s))) {
1441+
Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM,
1442+
LangOpts),
1443+
diag::err_invalid_digit)
1444+
<< StringRef(s, 1) << 1;
1445+
hadError = true;
1446+
}
1447+
// Other suffixes will be diagnosed by the caller.
1448+
return;
14361449
}
14371450

14381451
auto _ = llvm::make_scope_exit([&] {
14391452
// If we still have an octal value but we did not see an octal prefix,
14401453
// diagnose as being an obsolescent feature starting in C2y.
1441-
if (radix == 8 && LangOpts.C2y && !SawOctalPrefix && !hadError &&
1442-
!IsSingleZero)
1454+
if (radix == 8 && LangOpts.C2y && !hadError && !IsSingleZero)
14431455
Diags.Report(TokLoc, diag::warn_unprefixed_octal_deprecated);
14441456
});
14451457

clang/test/C/C2y/n3353.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ int o2 = 0xG; /* expected-error {{invalid suffix 'xG' on integer constant}}
9292
c2y-warning {{octal literals without a '0o' prefix are deprecated}}
9393
*/
9494

95+
// Show that floating-point suffixes on octal literals are rejected.
96+
auto f1 = 0o0.; /* expected-error {{invalid suffix '.' on integer constant}}
97+
compat-warning {{octal integer literals are incompatible with standards before C2y}}
98+
ext-warning {{octal integer literals are a C2y extension}}
99+
cpp-warning {{octal integer literals are a Clang extension}}
100+
*/
101+
auto f2 = 0o0.1; /* expected-error {{invalid suffix '.1' on integer constant}}
102+
compat-warning {{octal integer literals are incompatible with standards before C2y}}
103+
ext-warning {{octal integer literals are a C2y extension}}
104+
cpp-warning {{octal integer literals are a Clang extension}}
105+
*/
106+
auto f3 = 0o0e1; /* expected-error {{invalid suffix 'e1' on integer constant}}
107+
compat-warning {{octal integer literals are incompatible with standards before C2y}}
108+
ext-warning {{octal integer literals are a C2y extension}}
109+
cpp-warning {{octal integer literals are a Clang extension}}
110+
*/
111+
auto f4 = 0o0E1; /* expected-error {{invalid suffix 'E1' on integer constant}}
112+
compat-warning {{octal integer literals are incompatible with standards before C2y}}
113+
ext-warning {{octal integer literals are a C2y extension}}
114+
cpp-warning {{octal integer literals are a Clang extension}}
115+
*/
116+
95117
// Ensure digit separators work as expected.
96118
constexpr int p = 0o0'1'2'3'4'5'6'7; /* compat-warning {{octal integer literals are incompatible with standards before C2y}}
97119
ext-warning {{octal integer literals are a C2y extension}}

0 commit comments

Comments
 (0)