[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-11-19 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet added inline comments.



Comment at: clang-tools-extra/clangd/unittests/DumpASTTests.cpp:12
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"

sammccall wrote:
> adamcz wrote:
> > include order lint warning here
> Not sure what's going on here, this does seem to be the order that current 
> clang-format wants. Maybe this clang-tidy is an older version?
Has been fixed with 
https://github.com/llvm/llvm-project/commit/5a9f3867046c4e1c97760e22a505f4d1d788417e


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-11-19 Thread Sam McCall via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.
Closed by commit rG8adc4d1ec764: [clangd] Add textDocument/ast extension method 
to dump the AST (authored by sammccall).

Changed prior to commit:
  https://reviews.llvm.org/D89571?vs=301364=306551#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/DumpAST.cpp
  clang-tools-extra/clangd/DumpAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/test/ast.test
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/DumpASTTests.cpp

Index: clang-tools-extra/clangd/unittests/DumpASTTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/DumpASTTests.cpp
@@ -0,0 +1,151 @@
+//===-- DumpASTTests.cpp --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Annotations.h"
+#include "DumpAST.h"
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+using testing::SizeIs;
+
+TEST(DumpASTTests, BasicInfo) {
+  std::pair Cases[] = {
+  {R"cpp(
+float root(int *x) {
+  return *x + 1;
+}
+  )cpp",
+   R"(
+declaration: Function - root
+  type: FunctionProto
+type: Builtin - float
+declaration: ParmVar - x
+  type: Pointer
+type: Builtin - int
+  statement: Compound
+statement: Return
+  expression: ImplicitCast - IntegralToFloating
+expression: BinaryOperator - +
+  expression: ImplicitCast - LValueToRValue
+expression: UnaryOperator - *
+  expression: ImplicitCast - LValueToRValue
+expression: DeclRef - x
+  expression: IntegerLiteral - 1
+  )"},
+  {R"cpp(
+namespace root {
+struct S { static const int x = 0; };
+int y = S::x + root::S().x;
+}
+  )cpp",
+   R"(
+declaration: Namespace - root
+  declaration: CXXRecord - S
+declaration: Var - x
+  type: Qualified - const
+type: Builtin - int
+  expression: IntegerLiteral - 0
+declaration: CXXConstructor
+declaration: CXXConstructor
+declaration: CXXConstructor
+declaration: CXXDestructor
+  declaration: Var - y
+type: Builtin - int
+expression: ExprWithCleanups
+  expression: BinaryOperator - +
+expression: ImplicitCast - LValueToRValue
+  expression: DeclRef - x
+specifier: TypeSpec
+  type: Record - S
+expression: ImplicitCast - LValueToRValue
+  expression: Member - x
+expression: MaterializeTemporary - rvalue
+  expression: CXXTemporaryObject - S
+type: Elaborated
+  specifier: Namespace - root::
+  type: Record - S
+  )"},
+  {R"cpp(
+template  int root() {
+  (void)root();
+  return T::value;
+}
+  )cpp",
+   R"(
+declaration: FunctionTemplate - root
+  declaration: TemplateTypeParm - T
+  declaration: Function - root
+type: FunctionProto
+  type: Builtin - int
+statement: Compound
+  expression: CStyleCast - ToVoid
+type: Builtin - void
+expression: Call
+  expression: ImplicitCast - FunctionToPointerDecay
+expression: DeclRef - root
+  template argument: Type
+type: Builtin - unsigned int
+  statement: Return
+expression: DependentScopeDeclRef - value
+  specifier: TypeSpec
+type: TemplateTypeParm - T
+  )"},
+  {R"cpp(
+struct Foo { char operator+(int); };
+char root = Foo() + 42;
+  )cpp",
+   R"(
+declaration: Var - root
+  type: Builtin - char
+  expression: ExprWithCleanups
+expression: CXXOperatorCall
+  expression: ImplicitCast - FunctionToPointerDecay
+expression: DeclRef - operator+
+  expression: MaterializeTemporary - lvalue
+expression: CXXTemporaryObject - Foo
+  type: Record - Foo
+  expression: IntegerLiteral - 42
+  )"},
+  };
+  for (const auto  : Cases) {
+ParsedAST AST = TestTU::withCode(Case.first).build();
+auto Node = 

[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-11-19 Thread Sam McCall via Phabricator via cfe-commits
sammccall marked 3 inline comments as done.
sammccall added inline comments.



Comment at: clang-tools-extra/clangd/unittests/DumpASTTests.cpp:12
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"

adamcz wrote:
> include order lint warning here
Not sure what's going on here, this does seem to be the order that current 
clang-format wants. Maybe this clang-tidy is an older version?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-29 Thread Adam Czachorowski via Phabricator via cfe-commits
adamcz accepted this revision.
adamcz added inline comments.
This revision is now accepted and ready to land.



Comment at: clang-tools-extra/clangd/ClangdServer.cpp:799
+  return CB(Offset.takeError());
+if (auto O = positionToOffset(Inputs->Inputs.Contents, R.end))
+  End = *O;

Also here please ;)



Comment at: clang-tools-extra/clangd/ClangdServer.h:323
+  /// Describe the AST subtree for a piece of code.
+  void getAST(PathRef File, Range, Callback>);
+

sammccall wrote:
> adamcz wrote:
> > Any reason not to name arguments here?
> I generally prefer not to name them in the declaration when it doesn't 
> communicate anything beyond the type.
> 
> Done here as most of the other functions name them.
> 
> (I'm curious, do you think there are useful names here, or is the 
> irregularity between named/unnamed bothersome?)
Unnamed argument makes me think that it's ignored by this function. It's 
in-line with Google style guide, maybe doesn't necessary apply here.



Comment at: clang-tools-extra/clangd/DumpAST.cpp:209
+  std::string getDetail(const Decl *D) {
+auto *ND = dyn_cast(D);
+if (!ND || llvm::isa_and_nonnull(ND->getAsFunction()) 
||

lint says this can be const



Comment at: clang-tools-extra/clangd/unittests/DumpASTTests.cpp:12
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"

include order lint warning here



Comment at: clang-tools-extra/clangd/unittests/DumpASTTests.cpp:104
+  };
+  for (const auto  : Cases) {
+ParsedAST AST = TestTU::withCode(Case.first).build();

sammccall wrote:
> adamcz wrote:
> > Can you add a test case for class with overloaded operator(s)? They tend to 
> > cause issues here and there.
> Added one with use of an overloaded operator (LMK if you wanted to dump the 
> declaration instead)
Thanks, that's exactly what I wanted.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-28 Thread Sam McCall via Phabricator via cfe-commits
sammccall updated this revision to Diff 301364.
sammccall marked 2 inline comments as done.
sammccall added a comment.

Address comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/DumpAST.cpp
  clang-tools-extra/clangd/DumpAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/test/ast.test
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/DumpASTTests.cpp

Index: clang-tools-extra/clangd/unittests/DumpASTTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/DumpASTTests.cpp
@@ -0,0 +1,151 @@
+//===-- DumpASTTests.cpp --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Annotations.h"
+#include "DumpAST.h"
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+using testing::SizeIs;
+
+TEST(DumpASTTests, BasicInfo) {
+  std::pair Cases[] = {
+  {R"cpp(
+float root(int *x) {
+  return *x + 1;
+}
+  )cpp",
+   R"(
+declaration: Function - root
+  type: FunctionProto
+type: Builtin - float
+declaration: ParmVar - x
+  type: Pointer
+type: Builtin - int
+  statement: Compound
+statement: Return
+  expression: ImplicitCast - IntegralToFloating
+expression: BinaryOperator - +
+  expression: ImplicitCast - LValueToRValue
+expression: UnaryOperator - *
+  expression: ImplicitCast - LValueToRValue
+expression: DeclRef - x
+  expression: IntegerLiteral - 1
+  )"},
+  {R"cpp(
+namespace root {
+struct S { static const int x = 0; };
+int y = S::x + root::S().x;
+}
+  )cpp",
+   R"(
+declaration: Namespace - root
+  declaration: CXXRecord - S
+declaration: Var - x
+  type: Qualified - const
+type: Builtin - int
+  expression: IntegerLiteral - 0
+declaration: CXXConstructor
+declaration: CXXConstructor
+declaration: CXXConstructor
+declaration: CXXDestructor
+  declaration: Var - y
+type: Builtin - int
+expression: ExprWithCleanups
+  expression: BinaryOperator - +
+expression: ImplicitCast - LValueToRValue
+  expression: DeclRef - x
+specifier: TypeSpec
+  type: Record - S
+expression: ImplicitCast - LValueToRValue
+  expression: Member - x
+expression: MaterializeTemporary - rvalue
+  expression: CXXTemporaryObject - S
+type: Elaborated
+  specifier: Namespace - root::
+  type: Record - S
+  )"},
+  {R"cpp(
+template  int root() {
+  (void)root();
+  return T::value;
+}
+  )cpp",
+   R"(
+declaration: FunctionTemplate - root
+  declaration: TemplateTypeParm - T
+  declaration: Function - root
+type: FunctionProto
+  type: Builtin - int
+statement: Compound
+  expression: CStyleCast - ToVoid
+type: Builtin - void
+expression: Call
+  expression: ImplicitCast - FunctionToPointerDecay
+expression: DeclRef - root
+  template argument: Type
+type: Builtin - unsigned int
+  statement: Return
+expression: DependentScopeDeclRef - value
+  specifier: TypeSpec
+type: TemplateTypeParm - T
+  )"},
+  {R"cpp(
+struct Foo { char operator+(int); };
+char root = Foo() + 42;
+  )cpp",
+   R"(
+declaration: Var - root
+  type: Builtin - char
+  expression: ExprWithCleanups
+expression: CXXOperatorCall
+  expression: ImplicitCast - FunctionToPointerDecay
+expression: DeclRef - operator+
+  expression: MaterializeTemporary - lvalue
+expression: CXXTemporaryObject - Foo
+  type: Record - Foo
+  expression: IntegerLiteral - 42
+  )"},
+  };
+  for (const auto  : Cases) {
+ParsedAST AST = TestTU::withCode(Case.first).build();
+auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "root")),
+AST.getTokens(), AST.getASTContext());
+EXPECT_EQ(llvm::StringRef(Case.second).trim(),
+  llvm::StringRef(llvm::to_string(Node)).trim());
+  }
+}

[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-28 Thread Sam McCall via Phabricator via cfe-commits
sammccall marked 4 inline comments as done.
sammccall added inline comments.



Comment at: clang-tools-extra/clangd/ClangdServer.h:323
+  /// Describe the AST subtree for a piece of code.
+  void getAST(PathRef File, Range, Callback>);
+

adamcz wrote:
> Any reason not to name arguments here?
I generally prefer not to name them in the declaration when it doesn't 
communicate anything beyond the type.

Done here as most of the other functions name them.

(I'm curious, do you think there are useful names here, or is the irregularity 
between named/unnamed bothersome?)



Comment at: clang-tools-extra/clangd/DumpAST.cpp:241
+  return MTE->isBoundToLvalueReference() ? "lvalue" : "rvalue";
+return "";
+  }

adamcz wrote:
> Maybe instead of "" the fallback should be to just return the code in the 
> getSourceRange() range?
This goes against a couple of related goals I have here:
 - the detail should be short enough to quickly scan, roughly it should be one 
name (a class name is OK, but an arbitrary function signature isn't)
 - the detail should relate to this *node*, not the whole subtree



Comment at: clang-tools-extra/clangd/Protocol.h:1583
+  /// The text document.
+  TextDocumentIdentifier textDocument;
+

adamcz wrote:
> We should really disable readability-identifier-naming check on this file, 
> it's complaining on every single identifier.
Agree - but I don't actually know how to do this without turning it off for the 
whole directory. (We could do that...)



Comment at: clang-tools-extra/clangd/unittests/DumpASTTests.cpp:104
+  };
+  for (const auto  : Cases) {
+ParsedAST AST = TestTU::withCode(Case.first).build();

adamcz wrote:
> Can you add a test case for class with overloaded operator(s)? They tend to 
> cause issues here and there.
Added one with use of an overloaded operator (LMK if you wanted to dump the 
declaration instead)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-26 Thread Adam Czachorowski via Phabricator via cfe-commits
adamcz added inline comments.



Comment at: clang-tools-extra/clangd/ClangdServer.cpp:795
+unsigned Start, End;
+if (auto O = positionToOffset(Inputs->Inputs.Contents, R.start))
+  Start = *O;

Please rename O to anything else. *O below looks very much like *0 and it had 
me really confused.



Comment at: clang-tools-extra/clangd/ClangdServer.h:323
+  /// Describe the AST subtree for a piece of code.
+  void getAST(PathRef File, Range, Callback>);
+

Any reason not to name arguments here?



Comment at: clang-tools-extra/clangd/DumpAST.cpp:241
+  return MTE->isBoundToLvalueReference() ? "lvalue" : "rvalue";
+return "";
+  }

Maybe instead of "" the fallback should be to just return the code in the 
getSourceRange() range?



Comment at: clang-tools-extra/clangd/DumpAST.cpp:393
+  // DynTypedNode only works with const, RecursiveASTVisitor only non-const :-(
+  if (auto *D = N.get())
+V.TraverseDecl(const_cast(D));

Lots of lint warnings here about "const". Please fix.



Comment at: clang-tools-extra/clangd/Protocol.h:1583
+  /// The text document.
+  TextDocumentIdentifier textDocument;
+

We should really disable readability-identifier-naming check on this file, it's 
complaining on every single identifier.



Comment at: clang-tools-extra/clangd/unittests/DumpASTTests.cpp:104
+  };
+  for (const auto  : Cases) {
+ParsedAST AST = TestTU::withCode(Case.first).build();

Can you add a test case for class with overloaded operator(s)? They tend to 
cause issues here and there.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-19 Thread Sam McCall via Phabricator via cfe-commits
sammccall updated this revision to Diff 298961.
sammccall added a comment.

(for real this time)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/DumpAST.cpp
  clang-tools-extra/clangd/DumpAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/test/ast.test
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/DumpASTTests.cpp

Index: clang-tools-extra/clangd/unittests/DumpASTTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/DumpASTTests.cpp
@@ -0,0 +1,135 @@
+//===-- DumpASTTests.cpp --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Annotations.h"
+#include "DumpAST.h"
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+using testing::SizeIs;
+
+TEST(DumpASTTests, BasicInfo) {
+  std::pair Cases[] = {
+  {R"cpp(
+float root(int *x) {
+  return *x + 1;
+}
+  )cpp",
+   R"(
+declaration: Function - root
+  type: FunctionProto
+type: Builtin - float
+declaration: ParmVar - x
+  type: Pointer
+type: Builtin - int
+  statement: Compound
+statement: Return
+  expression: ImplicitCast - IntegralToFloating
+expression: BinaryOperator - +
+  expression: ImplicitCast - LValueToRValue
+expression: UnaryOperator - *
+  expression: ImplicitCast - LValueToRValue
+expression: DeclRef - x
+  expression: IntegerLiteral - 1
+  )"},
+  {R"cpp(
+namespace root {
+struct S { static const int x = 0; };
+int y = S::x + root::S().x;
+}
+  )cpp",
+   R"(
+declaration: Namespace - root
+  declaration: CXXRecord - S
+declaration: Var - x
+  type: Qualified - const
+type: Builtin - int
+  expression: IntegerLiteral - 0
+declaration: CXXConstructor
+declaration: CXXConstructor
+declaration: CXXConstructor
+declaration: CXXDestructor
+  declaration: Var - y
+type: Builtin - int
+expression: ExprWithCleanups
+  expression: BinaryOperator - +
+expression: ImplicitCast - LValueToRValue
+  expression: DeclRef - x
+specifier: TypeSpec
+  type: Record - S
+expression: ImplicitCast - LValueToRValue
+  expression: Member - x
+expression: MaterializeTemporary - rvalue
+  expression: CXXTemporaryObject - S
+type: Elaborated
+  specifier: Namespace - root::
+  type: Record - S
+  )"},
+  {R"cpp(
+template  int root() {
+  (void)root();
+  return T::value;
+}
+  )cpp",
+   R"(
+declaration: FunctionTemplate - root
+  declaration: TemplateTypeParm - T
+  declaration: Function - root
+type: FunctionProto
+  type: Builtin - int
+statement: Compound
+  expression: CStyleCast - ToVoid
+type: Builtin - void
+expression: Call
+  expression: ImplicitCast - FunctionToPointerDecay
+expression: DeclRef - root
+  template argument: Type
+type: Builtin - unsigned int
+  statement: Return
+expression: DependentScopeDeclRef - value
+  specifier: TypeSpec
+type: TemplateTypeParm - T
+  )"},
+  };
+  for (const auto  : Cases) {
+ParsedAST AST = TestTU::withCode(Case.first).build();
+auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "root")),
+AST.getTokens(), AST.getASTContext());
+EXPECT_EQ(llvm::StringRef(Case.second).trim(),
+  llvm::StringRef(llvm::to_string(Node)).trim());
+  }
+}
+
+TEST(DumpASTTests, Range) {
+  Annotations Case("$var[[$type[[int]] x]];");
+  ParsedAST AST = TestTU::withCode(Case.code()).build();
+  auto Node = dumpAST(DynTypedNode::create(findDecl(AST, "x")), AST.getTokens(),
+  AST.getASTContext());
+  EXPECT_EQ(Node.range, Case.range("var"));
+  ASSERT_THAT(Node.children, SizeIs(1)) << "Expected one child typeloc";
+  EXPECT_EQ(Node.children.front().range, Case.range("type"));
+}
+
+TEST(DumpASTTests, Arcana) {
+  ParsedAST AST = TestTU::withCode("int 

[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-19 Thread Sam McCall via Phabricator via cfe-commits
sammccall updated this revision to Diff 298959.
sammccall added a comment.

Oops, forgot newly added files :-\


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89571/new/

https://reviews.llvm.org/D89571

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/unittests/CMakeLists.txt

Index: clang-tools-extra/clangd/unittests/CMakeLists.txt
===
--- clang-tools-extra/clangd/unittests/CMakeLists.txt
+++ clang-tools-extra/clangd/unittests/CMakeLists.txt
@@ -51,6 +51,7 @@
   DexTests.cpp
   DiagnosticsTests.cpp
   DraftStoreTests.cpp
+  DumpASTTests.cpp
   ExpectedTypeTest.cpp
   FileDistanceTests.cpp
   FileIndexTests.cpp
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -5,6 +5,7 @@
 # CHECK-NEXT:  "jsonrpc": "2.0",
 # CHECK-NEXT:  "result": {
 # CHECK-NEXT:"capabilities": {
+# CHECK-NEXT:  "astProvider": true,
 # CHECK-NEXT:  "codeActionProvider": true,
 # CHECK-NEXT:  "completionProvider": {
 # CHECK-NEXT:"allCommitCharacters": [
Index: clang-tools-extra/clangd/Protocol.h
===
--- clang-tools-extra/clangd/Protocol.h
+++ clang-tools-extra/clangd/Protocol.h
@@ -1576,6 +1576,45 @@
 };
 llvm::json::Value toJSON(const FoldingRange );
 
+/// Payload for textDocument/ast request.
+/// This request is a clangd extension.
+struct ASTParams {
+  /// The text document.
+  TextDocumentIdentifier textDocument;
+
+  /// The position of the node to be dumped.
+  /// The highest-level node that entirely contains the range will be returned.
+  Range range;
+};
+bool fromJSON(const llvm::json::Value &, ASTParams &, llvm::json::Path);
+
+/// Simplified description of a clang AST node.
+/// This is clangd's internal representation of C++ code.
+struct ASTNode {
+  /// The general kind of node, such as "expression"
+  /// Corresponds to the base AST node type such as Expr.
+  std::string role;
+  /// The specific kind of node this is, such as "BinaryOperator".
+  /// This is usually a concrete node class (with Expr etc suffix dropped).
+  /// When there's no hierarchy (e.g. TemplateName), the variant (NameKind).
+  std::string kind;
+  /// Brief additional information, such as "||" for the particular operator.
+  /// The information included depends on the node kind, and may be empty.
+  std::string detail;
+  /// A one-line dump of detailed information about the node.
+  /// This includes role/kind/description information, but is rather cryptic.
+  /// It is similar to the output from `clang -Xclang -ast-dump`.
+  /// May be empty for certain types of nodes.
+  std::string arcana;
+  /// The range of the original source file covered by this node.
+  /// May be missing for implicit nodes, or those created by macro expansion.
+  llvm::Optional range;
+  /// Nodes nested within this one, such as the operands of a BinaryOperator.
+  std::vector children;
+};
+llvm::json::Value toJSON(const ASTNode &);
+llvm::raw_ostream <<(llvm::raw_ostream &, const ASTNode &);
+
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/Protocol.cpp
===
--- clang-tools-extra/clangd/Protocol.cpp
+++ clang-tools-extra/clangd/Protocol.cpp
@@ -1300,5 +1300,41 @@
   return Result;
 }
 
+bool fromJSON(const llvm::json::Value , ASTParams ,
+  llvm::json::Path P) {
+  llvm::json::ObjectMapper O(Params, P);
+  return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
+}
+
+llvm::json::Value toJSON(const ASTNode ) {
+  llvm::json::Object Result{
+  {"role", N.role},
+  {"kind", N.kind},
+  };
+  if (!N.children.empty())
+Result["children"] = N.children;
+  if (!N.detail.empty())
+Result["detail"] = N.detail;
+  if (!N.arcana.empty())
+Result["arcana"] = N.arcana;
+  if (N.range)
+Result["range"] = *N.range;
+  return Result;
+}
+
+llvm::raw_ostream <<(llvm::raw_ostream , const ASTNode ) {
+  std::function Print = [&](const ASTNode ,
+ unsigned Level) {
+OS.indent(2 * Level) << N.role << ": " << N.kind;
+if (!N.detail.empty())
+  OS << " - " << N.detail;
+OS << "\n";
+for (const ASTNode  : N.children)
+  Print(C, Level + 1);
+  };
+  Print(Root, 0);
+  return OS;
+}
+
 } // namespace clangd
 } // namespace clang
Index: 

[PATCH] D89571: [clangd] Add textDocument/ast extension method to dump the AST

2020-10-16 Thread Sam McCall via Phabricator via cfe-commits
sammccall created this revision.
sammccall added a reviewer: adamcz.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, mgorny.
Herald added a project: clang.
sammccall requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

This is a mass-market version of the "dump AST" tweak we have behind
-hidden-features.
I think in this friendlier form it'll be useful for people outside clang
developers, which would justify making it a real feature.
It could be useful as a step towards lightweight clang-AST tooling in clangd
itself (like matcher-based search).

Advantages over the tweak:

- simplified information makes it more accessible, likely somewhat useful 
without learning too much clang internals
- can be shown in a tree view
- structured information gives some options for presentation (e.g. icon + two 
text colors + tooltip in vscode)
- clickable nodes jump to the corresponding code

Disadvantages:

- a bunch of code to handle different node types
- likely missing some important info vs dump-ast due to brevity/oversight
- may end up chasing/maintaining support for the long tail of nodes

Demo with VSCode support: https://imgur.com/a/6gKfyIV


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89571

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/unittests/CMakeLists.txt

Index: clang-tools-extra/clangd/unittests/CMakeLists.txt
===
--- clang-tools-extra/clangd/unittests/CMakeLists.txt
+++ clang-tools-extra/clangd/unittests/CMakeLists.txt
@@ -51,6 +51,7 @@
   DexTests.cpp
   DiagnosticsTests.cpp
   DraftStoreTests.cpp
+  DumpASTTests.cpp
   ExpectedTypeTest.cpp
   FileDistanceTests.cpp
   FileIndexTests.cpp
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -5,6 +5,7 @@
 # CHECK-NEXT:  "jsonrpc": "2.0",
 # CHECK-NEXT:  "result": {
 # CHECK-NEXT:"capabilities": {
+# CHECK-NEXT:  "astProvider": true,
 # CHECK-NEXT:  "codeActionProvider": true,
 # CHECK-NEXT:  "completionProvider": {
 # CHECK-NEXT:"allCommitCharacters": [
Index: clang-tools-extra/clangd/Protocol.h
===
--- clang-tools-extra/clangd/Protocol.h
+++ clang-tools-extra/clangd/Protocol.h
@@ -1576,6 +1576,45 @@
 };
 llvm::json::Value toJSON(const FoldingRange );
 
+/// Payload for textDocument/ast request.
+/// This request is a clangd extension.
+struct ASTParams {
+  /// The text document.
+  TextDocumentIdentifier textDocument;
+
+  /// The position of the node to be dumped.
+  /// The highest-level node that entirely contains the range will be returned.
+  Range range;
+};
+bool fromJSON(const llvm::json::Value &, ASTParams &, llvm::json::Path);
+
+/// Simplified description of a clang AST node.
+/// This is clangd's internal representation of C++ code.
+struct ASTNode {
+  /// The general kind of node, such as "expression"
+  /// Corresponds to the base AST node type such as Expr.
+  std::string role;
+  /// The specific kind of node this is, such as "BinaryOperator".
+  /// This is usually a concrete node class (with Expr etc suffix dropped).
+  /// When there's no hierarchy (e.g. TemplateName), the variant (NameKind).
+  std::string kind;
+  /// Brief additional information, such as "||" for the particular operator.
+  /// The information included depends on the node kind, and may be empty.
+  std::string detail;
+  /// A one-line dump of detailed information about the node.
+  /// This includes role/kind/description information, but is rather cryptic.
+  /// It is similar to the output from `clang -Xclang -ast-dump`.
+  /// May be empty for certain types of nodes.
+  std::string arcana;
+  /// The range of the original source file covered by this node.
+  /// May be missing for implicit nodes, or those created by macro expansion.
+  llvm::Optional range;
+  /// Nodes nested within this one, such as the operands of a BinaryOperator.
+  std::vector children;
+};
+llvm::json::Value toJSON(const ASTNode &);
+llvm::raw_ostream <<(llvm::raw_ostream &, const ASTNode &);
+
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/Protocol.cpp
===
--- clang-tools-extra/clangd/Protocol.cpp
+++ clang-tools-extra/clangd/Protocol.cpp
@@ -1300,5 +1300,41 @@
   return Result;
 }
 
+bool fromJSON(const llvm::json::Value , ASTParams ,
+