]> granicus.if.org Git - clang/commitdiff
[parser] If there are unmatched braces in a function definition, try to
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 24 Mar 2012 02:26:51 +0000 (02:26 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 24 Mar 2012 02:26:51 +0000 (02:26 +0000)
recover by returning the statements that we parsed so far, instead of
dropping the whole function body.

rdar://10967343

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153367 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseStmt.cpp
test/Index/unmatched-braces.c [new file with mode: 0644]
test/Index/unmatched-braces.m [new file with mode: 0644]

index 5fe5e6a2b6a53392e629dae3faf67cbd6b4c7416..79b53c3b245faed6cad811fbb6e089d570638e77 100644 (file)
@@ -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);
 }
 
diff --git a/test/Index/unmatched-braces.c b/test/Index/unmatched-braces.c
new file mode 100644 (file)
index 0000000..b994605
--- /dev/null
@@ -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
diff --git a/test/Index/unmatched-braces.m b/test/Index/unmatched-braces.m
new file mode 100644 (file)
index 0000000..84fe82a
--- /dev/null
@@ -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