Skip to content

Commit e151f01

Browse files
committedDec 7, 2016
[ObjC++] Don't enter a C++ declarator scope when the current context is
an Objective-C declaration This commit ensures that Sema won't enter a C++ declarator scope when the current context is an Objective-C declaration. This prevents an assertion failure in EnterDeclaratorContext that's used to ensure that current context will be restored correctly after exiting the declarator context. rdar://20560175 Differential Revision: https://reviews.llvm.org/D26922 llvm-svn: 288893
1 parent 59336a0 commit e151f01

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
 

Diff for: ‎clang/lib/Parse/ParseDecl.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5264,6 +5264,14 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
52645264
// Change the declaration context for name lookup, until this function
52655265
// is exited (and the declarator has been parsed).
52665266
DeclScopeObj.EnterDeclaratorScope();
5267+
else if (getObjCDeclContext()) {
5268+
// Ensure that we don't interpret the next token as an identifier when
5269+
// dealing with declarations in an Objective-C container.
5270+
D.SetIdentifier(nullptr, Tok.getLocation());
5271+
D.setInvalidType(true);
5272+
ConsumeToken();
5273+
goto PastIdentifier;
5274+
}
52675275
}
52685276

52695277
// C++0x [dcl.fct]p14:

Diff for: ‎clang/lib/Sema/SemaCXXScopeSpec.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,11 @@ void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
10011001
bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
10021002
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
10031003

1004+
// Don't enter a declarator context when the current context is an Objective-C
1005+
// declaration.
1006+
if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext))
1007+
return false;
1008+
10041009
NestedNameSpecifier *Qualifier = SS.getScopeRep();
10051010

10061011
// There are only two places a well-formed program may qualify a

Diff for: ‎clang/test/SemaObjCXX/crash.mm

+35
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,38 @@ @implementation Test
2525
// expected-warning@-2 {{variadic templates are a C++11 extension}}
2626
#endif
2727
@end
28+
29+
// rdar://20560175
30+
31+
struct OuterType {
32+
typedef int InnerType;
33+
};
34+
35+
namespace ns {
36+
typedef int InnerType;
37+
};
38+
39+
@protocol InvalidProperties
40+
41+
@property (nonatomic) (OuterType::InnerType) invalidTypeParens;
42+
// expected-error@-1 {{type name requires a specifier or qualifier}}
43+
// expected-error@-2 {{property requires fields to be named}}
44+
// expected-error@-3 {{expected ';' at end of declaration list}}
45+
// expected-error@-4 {{C++ requires a type specifier for all declarations}}
46+
// expected-error@-5 {{cannot declare variable inside @interface or @protocol}}
47+
48+
@property (nonatomic) (ns::InnerType) invalidTypeParens2;
49+
// expected-error@-1 {{type name requires a specifier or qualifier}}
50+
// expected-error@-2 {{property requires fields to be named}}
51+
// expected-error@-3 {{expected ';' at end of declaration list}}
52+
// expected-error@-4 {{C++ requires a type specifier for all declarations}}
53+
// expected-error@-5 {{cannot declare variable inside @interface or @protocol}}
54+
55+
@property (nonatomic) int OuterType::InnerType; // expected-error {{property requires fields to be named}}
56+
57+
@property (nonatomic) int OuterType::InnerType foo; // expected-error {{property requires fields to be named}}
58+
// expected-error@-1 {{expected ';' at end of declaration list}}
59+
// expected-error@-2 {{C++ requires a type specifier for all declarations}}
60+
// expected-error@-3 {{cannot declare variable inside @interface or @protocol}}
61+
62+
@end

0 commit comments

Comments
 (0)
Please sign in to comment.