[clang-tools-extra] 8b04c33 - [pseudo] Forest dump ascii art isn't broken by large indices

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 30 07:54:56 PDT 2022


Author: Sam McCall
Date: 2022-06-30T16:53:51+02:00
New Revision: 8b04c331b51811fc6ddcfc8207b1ccdcea02108e

URL: https://github.com/llvm/llvm-project/commit/8b04c331b51811fc6ddcfc8207b1ccdcea02108e
DIFF: https://github.com/llvm/llvm-project/commit/8b04c331b51811fc6ddcfc8207b1ccdcea02108e.diff

LOG: [pseudo] Forest dump ascii art isn't broken by large indices

Added: 
    

Modified: 
    clang-tools-extra/pseudo/lib/Forest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/pseudo/lib/Forest.cpp b/clang-tools-extra/pseudo/lib/Forest.cpp
index d9086c8dea5cd..02818547761c8 100644
--- a/clang-tools-extra/pseudo/lib/Forest.cpp
+++ b/clang-tools-extra/pseudo/lib/Forest.cpp
@@ -33,10 +33,13 @@ std::string ForestNode::dump(const Grammar &G) const {
 
 std::string ForestNode::dumpRecursive(const Grammar &G,
                                       bool Abbreviated) const {
+  using llvm::formatv;
+  Token::Index MaxToken = 0;
   // Count visits of nodes so we can mark those seen multiple times.
   llvm::DenseMap<const ForestNode *, /*VisitCount*/ unsigned> VisitCounts;
   std::function<void(const ForestNode *)> CountVisits =
       [&](const ForestNode *P) {
+        MaxToken = std::max(MaxToken, P->startTokenIndex());
         if (VisitCounts[P]++ > 0)
           return; // Don't count children as multiply visited.
         if (P->kind() == Ambiguous)
@@ -46,6 +49,10 @@ std::string ForestNode::dumpRecursive(const Grammar &G,
       };
   CountVisits(this);
 
+  unsigned IndexWidth = std::max(3, (int)std::to_string(MaxToken).size());
+  // e.g. "[{0,4}, {1,4})" if MaxToken is 5742.
+  std::string RangeFormat = formatv("[{{0,{0}}, {{1,{0}}) ", IndexWidth);
+
   // The box-drawing characters that should be added as a child is rendered.
   struct LineDecoration {
     std::string Prefix;         // Prepended to every line.
@@ -93,9 +100,9 @@ std::string ForestNode::dumpRecursive(const Grammar &G,
         }
 
         if (End == KEnd)
-          Result += llvm::formatv("[{0,3}, end) ", P->startTokenIndex());
+          Result += formatv(RangeFormat.c_str(), P->startTokenIndex(), "end");
         else
-          Result += llvm::formatv("[{0,3}, {1,3}) ", P->startTokenIndex(), End);
+          Result += formatv(RangeFormat.c_str(), P->startTokenIndex(), End);
         Result += LineDec.Prefix;
         Result += LineDec.First;
         if (ElidedParent) {
@@ -110,9 +117,9 @@ std::string ForestNode::dumpRecursive(const Grammar &G,
 
           // The first time, print as #1. Later, =#1.
           if (First) {
-            Result += llvm::formatv("{0} #{1}", P->dump(G), ID);
+            Result += formatv("{0} #{1}", P->dump(G), ID);
           } else {
-            Result += llvm::formatv("{0} =#{1}", G.symbolName(P->symbol()), ID);
+            Result += formatv("{0} =#{1}", G.symbolName(P->symbol()), ID);
             Children = {}; // Don't walk the children again.
           }
         } else {


        


More information about the cfe-commits mailing list