[clang][ObjectiveC] Fix Parsing the :: Optional Scope Specifier (#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
This commit is contained in:
Qiongsi Wu
2024-12-20 09:47:26 -08:00
committed by GitHub
parent 0550c3292b
commit 1418018502
4 changed files with 39 additions and 1 deletions

View File

@@ -708,6 +708,9 @@ Improvements to Clang's diagnostics
- Fix -Wdangling false positives on conditional operators (#120206).
- Fixed a bug where Clang hung on an unsupported optional scope specifier ``::`` when parsing
Objective-C. Clang now emits a diagnostic message instead of hanging.
Improvements to Clang's time-trace
----------------------------------

View File

@@ -2222,8 +2222,15 @@ bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(
}
}
if (SS.isEmpty())
if (SS.isEmpty()) {
if (getLangOpts().ObjC && !getLangOpts().CPlusPlus &&
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);

View File

@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -x objective-c -fsyntax-only -Wno-objc-root-class -verify %s
int GV = 42;
@interface A
+ (int) getGV;
- (instancetype)init:(::A *) foo; // expected-error {{expected a type}}
@end
@implementation A
- (void)performSelector:(SEL)selector {}
- (void)double:(int)firstArg :(int)secondArg colon:(int)thirdArg {}
- (void)test {
// The `::` below should not trigger an error.
[self performSelector:@selector(double::colon:)];
}
+ (int) getGV { return ::GV; } // expected-error {{expected a type}}
- (instancetype)init:(::A *) foo { return self; } // expected-error {{expected a type}}
@end

View File

@@ -0,0 +1,9 @@
// Test to make sure the parser does not get stuck on the optional
// scope specifier on the type B.
// RUN: %clang_cc1 -fsyntax-only %s
class B;
@interface A
- (void) init:(::B *) foo;
@end