summaryrefslogtreecommitdiffstats
path: root/clang/lib/Tooling/ASTDiff/ASTDiff.cpp
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2017-08-18 21:26:13 +0000
committerJohannes Altmanninger <aclopte@gmail.com>2017-08-18 21:26:13 +0000
commit0ad4cc61ac9063a2943a42059283bf36f883f341 (patch)
tree5668d99e88b42969048d7da7e035b14ef036d736 /clang/lib/Tooling/ASTDiff/ASTDiff.cpp
parentf36cca88fb01d8f41f86e01ceb096693503a32e3 (diff)
downloadbcm5719-llvm-0ad4cc61ac9063a2943a42059283bf36f883f341.tar.gz
bcm5719-llvm-0ad4cc61ac9063a2943a42059283bf36f883f341.zip
[clang-diff] Move the JSON export function to clang-diff
Reviewers: arphaman Subscribers: klimek Differential Revision: https://reviews.llvm.org/D36178 llvm-svn: 311199
Diffstat (limited to 'clang/lib/Tooling/ASTDiff/ASTDiff.cpp')
-rw-r--r--clang/lib/Tooling/ASTDiff/ASTDiff.cpp54
1 files changed, 28 insertions, 26 deletions
diff --git a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp
index 3c8d0024dd6..0441dbc78da 100644
--- a/clang/lib/Tooling/ASTDiff/ASTDiff.cpp
+++ b/clang/lib/Tooling/ASTDiff/ASTDiff.cpp
@@ -176,9 +176,6 @@ 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<Node> Nodes;
@@ -438,28 +435,6 @@ 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;
@@ -674,6 +649,12 @@ 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 {
@@ -999,7 +980,28 @@ void ASTDiff::printMatch(raw_ostream &OS, const Match &M) const {
SyntaxTree::~SyntaxTree() = default;
-void SyntaxTree::printAsJson(raw_ostream &OS) { TreeImpl->printAsJsonImpl(OS); }
+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<unsigned, unsigned> 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<CXXThisExpr>()) {
+ if (ThisExpr->isImplicit())
+ EndLoc = BeginLoc;
+ }
+ unsigned Begin = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(BeginLoc));
+ unsigned End = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(EndLoc));
+ return {Begin, End};
+}
std::string SyntaxTree::getNodeValue(const DynTypedNode &DTN) const {
return TreeImpl->getNodeValue(DTN);
OpenPOWER on IntegriCloud