Skip to content

[C2y] Correctly handle 0 in the preprocessor #137844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025

Conversation

AaronBallman
Copy link
Collaborator

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.

We do not diagnose 0 as a deprecated octal literal outside of the
preprocessor. This fixes a bug where we were accidentally diagosing 0
from a preprocessor conditional, however.

No release note because this is fixing an issue with a new change.
@AaronBallman AaronBallman added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" false-positive Warning fires when it should not c2y labels Apr 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/137844.diff

2 Files Affected:

  • (modified) clang/lib/Lex/LiteralSupport.cpp (+5-2)
  • (modified) clang/test/C/C2y/n3353.c (+4)
diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp
index 20933cc8dee69..340b6539afc7c 100644
--- a/clang/lib/Lex/LiteralSupport.cpp
+++ b/clang/lib/Lex/LiteralSupport.cpp
@@ -1420,7 +1420,7 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
   }
 
   // Parse a potential octal literal prefix.
-  bool SawOctalPrefix = false;
+  bool SawOctalPrefix = false, IsNakedZero = false;
   if ((c1 == 'O' || c1 == 'o') && (s[1] >= '0' && s[1] <= '7')) {
     unsigned DiagId;
     if (LangOpts.C2y)
@@ -1438,7 +1438,8 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
   auto _ = llvm::make_scope_exit([&] {
     // If we still have an octal value but we did not see an octal prefix,
     // diagnose as being an obsolescent feature starting in C2y.
-    if (radix == 8 && LangOpts.C2y && !SawOctalPrefix && !hadError)
+    if (radix == 8 && LangOpts.C2y && !SawOctalPrefix && !hadError &&
+        !IsNakedZero)
       Diags.Report(TokLoc, diag::warn_unprefixed_octal_deprecated);
   });
 
@@ -1453,6 +1454,8 @@ void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
   // anything, we leave the digit start where it was.
   if (s != PossibleNewDigitStart)
     DigitsBegin = PossibleNewDigitStart;
+  else
+    IsNakedZero = s == ThisTokEnd; // Is the only thing we've seen a 0?
 
   if (s == ThisTokEnd)
     return; // Done, simple octal number like 01234
diff --git a/clang/test/C/C2y/n3353.c b/clang/test/C/C2y/n3353.c
index fb7f9439ac21b..a616228f1bad0 100644
--- a/clang/test/C/C2y/n3353.c
+++ b/clang/test/C/C2y/n3353.c
@@ -46,6 +46,10 @@ static const void *ptr = 0o0;  /* ext-warning {{octal integer literals are a C2y
 // 0 by itself is not deprecated, of course.
 int k = 0;
 
+// Test a preprocessor use of 0 by itself, which is also not deprecated.
+#if 0
+#endif
+
 // Make sure there are no surprises with auto and type deduction. Promotion
 // turns this into an 'int', and 'constexpr' implies 'const'.
 constexpr auto l = 0o1234567; /* ext-warning {{octal integer literals are a C2y extension}}

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 suggestions, else LGTM.

@AaronBallman
Copy link
Collaborator Author

Good suggestions, I've applied them

@AaronBallman AaronBallman merged commit f2b8539 into llvm:main Apr 30, 2025
11 checks passed
@AaronBallman AaronBallman deleted the aballman-pp-octal-zero branch April 30, 2025 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c2y clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category false-positive Warning fires when it should not
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants