From 5b4671c32835cfe7eee13e28fe8b13d1b53c9db6 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 18 Nov 2009 04:49:41 +0000 Subject: [PATCH] Code completion after @property, providing the names of forward-declared properties llvm-svn: 89196 --- clang/include/clang/Parse/Action.h | 6 ++++++ clang/lib/Parse/ParseObjc.cpp | 5 +++++ clang/lib/Sema/Sema.h | 3 ++- clang/lib/Sema/SemaCodeComplete.cpp | 22 +++++++++++++++++++--- clang/test/Index/complete-protocols.m | 9 +++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Parse/Action.h b/clang/include/clang/Parse/Action.h index ee852ff95cac..a9b32133234a 100644 --- a/clang/include/clang/Parse/Action.h +++ b/clang/include/clang/Parse/Action.h @@ -2365,6 +2365,12 @@ public: /// parsed. virtual void CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, unsigned NumProtocols) { } + + /// \brief Code completion for a protocol declaration or definition, after + /// the @protocol but before any identifier. + /// + /// \param S the scope in which the protocol declaration occurs. + virtual void CodeCompleteObjCProtocolDecl(Scope *S) { } //@} }; diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index dc173eaf4de7..65bd79d6b4f2 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -994,6 +994,11 @@ Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc, "ParseObjCAtProtocolDeclaration(): Expected @protocol"); ConsumeToken(); // the "protocol" identifier + if (Tok.is(tok::code_completion)) { + Actions.CodeCompleteObjCProtocolDecl(CurScope); + ConsumeToken(); + } + if (Tok.isNot(tok::identifier)) { Diag(Tok, diag::err_expected_ident); // missing protocol name. return DeclPtrTy(); diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 333cc00069ae..e16bfaa57c8b 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -4017,7 +4017,8 @@ public: virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver); virtual void CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, unsigned NumProtocols); - //@} + virtual void CodeCompleteObjCProtocolDecl(Scope *S); + //@} //===--------------------------------------------------------------------===// // Extra semantic analysis beyond the C type system diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index be1ddddd6995..9cecdadc867e 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -1830,6 +1830,7 @@ void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) { /// \brief Add all of the protocol declarations that we find in the given /// (translation unit) context. static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, + bool OnlyForwardDeclarations, ResultBuilder &Results) { typedef CodeCompleteConsumer::Result Result; @@ -1838,7 +1839,8 @@ static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, D != DEnd; ++D) { // Record any protocols we find. if (ObjCProtocolDecl *Proto = dyn_cast(*D)) - Results.MaybeAddResult(Result(Proto, 0), CurContext); + if (!OnlyForwardDeclarations || Proto->isForwardDecl()) + Results.MaybeAddResult(Result(Proto, 0), CurContext); // Record any forward-declared protocols we find. if (ObjCForwardProtocolDecl *Forward @@ -1847,7 +1849,8 @@ static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, P = Forward->protocol_begin(), PEnd = Forward->protocol_end(); P != PEnd; ++P) - Results.MaybeAddResult(Result(*P, 0), CurContext); + if (!OnlyForwardDeclarations || (*P)->isForwardDecl()) + Results.MaybeAddResult(Result(*P, 0), CurContext); } } } @@ -1864,7 +1867,20 @@ void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, Results.Ignore(Protocol); // Add all protocols. - AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, Results); + AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, + Results); + + Results.ExitScope(); + HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); +} + +void Sema::CodeCompleteObjCProtocolDecl(Scope *) { + ResultBuilder Results(*this); + Results.EnterNewScope(); + + // Add all protocols. + AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, + Results); Results.ExitScope(); HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); diff --git a/clang/test/Index/complete-protocols.m b/clang/test/Index/complete-protocols.m index f84b6248b8e6..89f61bcf9ac9 100644 --- a/clang/test/Index/complete-protocols.m +++ b/clang/test/Index/complete-protocols.m @@ -8,9 +8,18 @@ void f(id); +@protocol Protocol0; +@protocol NewProtocol +{ +} +@end + // RUN: c-index-test -code-completion-at=%s:9:11 %s | FileCheck -check-prefix=CHECK-CC1 %s // CHECK-CC1: ObjCProtocolDecl:{TypedText Protocol1} // CHECK-CC1: ObjCProtocolDecl:{TypedText Protocol2} // RUN: c-index-test -code-completion-at=%s:9:21 %s | FileCheck -check-prefix=CHECK-CC2 %s // CHECK-CC2-NOT: ObjCProtocolDecl:{TypedText Protocol1} // CHECK-CC2: ObjCProtocolDecl:{TypedText Protocol2} +// RUN: c-index-test -code-completion-at=%s:12:11 %s | FileCheck -check-prefix=CHECK-CC3 %s +// CHECK-CC3: ObjCProtocolDecl:{TypedText Protocol0} +// CHECK-CC3-NEXT: ObjCProtocolDecl:{TypedText Protocol2}