[clang-tools-extra] e3e1583 - [clangd] Tidy up SelectionTree dumps with newlines

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon May 18 16:48:09 PDT 2020


Author: Sam McCall
Date: 2020-05-19T01:48:01+02:00
New Revision: e3e15836af79cbcfe452624caf326b854781abdd

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

LOG: [clangd] Tidy up SelectionTree dumps with newlines

Added: 
    

Modified: 
    clang-tools-extra/clangd/Selection.cpp
    clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index df7e82ccb2d3..1db15ba6699a 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -25,6 +25,7 @@
 #include "clang/Lex/Lexer.h"
 #include "clang/Tooling/Syntax/Tokens.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
@@ -708,6 +709,24 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
 
 } // namespace
 
+llvm::SmallString<256> abbreviatedString(DynTypedNode N,
+                                         const PrintingPolicy &PP) {
+  llvm::SmallString<256> Result;
+  {
+    llvm::raw_svector_ostream OS(Result);
+    N.print(OS, PP);
+  }
+  auto Pos = Result.find('\n');
+  if (Pos != llvm::StringRef::npos) {
+    bool MoreText =
+        !llvm::all_of(llvm::StringRef(Result).drop_front(Pos), llvm::isSpace);
+    Result.resize(Pos);
+    if (MoreText)
+      Result.append(" …");
+  }
+  return Result;
+}
+
 void SelectionTree::print(llvm::raw_ostream &OS, const SelectionTree::Node &N,
                           int Indent) const {
   if (N.Selected)
@@ -716,9 +735,7 @@ void SelectionTree::print(llvm::raw_ostream &OS, const SelectionTree::Node &N,
   else
     OS.indent(Indent);
   printNodeKind(OS, N.ASTNode);
-  OS << ' ';
-  N.ASTNode.print(OS, PrintPolicy);
-  OS << "\n";
+  OS << ' ' << abbreviatedString(N.ASTNode, PrintPolicy) << "\n";
   for (const Node *Child : N.Children)
     print(OS, *Child, Indent + 2);
 }

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index 95f086f77386..b0a941dae5d2 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -173,6 +173,15 @@ TEST_F(ShowSelectionTreeTest, Test) {
         *IntegerLiteral 2
 )";
   EXPECT_EQ(apply("int fcall(int); int x = fca[[ll(2 +]]2);"), Output);
+
+  Output = R"(message:
+ TranslationUnitDecl 
+   FunctionDecl void x()
+     CompoundStmt { …
+       ForStmt for (;;) …
+        *BreakStmt break;
+)";
+  EXPECT_EQ(apply("void x() { for (;;) br^eak; }"), Output);
 }
 
 TWEAK_TEST(DumpRecordLayout);


        


More information about the cfe-commits mailing list