Skip to content

Commit f2b8539

Browse files
authored
[C2y] Correctly handle 0 in the preprocessor (#137844)
We do not diagnose 0 as a deprecated octal literal outside of the preprocessor. This fixes a bug where we were accidentally diagnosing 0 from a preprocessor conditional, however. No release note because this is fixing an issue with a new change.
1 parent 3dc3d43 commit f2b8539

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

clang/lib/Lex/LiteralSupport.cpp

Lines changed: 5 additions & 2 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;
1423+
bool SawOctalPrefix = false, IsSingleZero = false;
14241424
if ((c1 == 'O' || c1 == 'o') && (s[1] >= '0' && s[1] <= '7')) {
14251425
unsigned DiagId;
14261426
if (LangOpts.C2y)
@@ -1438,7 +1438,8 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
14381438
auto _ = llvm::make_scope_exit([&] {
14391439
// If we still have an octal value but we did not see an octal prefix,
14401440
// diagnose as being an obsolescent feature starting in C2y.
1441-
if (radix == 8 && LangOpts.C2y && !SawOctalPrefix && !hadError)
1441+
if (radix == 8 && LangOpts.C2y && !SawOctalPrefix && !hadError &&
1442+
!IsSingleZero)
14421443
Diags.Report(TokLoc, diag::warn_unprefixed_octal_deprecated);
14431444
});
14441445

@@ -1453,6 +1454,8 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
14531454
// anything, we leave the digit start where it was.
14541455
if (s != PossibleNewDigitStart)
14551456
DigitsBegin = PossibleNewDigitStart;
1457+
else
1458+
IsSingleZero = (s == ThisTokEnd); // Is the only thing we've seen a 0?
14561459

14571460
if (s == ThisTokEnd)
14581461
return; // Done, simple octal number like 01234

clang/test/C/C2y/n3353.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ static const void *ptr = 0o0; /* ext-warning {{octal integer literals are a C2y
4646
// 0 by itself is not deprecated, of course.
4747
int k = 0;
4848

49+
// Test a preprocessor use of 0 by itself, which is also not deprecated.
50+
#if 0
51+
#endif
52+
4953
// Make sure there are no surprises with auto and type deduction. Promotion
5054
// turns this into an 'int', and 'constexpr' implies 'const'.
5155
constexpr auto l = 0o1234567; /* ext-warning {{octal integer literals are a C2y extension}}

0 commit comments

Comments
 (0)