Skip to content

Commit

Permalink
[parser] If there are unmatched braces in a function definition, try to
Browse files Browse the repository at this point in the history
recover by returning the statements that we parsed so far, instead of
dropping the whole function body.

rdar://10967343

llvm-svn: 153367
  • Loading branch information
akyrtzi committed Mar 24, 2012
1 parent 4b9ab74 commit 6db8501
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
13 changes: 8 additions & 5 deletions clang/lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,17 +823,20 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Stmts.push_back(R.release());
}

SourceLocation CloseLoc = Tok.getLocation();

// We broke out of the while loop because we found a '}' or EOF.
if (Tok.isNot(tok::r_brace)) {
Diag(Tok, diag::err_expected_rbrace);
Diag(T.getOpenLocation(), diag::note_matching) << "{";
return StmtError();
// Recover by creating a compound statement with what we parsed so far,
// instead of dropping everything and returning StmtError();
} else {
if (!T.consumeClose())
CloseLoc = T.getCloseLocation();
}

if (T.consumeClose())
return StmtError();

return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
move_arg(Stmts), isStmtExpr);
}

Expand Down
9 changes: 9 additions & 0 deletions clang/test/Index/unmatched-braces.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void foo() {
int x;
if (x) {
}

// RUN: c-index-test -cursor-at=%s:2:7 %s > %t
// RUN: FileCheck %s -input-file %t

// CHECK: VarDecl=x:2:7
11 changes: 11 additions & 0 deletions clang/test/Index/unmatched-braces.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@implementation I
-(void)meth {
int x;
if (x) {
}
@end

// RUN: c-index-test -cursor-at=%s:3:7 %s > %t
// RUN: FileCheck %s -input-file %t

// CHECK: VarDecl=x:3:7

0 comments on commit 6db8501

Please sign in to comment.