/// \brief Code completion occurs at the beginning of the
/// initialization statement (or expression) in a for loop.
CCC_ForInit,
- /// \brief Code completion ocurs within the condition of an if,
+ /// \brief Code completion occurs within the condition of an if,
/// while, switch, or for statement.
- CCC_Condition
+ CCC_Condition,
+ /// \brief Code completion occurs within the body of a function on a
+ /// recovery path, where we do not have a specific handle on our position
+ /// in the grammar.
+ CCC_RecoveryInFunction
};
/// \brief Code completion for an ordinary name that occurs within the given
assert(!isTokenStringLiteral() && !isTokenParen() && !isTokenBracket() &&
!isTokenBrace() &&
"Should consume special tokens with Consume*Token");
+ if (Tok.is(tok::code_completion)) {
+ CodeCompletionRecovery();
+ return ConsumeCodeCompletionToken();
+ }
+
PrevTokLocation = Tok.getLocation();
PP.Lex(Tok);
return PrevTokLocation;
return PrevTokLocation;
}
+ /// \brief Consume the current code-completion token.
+ ///
+ /// This routine should be called to consume the code-completion token once
+ /// a code-completion action has already been invoked.
+ SourceLocation ConsumeCodeCompletionToken() {
+ assert(Tok.is(tok::code_completion));
+ PrevTokLocation = Tok.getLocation();
+ PP.Lex(Tok);
+ return PrevTokLocation;
+ }
+
+ ///\ brief When we are consuming a code-completion token within having
+ /// matched specific position in the grammar, provide code-completion results
+ /// based on context.
+ void CodeCompletionRecovery();
+
/// GetLookAheadToken - This peeks ahead N tokens and returns that token
/// without consuming any tokens. LookAhead(0) returns 'Tok', LookAhead(1)
/// returns the token after Tok, etc.
// Only do the eof -> code_completion translation once.
PP->SetCodeCompletionPoint(0, 0, 0);
+
+ // Silence any diagnostics that occur once we hit the code-completion point.
+ PP->getDiagnostics().setSuppressAllDiagnostics(true);
return true;
}
CCC = Action::CCC_ObjCImplementation;
Actions.CodeCompleteOrdinaryName(CurScope, CCC);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
DS.SetRangeStart(Tok.getLocation());
if (Tok.is(tok::code_completion)) {
// Code completion for an enum name.
Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
llvm::OwningPtr<AttributeList> Attr;
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteNamespaceDecl(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
SourceLocation IdentLoc;
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteNamespaceAliasDecl(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
CXXScopeSpec SS;
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteUsing(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.is(tok::kw_namespace))
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteUsingDirective(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
CXXScopeSpec SS;
if (Tok.is(tok::code_completion)) {
// Code completion for a struct, class, or union name.
Actions.CodeCompleteTag(CurScope, TagType);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
AttributeList *AttrList = 0;
Parser::OwningExprResult Parser::ParseAssignmentExpression() {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Expression);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.is(tok::kw_throw))
return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
case tok::code_completion:
Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Expression);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
NotCastExpr, TypeOfCast);
case tok::l_square:
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::r_paren)) {
Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
OpLoc, OpKind == tok::arrow);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (MayBePseudoDestructor) {
if (Tok.is(tok::code_completion)) {
if (Completer)
(Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
OwningExprResult Expr(ParseAssignmentExpression());
// Code completion for a nested-name-specifier, where the code
// code completion token follows the '::'.
Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
}
bool ConvertToBoolean) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Condition);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (!isCXXConditionDeclaration()) {
Actions.CodeCompleteOperatorName(CurScope);
// Consume the operator token.
- ConsumeToken();
+ ConsumeCodeCompletionToken();
// Don't try to parse any further.
return true;
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
switch (Tok.getObjCKeywordID()) {
// Code completion after '@interface'.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCInterfaceDecl(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
IdentifierInfo *categoryId = 0;
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId, nameLoc);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// For ObjC2, the category name is optional (not an error).
// Code completion of superclass names.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCSuperclass(CurScope, nameId, nameLoc);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
Actions.CodeCompleteOrdinaryName(CurScope,
ObjCImpDecl? Action::CCC_ObjCImplementation
: Action::CCC_ObjCInterface);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// If we don't have an @ directive, parse it as a function definition.
SourceLocation AtLoc = ConsumeToken(); // the "@"
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
break;
}
// EOF. In the former case, eat the @end. In the later case, emit an error.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
} else if (Tok.isObjCAtKeyword(tok::objc_end))
ConsumeToken(); // the "end" identifier
else
while (1) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
const IdentifierInfo *II = Tok.getIdentifierInfo();
else
Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
Methods, NumMethods);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus,
/*ReturnType=*/0, IDecl);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// Parse the return type if present.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus,
ReturnType, IDecl);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// Now parse the selector.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
ProtocolIdents.size());
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCAtVisibility(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
switch (Tok.getObjCKeywordID()) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(CurScope,
Action::CCC_ObjCInstanceVariableList);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
struct ObjCIvarCallback : FieldCallback {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCProtocolDecl(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
// Code completion after '@implementation'.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCImplementationDecl(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId, nameLoc);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.is(tok::identifier)) {
while (true) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
ObjCImpDecl);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
while (true) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
if (Tok.isNot(tok::identifier)) {
Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteObjCAtStatement(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
return StmtError();
}
switch (Tok.getKind()) {
case tok::code_completion:
Actions.CodeCompleteObjCAtExpression(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
return ExprError();
case tok::string_literal: // primary-expression: string-literal
else
Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
0, 0);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// Parse objc-selector
Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
KeyIdents.data(),
KeyIdents.size());
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// Check for another keyword selector.
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteCase(CurScope);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
/// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
Actions.CodeCompleteOrdinaryName(CurScope,
C99orCXXorObjC? Action::CCC_ForInit
: Action::CCC_Expression);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
}
// Parse the first part of the for specifier.
/// returned.
bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
const char *Msg, tok::TokenKind SkipToTok) {
- if (Tok.is(ExpectedTok)) {
+ if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
ConsumeAnyToken();
return false;
}
case tok::eof:
// Ran out of tokens.
return false;
-
+
+ case tok::code_completion:
+ ConsumeToken();
+ return false;
+
case tok::l_paren:
// Recursively skip properly-nested parens.
ConsumeParen();
Actions.CodeCompleteOrdinaryName(CurScope,
ObjCImpDecl? Action::CCC_ObjCImplementation
: Action::CCC_Namespace);
- ConsumeToken();
+ ConsumeCodeCompletionToken();
return ParseExternalDeclaration(Attr);
case tok::kw_using:
case tok::kw_namespace:
return false;
}
+void Parser::CodeCompletionRecovery() {
+ for (Scope *S = CurScope; S; S = S->getParent()) {
+ if (S->getFlags() & Scope::FnScope) {
+ Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_RecoveryInFunction);
+ return;
+ }
+
+ if (S->getFlags() & Scope::ClassScope) {
+ Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Class);
+ return;
+ }
+ }
+
+ Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Namespace);
+}
+
// Anchor the Parser::FieldCallback vtable to this translation unit.
// We use a spurious method instead of the destructor because
// destroying FieldCallbacks can actually be slightly
}
}
}
-
- // Once we've printed the code-completion results, suppress remaining
- // diagnostics.
- // FIXME: Move this somewhere else!
- SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
}
void
delete CCS;
}
}
-
- // Once we've printed the code-completion results, suppress remaining
- // diagnostics.
- // FIXME: Move this somewhere else!
- SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
}
void
CCS->Serialize(OS);
delete CCS;
}
-
- // Once we've printed the code-completion results, suppress remaining
- // diagnostics.
- // FIXME: Move this somewhere else!
- SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
}
void
CCS->Serialize(OS);
delete CCS;
}
-
- // Once we've printed the code-completion results, suppress remaining
- // diagnostics.
- // FIXME: Move this somewhere else!
- SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
}
case Action::CCC_Statement:
case Action::CCC_ForInit:
case Action::CCC_Condition:
+ case Action::CCC_RecoveryInFunction:
break;
}
}
AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true);
break;
+ case Action::CCC_RecoveryInFunction:
case Action::CCC_Statement: {
Results.AddResult(Result("typedef"));
case CCC_Condition:
Results.setFilter(&ResultBuilder::IsOrdinaryName);
break;
+
+ case CCC_RecoveryInFunction:
+ // Unfiltered
+ break;
}
CodeCompletionDeclConsumer Consumer(Results, CurContext);
}
Results.ExitScope();
-
- // This also suppresses remaining diagnostics.
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
}
--- /dev/null
+/* Run lines are at the end, since line/column matter in this test. */
+
+@interface A
+- (void)method:(int)x;
+@end
+
+@implementation A
+- (void)method:(int)x {
+ A *a = [A method:1];
+ blarg * blah = wibble
+}
+@end
+
+// RUN: c-index-test -code-completion-at=%s:9:20 %s 2>%t | FileCheck -check-prefix=CHECK-CC1 %s
+// RUN: not grep error %t
+// CHECK-CC1: NotImplemented:{TypedText @encode}{LeftParen (}{Placeholder type-name}{RightParen )}
+// CHECK-CC1: NotImplemented:{TypedText _Bool}
+// CHECK-CC1: VarDecl:{ResultType A *}{TypedText a}
+// CHECK-CC1: NotImplemented:{TypedText sizeof}{LeftParen (}{Placeholder expression-or-type}{RightParen )}
+
+// RUN: c-index-test -code-completion-at=%s:10:24 %s 2>%t | FileCheck -check-prefix=CHECK-CC1 %s