Skip to content

[clang-repl] Ensure clang-repl accepts all C keywords supported in all language models #142749

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 3 commits into from
Jun 5, 2025

Conversation

anutosh491
Copy link
Member

As can be seen through the docs (https://github.com/llvm/llvm-project/blob/7e1fa09ce2a228c949ce4490c98f2c73ed8ada00/clang/docs/LanguageExtensions.rst#c-keywords-supported-in-all-language-modes), Clang supports certain C keywords in all language modes — this patch ensures clang-repl handles them consistently.

Here's an example testing all the above keywords. We have everything in place except _Imaginary (_Complex works but _Imaginary doesn't which was weird) and _Noreturn

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 4, 2025

@llvm/pr-subscribers-clang

Author: Anutosh Bhat (anutosh491)

Changes

As can be seen through the docs (https://github.com/llvm/llvm-project/blob/7e1fa09ce2a228c949ce4490c98f2c73ed8ada00/clang/docs/LanguageExtensions.rst#c-keywords-supported-in-all-language-modes), Clang supports certain C keywords in all language modes — this patch ensures clang-repl handles them consistently.

Here's an example testing all the above keywords. We have everything in place except _Imaginary (_Complex works but _Imaginary doesn't which was weird) and _Noreturn


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

1 Files Affected:

  • (modified) clang/lib/Parse/ParseTentative.cpp (+2)
diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp
index 95cee824c40b7..f50bcd8ea90bb 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -1171,6 +1171,7 @@ Parser::isCXXDeclarationSpecifier(ImplicitTypenameContext AllowImplicitTypename,
   case tok::kw_inline:
   case tok::kw_virtual:
   case tok::kw_explicit:
+  case tok::kw__Noreturn:
 
     // Modules
   case tok::kw___module_private__:
@@ -1225,6 +1226,7 @@ Parser::isCXXDeclarationSpecifier(ImplicitTypenameContext AllowImplicitTypename,
     // GNU
   case tok::kw_restrict:
   case tok::kw__Complex:
+  case tok::kw__Imaginary:
   case tok::kw___attribute:
   case tok::kw___auto_type:
     return TPResult::True;

@anutosh491
Copy link
Member Author

On Main

anutosh491@vv-nuc:/build/anutosh491/llvm-project/build/bin$ ./clang-repl
clang-repl> _Alignas(16) int x;
clang-repl> int align = _Alignof(double);
clang-repl> _Atomic int atomic_var = 0;
clang-repl> _Complex double complex_val = 1.0 + 2.0i;
clang-repl> _Float16 f = 1.5;
clang-repl> _Thread_local int counter = 0;
clang-repl> _Static_assert(sizeof(int) == 4, "int must be 4 bytes");
clang-repl> _Imaginary float i = 2.0f;
In file included from <<< inputs >>>:1:
input_line_8:1:1: error: expected expression
    1 | _Imaginary float i = 2.0f;
      | ^
error: Parsing failed.
clang-repl> _Noreturn void die() {    while (true) {}}
input_line_9:1:1: error: expected expression
    1 | _Noreturn void die() {    while (true) {}}
      | ^
error: Parsing failed.

On Branch after the change

anutosh491@vv-nuc:/build/anutosh491/llvm-project/build/bin$ ./clang-repl
clang-repl> _Alignas(16) int x;
clang-repl> int align = _Alignof(double);
clang-repl> _Atomic int atomic_var = 0;
clang-repl> _Complex double complex_val = 1.0 + 2.0i;
clang-repl> _Float16 f = 1.5;
clang-repl> _Thread_local int counter = 0;
clang-repl> _Static_assert(sizeof(int) == 4, "int must be 4 bytes");
clang-repl> _Imaginary float i = 2.0f;
In file included from <<< inputs >>>:1:
input_line_8:1:1: error: imaginary types are not supported
    1 | _Imaginary float i = 2.0f;
      | ^
error: Parsing failed.
clang-repl> _Noreturn void die() {    while (true) {}}

This now works how clang would expect it to. For eg

(xeus-cpp) anutosh491@Anutoshs-MacBook-Air xeus-cpp % cat test-file.cpp 
_Imaginary float i = 2.0f;%                                                                                                                                                                                               
(xeus-cpp) anutosh491@Anutoshs-MacBook-Air xeus-cpp % clang++ -std=c++20 test-file.cpp -o test_program 
test-file.cpp:1:1: error: imaginary types are not supported
_Imaginary float i = 2.0f;
^
1 error generated.

And anything with _Noreturn would just compile fine.

@anutosh491
Copy link
Member Author

anutosh491 commented Jun 4, 2025

The error comes from here to be precise

if (getLangOpts().IncrementalExtensions &&
!isDeclarationStatement(/*DisambiguatingWithExpression=*/true))
return ParseTopLevelStmtDecl();

IsDeclarationStatement should be true but returns false enabling a faulty call to ParseTopLevelStmtDecl

These are also a part of isDeclarationSpecifier (hence -Xcc -xc worked ig)

bool isDeclarationStatement(bool DisambiguatingWithExpression = false) {
if (getLangOpts().CPlusPlus)
return isCXXDeclarationStatement(DisambiguatingWithExpression);
return isDeclarationSpecifier(ImplicitTypenameContext::No, true);
}

case tok::kw__Imaginary:

case tok::kw__Noreturn:

@anutosh491
Copy link
Member Author

Should help us in the long run and fix the errors we see while including input/output based header on xeus-cpp-lite (happens cause these internally reference _Noreturn that clang-repl currently fails to understand)

image

@vgvassilev
Copy link
Contributor

Can you add tests?

@anutosh491
Copy link
Member Author

Can you add tests?

Done.

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

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

Lgtm!

@anutosh491 anutosh491 merged commit 7ca7bcb into llvm:main Jun 5, 2025
11 checks passed
@anutosh491 anutosh491 deleted the keywords branch June 5, 2025 05:53
@anutosh491 anutosh491 added this to the LLVM 20.X Release milestone Jun 5, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jun 5, 2025
@anutosh491
Copy link
Member Author

/cherry-pick 7ca7bcb

@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

/pull-request #142909

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Jun 5, 2025
anutosh491 added a commit to anutosh491/llvm-project that referenced this pull request Jun 5, 2025
anutosh491 added a commit to anutosh491/llvm-project that referenced this pull request Jun 5, 2025
…ed in all language models (llvm#142749)"

This broke CI on platforms such as PPC64LE and AIX due to _Float16 not being supported.
We will reintroduce the changes later with proper platform guards and tests.

This reverts commit 7ca7bcb.
vgvassilev pushed a commit that referenced this pull request Jun 5, 2025
…ed in all language models (#142749) (#142933)

This broke CI on platforms such as PPC64LE and AIX due to _Float16 not being supported.
We will reintroduce the changes later with proper platform guards and tests.

This reverts commit 7ca7bcb.
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…l language models (llvm#142749)

As can be seen through the docs
(https://github.com/llvm/llvm-project/blob/7e1fa09ce2a228c949ce4490c98f2c73ed8ada00/clang/docs/LanguageExtensions.rst#c-keywords-supported-in-all-language-modes),
Clang supports certain C keywords in all language modes — this patch
ensures clang-repl handles them consistently.

Here's an example testing all the above keywords. We have everything in
place except `_Imaginary` (_Complex works but _Imaginary doesn't which
was weird) and `_Noreturn`
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…ed in all language models (llvm#142749) (llvm#142933)

This broke CI on platforms such as PPC64LE and AIX due to _Float16 not being supported.
We will reintroduce the changes later with proper platform guards and tests.

This reverts commit 7ca7bcb.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…l language models (llvm#142749)

As can be seen through the docs
(https://github.com/llvm/llvm-project/blob/7e1fa09ce2a228c949ce4490c98f2c73ed8ada00/clang/docs/LanguageExtensions.rst#c-keywords-supported-in-all-language-modes),
Clang supports certain C keywords in all language modes — this patch
ensures clang-repl handles them consistently.

Here's an example testing all the above keywords. We have everything in
place except `_Imaginary` (_Complex works but _Imaginary doesn't which
was weird) and `_Noreturn`
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…ed in all language models (llvm#142749) (llvm#142933)

This broke CI on platforms such as PPC64LE and AIX due to _Float16 not being supported.
We will reintroduce the changes later with proper platform guards and tests.

This reverts commit 7ca7bcb.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
Development

Successfully merging this pull request may close these issues.

3 participants