Skip to content

[ObjC] Stop hard-coding the list of keywords that are allowed in selectors #138952

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
May 13, 2025

Conversation

ahatanak
Copy link
Collaborator

@ahatanak ahatanak commented May 7, 2025

The design intent is that ObjC selector pieces can be arbitrary keywords, including basic C keywords like if, and the grammar permits this and makes it unambiguous.

Allow any token that has an identifier (except __attribute__) to be part of a selector.

rdar://150383689

selectors

The design intent is that ObjC selector pieces can be arbitrary
keywords, including basic C keywords like `if`, and the grammar permits
this and makes it unambiguous.

Use TokenKinds.def to autogenerate the list of allowed keywords instead.

rdar://150383689
@ahatanak ahatanak requested a review from rjmccall May 7, 2025 20:07
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 7, 2025
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-clang

Author: Akira Hatanaka (ahatanak)

Changes

The design intent is that ObjC selector pieces can be arbitrary keywords, including basic C keywords like if, and the grammar permits this and makes it unambiguous.

Use TokenKinds.def to autogenerate the list of allowed keywords instead.

rdar://150383689


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

2 Files Affected:

  • (modified) clang/lib/Parse/ParseObjc.cpp (+2-69)
  • (modified) clang/test/SemaObjCXX/cxxoperator-selector.mm (+5)
diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp
index 6496b4fba54f2..a427cb24e48bb 100644
--- a/clang/lib/Parse/ParseObjc.cpp
+++ b/clang/lib/Parse/ParseObjc.cpp
@@ -1095,75 +1095,8 @@ IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
   }
 
   case tok::identifier:
-  case tok::kw_asm:
-  case tok::kw_auto:
-  case tok::kw_bool:
-  case tok::kw_break:
-  case tok::kw_case:
-  case tok::kw_catch:
-  case tok::kw_char:
-  case tok::kw_class:
-  case tok::kw_const:
-  case tok::kw_const_cast:
-  case tok::kw_continue:
-  case tok::kw_default:
-  case tok::kw_delete:
-  case tok::kw_do:
-  case tok::kw_double:
-  case tok::kw_dynamic_cast:
-  case tok::kw_else:
-  case tok::kw_enum:
-  case tok::kw_explicit:
-  case tok::kw_export:
-  case tok::kw_extern:
-  case tok::kw_false:
-  case tok::kw_float:
-  case tok::kw_for:
-  case tok::kw_friend:
-  case tok::kw_goto:
-  case tok::kw_if:
-  case tok::kw_inline:
-  case tok::kw_int:
-  case tok::kw_long:
-  case tok::kw_mutable:
-  case tok::kw_namespace:
-  case tok::kw_new:
-  case tok::kw_operator:
-  case tok::kw_private:
-  case tok::kw_protected:
-  case tok::kw_public:
-  case tok::kw_register:
-  case tok::kw_reinterpret_cast:
-  case tok::kw_restrict:
-  case tok::kw_return:
-  case tok::kw_short:
-  case tok::kw_signed:
-  case tok::kw_sizeof:
-  case tok::kw_static:
-  case tok::kw_static_cast:
-  case tok::kw_struct:
-  case tok::kw_switch:
-  case tok::kw_template:
-  case tok::kw_this:
-  case tok::kw_throw:
-  case tok::kw_true:
-  case tok::kw_try:
-  case tok::kw_typedef:
-  case tok::kw_typeid:
-  case tok::kw_typename:
-  case tok::kw_typeof:
-  case tok::kw_union:
-  case tok::kw_unsigned:
-  case tok::kw_using:
-  case tok::kw_virtual:
-  case tok::kw_void:
-  case tok::kw_volatile:
-  case tok::kw_wchar_t:
-  case tok::kw_while:
-  case tok::kw__Bool:
-  case tok::kw__Complex:
-  case tok::kw___alignof:
-  case tok::kw___auto_type:
+#define KEYWORD(X,Y) case tok::kw_ ## X:
+#include "clang/Basic/TokenKinds.def"
     IdentifierInfo *II = Tok.getIdentifierInfo();
     SelectorLoc = ConsumeToken();
     return II;
diff --git a/clang/test/SemaObjCXX/cxxoperator-selector.mm b/clang/test/SemaObjCXX/cxxoperator-selector.mm
index 8134b82ebacbb..2348bd783f519 100644
--- a/clang/test/SemaObjCXX/cxxoperator-selector.mm
+++ b/clang/test/SemaObjCXX/cxxoperator-selector.mm
@@ -19,5 +19,10 @@ - (id) and{return 0; };
 - (id) xor{return 0; };
 - (id) or{return 0; };
 
+- (void)decltype {}
+- (void)constexpr {}
+- (void)noexcept {}
+- (void)nullptr {}
+
 - (void)dataSetForValuesBetween:(NSDate *)startDate and:(NSDate *)endDate { return; }
 @end

Copy link

github-actions bot commented May 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

@ahatanak ahatanak merged commit 7460056 into llvm:main May 13, 2025
11 checks passed
@ahatanak ahatanak deleted the objc-selector-keyword branch May 13, 2025 23:25
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
None yet
Development

Successfully merging this pull request may close these issues.

4 participants