From b216c8861c60af9c56c900a485233255c4452df2 Mon Sep 17 00:00:00 2001 From: Steve Naroff <snaroff@apple.com> Date: Tue, 9 Oct 2007 22:01:59 +0000 Subject: [PATCH] Make sure methods with no return type default to "id". This fixes a crasher in Sema::MatchTwoMethodDeclarations(), identified by selector-overload.m (just added). Added Action::ActOnTranslationUnitScope() and renamed Action::PopScope to ActOnPopScope. Added a Translation Unit Scope instance variable to Sema (will be very useful to ObjC-related actions, since ObjC declarations are always file-scoped). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42817 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/ASTContext.cpp | 1 - Parse/Parser.cpp | 5 ++-- Sema/Sema.cpp | 4 +++ Sema/Sema.h | 8 +++++- Sema/SemaDecl.cpp | 15 ++++++++++-- clang.xcodeproj/project.pbxproj | 3 +-- include/clang/Parse/Action.h | 12 ++++++--- test/Sema/selector-overload.m | 43 +++++++++++++++++++++++++++++++++ 8 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 test/Sema/selector-overload.m diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index c9959df31fd..6608330b680 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -110,7 +110,6 @@ void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); } - void ASTContext::InitBuiltinTypes() { assert(VoidTy.isNull() && "Context reinitialized?"); diff --git a/Parse/Parser.cpp b/Parse/Parser.cpp index 117afab5cfa..fc5c4ae4f71 100644 --- a/Parse/Parser.cpp +++ b/Parse/Parser.cpp @@ -192,7 +192,7 @@ void Parser::ExitScope() { // Inform the actions module that this scope is going away if there are any // decls in it. if (!CurScope->decl_empty()) - Actions.PopScope(Tok.getLocation(), CurScope); + Actions.ActOnPopScope(Tok.getLocation(), CurScope); Scope *OldScope = CurScope; CurScope = OldScope->getParent(); @@ -228,7 +228,8 @@ void Parser::Initialize() { // Create the translation unit scope. Install it as the current scope. assert(CurScope == 0 && "A scope is already active?"); EnterScope(Scope::DeclScope); - + Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope); + // Install builtin types. // TODO: Move this someplace more useful. { diff --git a/Sema/Sema.cpp b/Sema/Sema.cpp index 1a012706927..125b0cb63fd 100644 --- a/Sema/Sema.cpp +++ b/Sema/Sema.cpp @@ -19,6 +19,10 @@ using namespace clang; +void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { + TUScope = S; +} + Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup) : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { diff --git a/Sema/Sema.h b/Sema/Sema.h index e2ec394901c..6c2b41e3ce3 100644 --- a/Sema/Sema.h +++ b/Sema/Sema.h @@ -113,6 +113,9 @@ class Sema : public Action { /// This list is populated upon the creation of a Sema object. IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ]; + /// Translation Unit Scope - useful to many Objective-C actions that need + /// to lookup file scope declarations (like classes, protocols, etc.). + Scope *TUScope; public: Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup); @@ -161,7 +164,10 @@ private: virtual DeclTy *ActOnStartOfFunctionDef(Scope *S, Declarator &D); virtual DeclTy *ActOnFunctionDefBody(DeclTy *Decl, StmtTy *Body); - virtual void PopScope(SourceLocation Loc, Scope *S); + + /// Scope actions. + virtual void ActOnPopScope(SourceLocation Loc, Scope *S); + virtual void ActOnTranslationUnitScope(SourceLocation Loc, Scope *S); /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with /// no declarator (e.g. "struct foo;") is parsed. diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index d85ee6bc301..ba6c7d0175e 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -34,7 +34,7 @@ Sema::DeclTy *Sema::isTypeName(const IdentifierInfo &II, Scope *S) const { return 0; } -void Sema::PopScope(SourceLocation Loc, Scope *S) { +void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) { if (S->decl_empty()) return; assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!"); @@ -1782,7 +1782,18 @@ Sema::DeclTy *Sema::ActOnMethodDeclaration(SourceLocation MethodLoc, VarDecl::None, 0); Params.push_back(Param); } - QualType resultDeclType = QualType::getFromOpaquePtr(ReturnType); + QualType resultDeclType; + + if (ReturnType) + resultDeclType = QualType::getFromOpaquePtr(ReturnType); + else { // get the type for "id". + IdentifierInfo *IdIdent = &Context.Idents.get("id"); + ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary, + SourceLocation(), TUScope); + TypedefDecl *IdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl); + assert(IdTypedef && "ActOnMethodDeclaration(): Couldn't find 'id' type"); + resultDeclType = IdTypedef->getUnderlyingType(); + } ObjcMethodDecl* ObjcMethod = new ObjcMethodDecl(MethodLoc, Sel, resultDeclType, 0, -1, AttrList, MethodType == tok::minus, diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index c7f4908c0dd..0db725b41a9 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -237,7 +237,7 @@ 84AF36A00CB17A3B00C820A5 /* DeclObjC.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = DeclObjC.h; path = clang/AST/DeclObjC.h; sourceTree = "<group>"; }; 84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; }; 84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; }; - 8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; }; + 8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; }; DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; }; DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; }; DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; }; @@ -739,7 +739,6 @@ 08FB7793FE84155DC02AAC07 /* Project object */ = { isa = PBXProject; buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */; - compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 1; mainGroup = 08FB7794FE84155DC02AAC07 /* clang */; projectDirPath = ""; diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 31045f3cf44..af8170fe904 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -135,10 +135,14 @@ public: } - /// PopScope - This callback is called immediately before the specified scope - /// is popped and deleted. - virtual void PopScope(SourceLocation Loc, Scope *S) {} - + /// ActOnPopScope - This callback is called immediately before the specified + /// scope is popped and deleted. + virtual void ActOnPopScope(SourceLocation Loc, Scope *S) {} + + /// ActOnTranslationUnitScope - This callback is called once, immediately + /// after creating the translation unit scope (in Parser::Initialize). + virtual void ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {} + /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with /// no declarator (e.g. "struct foo;") is parsed. virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { diff --git a/test/Sema/selector-overload.m b/test/Sema/selector-overload.m new file mode 100644 index 00000000000..8b433a48030 --- /dev/null +++ b/test/Sema/selector-overload.m @@ -0,0 +1,43 @@ +// RUN: clang %s -parse-ast +#import <Foundation/NSObject.h> + +struct D { + double d; +}; + +@interface Foo : NSObject + +- method:(int)a; +- method:(int)a; + +@end + +@interface Bar : NSObject + +- method:(void *)a; + +@end + +@interface Car : NSObject + +- method:(struct D)a; + +@end + +@interface Zar : NSObject + +- method:(float)a; + +@end + +@interface Rar : NSObject + +- method:(float)a; + +@end + +int main() { +// id xx = [[Car alloc] init]; + +// [xx method:4]; +} -- GitLab