Skip to content

Commit 8bdf13b

Browse files
[ObjC] Name lookup in methods shouldn't allow shadowing types (llvm#116683)
Arguably as a bug, Clang has previously not mixed up Objective-C parameter names with types. This allows developers to write parameter names that _should_ shadow type names, but don't. For instance: @interface Foo -(void)foo:(int)id bar:(id)name; // OK @EnD Commit 9778808 changed the way that parameters are parsed to bring it more in line with how C parameters are parsed, but it breaks the example above. Given an expectation that the change wouldn't introduce source breaks, this is not something we can go forward with. 9778808 did this so that late-parsed attributes could reference Objective-C parameters. This change buffers Objective-C parameter info until after all parameters are parsed and turns them into parameter declarations before realizing late-parsed attributes instead. Radar-ID: 139996306
1 parent 39e65b8 commit 8bdf13b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

clang/lib/Parse/ParseObjc.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
14541454

14551455
SmallVector<const IdentifierInfo *, 12> KeyIdents;
14561456
SmallVector<SourceLocation, 12> KeyLocs;
1457-
SmallVector<ParmVarDecl *, 12> ObjCParamInfo;
1457+
SmallVector<SemaObjC::ObjCArgInfo, 12> ArgInfos;
14581458
ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
14591459
Scope::FunctionDeclarationScope | Scope::DeclScope);
14601460

@@ -1495,9 +1495,7 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
14951495
ArgInfo.NameLoc = Tok.getLocation();
14961496
ConsumeToken(); // Eat the identifier.
14971497

1498-
ParmVarDecl *Param = Actions.ObjC().ActOnMethodParmDeclaration(
1499-
getCurScope(), ArgInfo, ObjCParamInfo.size(), MethodDefinition);
1500-
ObjCParamInfo.push_back(Param);
1498+
ArgInfos.push_back(ArgInfo);
15011499
KeyIdents.push_back(SelIdent);
15021500
KeyLocs.push_back(selLoc);
15031501

@@ -1557,6 +1555,17 @@ Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
15571555
nullptr));
15581556
}
15591557

1558+
// Turn ArgInfos into parameters. This must happen after parsing all
1559+
// parameters for bug compatibility with previous versions of Clang. (For
1560+
// instance, if a method declares a parameter called "id", that parameter must
1561+
// not shadow the "id" type.)
1562+
SmallVector<ParmVarDecl *, 12> ObjCParamInfo;
1563+
for (auto &ArgInfo : ArgInfos) {
1564+
ParmVarDecl *Param = Actions.ObjC().ActOnMethodParmDeclaration(
1565+
getCurScope(), ArgInfo, ObjCParamInfo.size(), MethodDefinition);
1566+
ObjCParamInfo.push_back(Param);
1567+
}
1568+
15601569
// FIXME: Add support for optional parameter list...
15611570
// If attributes exist after the method, parse them.
15621571
MaybeParseAttributes(PAKM_CXX11 | (getLangOpts().ObjC ? PAKM_GNU : 0),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class %s
2+
3+
4+
@interface Foo
5+
-(void)paramNamedID:(int)id usesIDType:(id)notShadowed;
6+
-(void)paramNamedID:(int)id, id notShadowed; // expected-warning{{use of C-style parameters in Objective-C method declarations is deprecated}}
7+
@end

0 commit comments

Comments
 (0)