Skip to content

Commit 5c60af3

Browse files
authored
Merge pull request swiftlang#28403 from rintaro/parse-skipbracedblock-adjust
[Parse] Adjust skipBracedBlock()
2 parents f4002c8 + 9dfac63 commit 5c60af3

File tree

7 files changed

+34
-43
lines changed

7 files changed

+34
-43
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,10 @@ class Parser {
651651
/// plain Tok.is(T1) check).
652652
bool skipUntilTokenOrEndOfLine(tok T1);
653653

654+
/// Skip a braced block (e.g. function body). The current token must be '{'.
655+
/// Returns \c true if the parser hit the eof before finding matched '}'.
656+
bool skipBracedBlock();
657+
654658
/// If the parser is generating only a syntax tree, try loading the current
655659
/// node from a previously generated syntax tree.
656660
/// Returns \c true if the node has been loaded and inserted into the current

lib/Parse/ParseDecl.cpp

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,21 +2919,11 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
29192919
static unsigned skipUntilMatchingRBrace(Parser &P,
29202920
bool &HasPoundDirective,
29212921
bool &HasOperatorDeclarations,
2922-
bool &HasNestedClassDeclarations,
2923-
SyntaxParsingContext *&SyntaxContext) {
2922+
bool &HasNestedClassDeclarations) {
29242923
HasPoundDirective = false;
29252924
HasOperatorDeclarations = false;
29262925
HasNestedClassDeclarations = false;
29272926

2928-
bool isRootCtx = SyntaxContext->isRoot();
2929-
SyntaxParsingContext BlockItemListContext(SyntaxContext,
2930-
SyntaxKind::CodeBlockItemList);
2931-
if (isRootCtx) {
2932-
BlockItemListContext.setTransparent();
2933-
}
2934-
SyntaxParsingContext BlockItemContext(SyntaxContext,
2935-
SyntaxKind::CodeBlockItem);
2936-
SyntaxParsingContext BodyContext(SyntaxContext, SyntaxKind::TokenList);
29372927
unsigned OpenBraces = 1;
29382928

29392929
bool LastTokenWasFunc = false;
@@ -4117,8 +4107,7 @@ bool Parser::canDelayMemberDeclParsing(bool &HasOperatorDeclarations,
41174107
skipUntilMatchingRBrace(*this,
41184108
HasPoundDirective,
41194109
HasOperatorDeclarations,
4120-
HasNestedClassDeclarations,
4121-
SyntaxContext);
4110+
HasNestedClassDeclarations);
41224111
if (!HasPoundDirective)
41234112
BackTrack.cancelBacktrack();
41244113
return !BackTrack.willBacktrack();
@@ -4809,25 +4798,24 @@ static ParameterList *parseOptionalAccessorArgument(SourceLoc SpecifierLoc,
48094798
return ParameterList::create(P.Context, StartLoc, param, EndLoc);
48104799
}
48114800

4812-
static unsigned skipBracedBlock(Parser &P,
4813-
SyntaxParsingContext *&SyntaxContext) {
4814-
SyntaxParsingContext CodeBlockContext(SyntaxContext, SyntaxKind::CodeBlock);
4815-
P.consumeToken(tok::l_brace);
4801+
bool Parser::skipBracedBlock() {
4802+
SyntaxParsingContext disabled(SyntaxContext);
4803+
SyntaxContext->disable();
4804+
consumeToken(tok::l_brace);
48164805

48174806
// We don't care if a skipped function body contained any of these, so
48184807
// just ignore them.
48194808
bool HasPoundDirectives;
48204809
bool HasOperatorDeclarations;
48214810
bool HasNestedClassDeclarations;
48224811

4823-
unsigned OpenBraces = skipUntilMatchingRBrace(P,
4812+
unsigned OpenBraces = skipUntilMatchingRBrace(*this,
48244813
HasPoundDirectives,
48254814
HasOperatorDeclarations,
4826-
HasNestedClassDeclarations,
4827-
SyntaxContext);
4828-
if (P.consumeIf(tok::r_brace))
4815+
HasNestedClassDeclarations);
4816+
if (consumeIf(tok::r_brace))
48294817
OpenBraces--;
4830-
return OpenBraces;
4818+
return OpenBraces != 0;
48314819
}
48324820

48334821
/// Returns a descriptive name for the given accessor/addressor kind.
@@ -5697,20 +5685,8 @@ void Parser::consumeAbstractFunctionBody(AbstractFunctionDecl *AFD,
56975685
SourceRange BodyRange;
56985686
BodyRange.Start = Tok.getLoc();
56995687

5700-
// Consume the '{', and find the matching '}'.
5701-
unsigned OpenBraces = skipBracedBlock(*this, SyntaxContext);
5702-
if (OpenBraces != 0 && Tok.isNot(tok::code_complete)) {
5703-
assert(Tok.is(tok::eof));
5704-
// We hit EOF, and not every brace has a pair. Recover by searching
5705-
// for the next decl except variable decls and cutting off before
5706-
// that point.
5707-
backtrackToPosition(BeginParserPosition);
5708-
consumeToken(tok::l_brace);
5709-
while (Tok.is(tok::kw_var) || Tok.is(tok::kw_let) ||
5710-
(Tok.isNot(tok::eof) && !isStartOfDecl())) {
5711-
consumeToken();
5712-
}
5713-
}
5688+
// Advance the parser to the end of the block; '{' ... '}'.
5689+
skipBracedBlock();
57145690

57155691
BodyRange.End = PreviousLoc;
57165692

@@ -7152,12 +7128,13 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags,
71527128
if (parseIdentifier(name, nameLoc, diag::expected_precedencegroup_name)) {
71537129
// If the identifier is missing or a keyword or something, try to
71547130
// skip the entire body.
7155-
if (consumeIf(tok::l_brace)) {
7131+
if (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof) &&
7132+
peekToken().is(tok::l_brace))
7133+
consumeToken();
7134+
if (Tok.is(tok::l_brace)) {
7135+
consumeToken(tok::l_brace);
71567136
skipUntilDeclRBrace();
71577137
(void) consumeIf(tok::r_brace);
7158-
} else if (Tok.isNot(tok::eof) && peekToken().is(tok::l_brace)) {
7159-
consumeToken();
7160-
skipBracedBlock(*this, SyntaxContext);
71617138
}
71627139
return nullptr;
71637140
}

test/SourceKit/CodeComplete/complete_group_overloads.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func test005() {
8989
// BAR_INIT_0: ()
9090
// BAR_INIT_0-NEXT: (x: A)
9191
// BAR_INIT_0-NEXT: (x: B)
92+
// BAR_INIT_0-NEXT: .foo(self: Bar)
9293
// BAR_INIT_0-NEXT: .self
9394

9495
extension Bar {

test/SourceKit/CodeComplete/complete_requestlimit.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func test001() {
3232
// TOP_LEVEL_0_ALL-NEXT: z
3333
// TOP_LEVEL_0_ALL-NEXT: A
3434
// TOP_LEVEL_0_ALL-NEXT: B
35+
// TOP_LEVEL_0_ALL-NEXT: C
36+
// TOP_LEVEL_0_ALL-NEXT: D
3537
// TOP_LEVEL_0_ALL-NEXT: test
3638

3739
// TOP_LEVEL_0_3: let

test/SourceKit/CodeComplete/complete_sort_order.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func test6() {
190190
// VOID_1_RAW-NEXT: key.sourcetext: "foo1()",
191191
// VOID_1_RAW-NEXT: key.description: "foo1()",
192192
// VOID_1_RAW-NEXT: key.typename: "Void",
193-
// VOID_1_RAW-NEXT: key.context: source.codecompletion.context.thismodule,
193+
// VOID_1_RAW-NEXT: key.context: source.codecompletion.context.local,
194194
// VOID_1_RAW-NEXT: key.num_bytes_to_erase: 0,
195195
// VOID_1_RAW-NEXT: key.not_recommended: 1,
196196

@@ -205,8 +205,8 @@ func test7() {
205205
#^CASE_0,caseSensitiveCheck,CaseSensitiveCheck^#
206206
}
207207
// CASE_0: Results for filterText: caseSensitiveCheck [
208-
// CASE_0: caseSensitiveCheck
209208
// CASE_0: CaseSensitiveCheck
209+
// CASE_0: caseSensitiveCheck
210210
// CASE_0: caseSensitiveCheck.
211211
// CASE_0: ]
212212
// CASE_0: Results for filterText: CaseSensitiveCheck [

tools/SourceKit/tools/complete-test/complete-test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ removeCodeCompletionTokens(StringRef Input, StringRef TokenName,
392392
if (match[1].str() != TokenName)
393393
continue;
394394
*CompletionOffset = CleanFile.size();
395-
CleanFile.push_back('\0');
396395
if (match.size() == 2 || !match[2].matched)
397396
continue;
398397

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,10 @@ static int doTypeContextInfo(const CompilerInvocation &InitInvok,
745745
// they are somewhat heavy operations and are not needed for completions.
746746
Invocation.getFrontendOptions().IgnoreSwiftSourceInfo = true;
747747

748+
// Disable to build syntax tree because code-completion skips some portion of
749+
// source text. That breaks an invariant of syntax tree building.
750+
Invocation.getLangOptions().BuildSyntaxTree = false;
751+
748752
Invocation.setCodeCompletionPoint(CleanFile.get(), Offset);
749753

750754
// Create a CodeCompletionConsumer.
@@ -810,6 +814,10 @@ doConformingMethodList(const CompilerInvocation &InitInvok,
810814
// they are somewhat heavy operations and are not needed for completions.
811815
Invocation.getFrontendOptions().IgnoreSwiftSourceInfo = true;
812816

817+
// Disable to build syntax tree because code-completion skips some portion of
818+
// source text. That breaks an invariant of syntax tree building.
819+
Invocation.getLangOptions().BuildSyntaxTree = false;
820+
813821
Invocation.setCodeCompletionPoint(CleanFile.get(), Offset);
814822

815823
SmallVector<const char *, 4> typeNames;

0 commit comments

Comments
 (0)