From deb2a2adc87fa69da694d15fe1dfd45b99218529 Mon Sep 17 00:00:00 2001 From: Vlad Tsyrklevich Date: Fri, 18 Aug 2017 23:21:11 +0000 Subject: Revert "[clang-diff] Move the JSON export function to clang-diff" This reverts commit r311199, it was causing widespread build failures. llvm-svn: 311211 --- clang/lib/Tooling/ASTDiff/ASTDiff.cpp | 54 +++++++++++++++++------------------ 1 file changed, 26 insertions(+), 28 deletions(-) (limited to 'clang/lib/Tooling/ASTDiff/ASTDiff.cpp') diff --git a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp index 0441dbc78da..3c8d0024dd6 100644 --- a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp +++ b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp @@ -176,6 +176,9 @@ public: void printTree(NodeId Root) const; void printTree(raw_ostream &OS, NodeId Root) const; + void printAsJsonImpl(raw_ostream &OS) const; + void printNodeAsJson(raw_ostream &OS, NodeId Id) const; + private: /// Nodes in preorder. std::vector Nodes; @@ -435,6 +438,28 @@ void SyntaxTree::Impl::printNode(raw_ostream &OS, NodeId Id) const { OS << "(" << PostorderIds[Id] << ")"; } +void SyntaxTree::Impl::printNodeAsJson(raw_ostream &OS, NodeId Id) const { + auto N = getNode(Id); + OS << R"({"type":")" << N.getTypeLabel() << R"(")"; + if (getNodeValue(Id) != "") + OS << R"(,"value":")" << getNodeValue(Id) << R"(")"; + OS << R"(,"children":[)"; + if (N.Children.size() > 0) { + printNodeAsJson(OS, N.Children[0]); + for (size_t I = 1, E = N.Children.size(); I < E; ++I) { + OS << ","; + printNodeAsJson(OS, N.Children[I]); + } + } + OS << "]}"; +} + +void SyntaxTree::Impl::printAsJsonImpl(raw_ostream &OS) const { + OS << R"({"root":)"; + printNodeAsJson(OS, getRootId()); + OS << "}\n"; +} + /// Identifies a node in a subtree by its postorder offset, starting at 1. struct SNodeId { int Id = 0; @@ -649,12 +674,6 @@ private: } }; -ast_type_traits::ASTNodeKind Node::getType() const { - return ASTNode.getNodeKind(); -} - -StringRef Node::getTypeLabel() const { return getType().asStringRef(); } - namespace { // Compares nodes by their depth. struct HeightLess { @@ -980,28 +999,7 @@ void ASTDiff::printMatch(raw_ostream &OS, const Match &M) const { SyntaxTree::~SyntaxTree() = default; -const ASTContext &SyntaxTree::getASTContext() const { return TreeImpl->AST; } - -const Node &SyntaxTree::getNode(NodeId Id) const { - return TreeImpl->getNode(Id); -} - -NodeId SyntaxTree::getRootId() const { return TreeImpl->getRootId(); } - -std::pair SyntaxTree::getSourceRangeOffsets(const Node &N) const { - const SourceManager &SrcMgr = TreeImpl->AST.getSourceManager(); - SourceRange Range = N.ASTNode.getSourceRange(); - SourceLocation BeginLoc = Range.getBegin(); - SourceLocation EndLoc = Lexer::getLocForEndOfToken( - Range.getEnd(), /*Offset=*/0, SrcMgr, TreeImpl->AST.getLangOpts()); - if (auto *ThisExpr = N.ASTNode.get()) { - if (ThisExpr->isImplicit()) - EndLoc = BeginLoc; - } - unsigned Begin = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(BeginLoc)); - unsigned End = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(EndLoc)); - return {Begin, End}; -} +void SyntaxTree::printAsJson(raw_ostream &OS) { TreeImpl->printAsJsonImpl(OS); } std::string SyntaxTree::getNodeValue(const DynTypedNode &DTN) const { return TreeImpl->getNodeValue(DTN); -- cgit v1.2.3