Skip to content

Conversation

@qiongsiwu
Copy link
Contributor

@qiongsiwu qiongsiwu commented Dec 13, 2024

The parser hangs when processing types/variables prefixed by :: as an optional scope specifier. For example,

- (instancetype)init:(::A *) foo;

The parser should not hang, and it should emit an error. This PR implements the error check.

rdar://140885078

@qiongsiwu qiongsiwu requested a review from Bigcheese December 13, 2024 17:45
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 13, 2024

@llvm/pr-subscribers-clang

Author: Qiongsi Wu (qiongsiwu)

Changes

The parser hangs when processing method parameters with types prefixed by ::. For example,

- (instancetype)init:(::A *) foo;

The parser should not hang, and it should emit an error. This PR implements the error check.


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

2 Files Affected:

  • (modified) clang/lib/Parse/Parser.cpp (+7-1)
  • (added) clang/test/Parser/objc-coloncolon.m (+5)
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 36e56a92c3092e..aa78d702553172 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -2222,8 +2222,14 @@ bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(
     }
   }
 
-  if (SS.isEmpty())
+  if (SS.isEmpty()) {
+    if (getLangOpts().ObjC && Tok.is(tok::coloncolon)) {
+      // ObjectiveC does not allow :: as as a scope token.
+      Diag(ConsumeToken(), diag::err_expected_type);
+      return true;
+    }
     return false;
+  }
 
   // A C++ scope specifier that isn't followed by a typename.
   AnnotateScopeToken(SS, IsNewScope);
diff --git a/clang/test/Parser/objc-coloncolon.m b/clang/test/Parser/objc-coloncolon.m
new file mode 100644
index 00000000000000..e8a09898263bb3
--- /dev/null
+++ b/clang/test/Parser/objc-coloncolon.m
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -x objective-c -fsyntax-only -verify %s
+
+@interface A
+- (instancetype)init:(::A *) foo; // expected-error {{expected a type}} 
+@end

@qiongsiwu qiongsiwu self-assigned this Dec 13, 2024
@qiongsiwu
Copy link
Contributor Author

This fix tripped some tests in libc++. I am investigating.

@cyndyishida cyndyishida requested a review from vsapsai December 16, 2024 19:02
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

This should have a release note.

@vsapsai
Copy link
Collaborator

vsapsai commented Dec 17, 2024

How does it work in Objective-C++? I don't know even if we have a test but hope we do.

@cor3ntin
Copy link
Contributor

How does it work in Objective-C++? I don't know even if we have a test but hope we do.

Right, this needs tests for Objective-C++

@vsapsai
Copy link
Collaborator

vsapsai commented Dec 17, 2024

I don't know if we have a test for it but I've realized there are cases where you can have a legitimate double colon in Objective-C. For example,

@interface NSObject
@end

@implementation NSObject
- (void)performSelector:(SEL)selector {}

- (void)double:(int)firstArg :(int)secondArg colon:(int)thirdArg {}

- (void)test {
  [self performSelector:@selector(double::colon:)];
}
@end

It's not a method parameter type, so it is possible your code isn't executed. But it is worth checking if we test this case.

@qiongsiwu qiongsiwu changed the title [clang][ObjectiveC] Fix Parsing Method Parameter Types with the :: Prefix [clang][ObjectiveC] Fix Parsing Types with the :: Optional Scope Specifier Dec 17, 2024
@qiongsiwu
Copy link
Contributor Author

Thanks for the feedback everyone! The PR is updated to address the review comments.

@qiongsiwu
Copy link
Contributor Author

qiongsiwu commented Dec 18, 2024

The Objective-C test is modified to contain valid code with :: to include @vsapsai 's suggestion . https://github.com/llvm/llvm-project/pull/119908/files#diff-0c0e8844905a75c581903811a694e614f49b2945e2254a76bb5a215443028554R11

@qiongsiwu
Copy link
Contributor Author

Gentle ping for review. Thanks so much!

@qiongsiwu qiongsiwu changed the title [clang][ObjectiveC] Fix Parsing Types with the :: Optional Scope Specifier [clang][ObjectiveC] Fix Parsing the :: Optional Scope Specifier Dec 20, 2024
@qiongsiwu qiongsiwu merged commit 1418018 into llvm:main Dec 20, 2024
9 checks passed
@MaskRay
Copy link
Member

MaskRay commented Dec 22, 2024

(https://discourse.llvm.org/t/hidden-emails-on-github-should-we-do-something-about-it/74223 the convention is to disable github's hide-email feature)

fredriss pushed a commit to swiftlang/llvm-project that referenced this pull request Jan 8, 2025
…vm#119908)

The parser hangs when processing types/variables prefixed by `::` as an
optional scope specifier. For example,
```
- (instancetype)init:(::A *) foo;
```

The parser should not hang, and it should emit an error. This PR
implements the error check.

rdar://140885078
(cherry picked from commit 1418018)
fredriss added a commit to swiftlang/llvm-project that referenced this pull request Jan 8, 2025
…on_fix

[clang][ObjectiveC] Fix Parsing the `::` Optional Scope Specifier (llvm#119908)
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.

7 participants