[PATCH] D132732: [pseudo] Collect positions in TokenStream that trigger the error recovery.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 26 05:10:07 PDT 2022


hokein created this revision.
Herald added a project: All.
hokein requested review of this revision.
Herald added subscribers: cfe-commits, alextsao1999.
Herald added a project: clang-tools-extra.

Mainly for making it easier to debug broken code. No intend to land it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132732

Files:
  clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
  clang-tools-extra/pseudo/lib/GLR.cpp
  clang-tools-extra/pseudo/tool/ClangPseudo.cpp


Index: clang-tools-extra/pseudo/tool/ClangPseudo.cpp
===================================================================
--- clang-tools-extra/pseudo/tool/ClangPseudo.cpp
+++ clang-tools-extra/pseudo/tool/ClangPseudo.cpp
@@ -153,12 +153,26 @@
           "The start symbol {0} doesn't exit in the grammar!\n", StartSymbol);
       return 2;
     }
-    auto &Root =
-        glrParse(clang::pseudo::ParseParams{*ParseableStream, Arena, GSS},
-                 *StartSymID, Lang);
+    std::vector<Token::Index> FailingPoints;
+    auto &Root = glrParse(clang::pseudo::ParseParams{*ParseableStream, Arena,
+                                                     GSS, &FailingPoints},
+                          *StartSymID, Lang);
     if (PrintForest)
       llvm::outs() << Root.dumpRecursive(Lang.G, /*Abbreviated=*/ForestAbbrev);
-
+    if (!FailingPoints.empty()) {
+      llvm::for_each(FailingPoints, [&](Token::Index Pos) {
+        if (Pos >= ParseableStream->tokens().size())
+          return;
+        const auto &T = ParseableStream->tokens()[Pos];
+        std::string LineCode;
+        llvm::for_each(ParseableStream->tokens(), [&](const Token &E) {
+          if (E.Line == T.Line)
+            LineCode += (" " + E.text().str());
+        });
+        llvm::outs() << llvm::formatv("Error on parsing Line {0}: {1}\n",
+                                      T.Line + 1, LineCode);
+      });
+    }
     if (HTMLForest.getNumOccurrences()) {
       std::error_code EC;
       llvm::raw_fd_ostream HTMLOut(HTMLForest, EC);
Index: clang-tools-extra/pseudo/lib/GLR.cpp
===================================================================
--- clang-tools-extra/pseudo/lib/GLR.cpp
+++ clang-tools-extra/pseudo/lib/GLR.cpp
@@ -646,7 +646,8 @@
                                  "Re-reducing without lookahead.\n");
       Heads.resize(HeadsPartition);
       Reduce(Heads, /*allow all reductions*/ tokenSymbol(tok::unknown));
-
+      if (Params.FailingPoints)
+        Params.FailingPoints->push_back(I);
       glrRecover(Heads, I, Params, Lang, NextHeads);
       assert(!NextHeads.empty() &&  "no fallback recovery strategy!" );
     } else
Index: clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
===================================================================
--- clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
+++ clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
@@ -122,6 +122,9 @@
   // Arena for data structure used by the GLR algorithm.
   ForestArena &Forest;  // Storage for the output forest.
   GSS &GSStack;         // Storage for parsing stacks.
+
+  // If not null, store the token indices that trigger the error-recovery.
+  std::vector<Token::Index> *FailingPoints = nullptr;
 };
 
 // Parses the given token stream as the start symbol with the GLR algorithm,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132732.455874.patch
Type: text/x-patch
Size: 2799 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220826/3717d512/attachment.bin>


More information about the cfe-commits mailing list