[PATCH] D134737: [pseudo][wip] Recovery for statements.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 27 06:53:14 PDT 2022


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a project: All.
hokein requested review of this revision.
Herald added a subscriber: alextsao1999.
Herald added a project: clang-tools-extra.

The main idea is to reuse the existing declaration recovery strategy --
extending it to cover some statement-specific bits.

Base on https://reviews.llvm.org/D130460


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134737

Files:
  clang-tools-extra/pseudo/lib/cxx/CXX.cpp
  clang-tools-extra/pseudo/lib/cxx/Recovery.cpp
  clang-tools-extra/pseudo/lib/cxx/cxx.bnf


Index: clang-tools-extra/pseudo/lib/cxx/cxx.bnf
===================================================================
--- clang-tools-extra/pseudo/lib/cxx/cxx.bnf
+++ clang-tools-extra/pseudo/lib/cxx/cxx.bnf
@@ -283,8 +283,8 @@
 labeled-statement := DEFAULT : statement
 expression-statement := expression_opt ;
 compound-statement := { statement-seq_opt [recover=Brackets] }
-statement-seq := statement
-statement-seq := statement-seq statement
+statement-seq := statement [recover=NextStatement]
+statement-seq := statement-seq statement [recover=NextStatement]
 selection-statement := IF CONSTEXPR_opt ( init-statement_opt condition ) statement [guard]
 selection-statement := IF CONSTEXPR_opt ( init-statement_opt condition ) statement ELSE statement
 selection-statement := SWITCH ( init-statement_opt condition ) statement
Index: clang-tools-extra/pseudo/lib/cxx/Recovery.cpp
===================================================================
--- clang-tools-extra/pseudo/lib/cxx/Recovery.cpp
+++ clang-tools-extra/pseudo/lib/cxx/Recovery.cpp
@@ -31,6 +31,11 @@
   auto ConsiderForBraces = [&](tok::TokenKind K) {
     if (K == tok::kw_export || K == tok::kw_extern || K == tok::kw_namespace)
       AcceptBraces = true;
+    // Statements that don't end in semicolons:
+    //   while/switch/if/for (...) {}
+    if (K == tok::kw_for || K == tok::kw_while || K == tok::kw_if ||
+        K == tok::kw_switch)
+      AcceptBraces = true;
   };
 
   // Some tokens are pretty good indicators of a declaration starting, when
@@ -71,6 +76,15 @@
     case tok::kw_public:
     case tok::kw_private:
     case tok::kw_protected:
+    // Typical keywords for statements
+    case tok::kw_for:
+    case tok::kw_while:
+    case tok::kw_if:
+    case tok::kw_goto:
+    case tok::kw_switch:
+    case tok::kw_return:
+    case tok::kw_break:
+    case tok::kw_continue:
       return true;
     default:
       return false;
Index: clang-tools-extra/pseudo/lib/cxx/CXX.cpp
===================================================================
--- clang-tools-extra/pseudo/lib/cxx/CXX.cpp
+++ clang-tools-extra/pseudo/lib/cxx/CXX.cpp
@@ -427,6 +427,7 @@
   return {
       {Extension::Brackets, recoverBrackets},
       {Extension::NextDeclaration, recoverNextDeclaration},
+      {Extension::NextStatement, recoverNextDeclaration},
   };
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134737.463220.patch
Type: text/x-patch
Size: 2338 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220927/a1bd666f/attachment.bin>


More information about the cfe-commits mailing list