summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVlad Tsyrklevich <vlad@tsyrklevich.net>2017-08-18 23:21:11 +0000
committerVlad Tsyrklevich <vlad@tsyrklevich.net>2017-08-18 23:21:11 +0000
commiteac4c13ac9ea8f12bc049e040c7b9c8a517f54e7 (patch)
tree8a05f2a0726fd29aba0e7c5b9b9d112b5db00d49
parent0c78c5729f29315d7945988efd048c0cb86c07ce (diff)
Revert "[clang-diff] Move the JSON export function to clang-diff"
This reverts commit r311199, it was causing widespread build failures. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311211 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Tooling/ASTDiff/ASTDiff.h13
-rw-r--r--lib/Tooling/ASTDiff/ASTDiff.cpp54
-rw-r--r--test/Tooling/clang-diff-json.cpp27
-rw-r--r--test/lit.cfg1
-rw-r--r--test/lit.site.cfg.in1
-rw-r--r--tools/clang-diff/ClangDiff.cpp65
6 files changed, 31 insertions, 130 deletions
diff --git a/include/clang/Tooling/ASTDiff/ASTDiff.h b/include/clang/Tooling/ASTDiff/ASTDiff.h
index eb5a14d288..8ceb2f44d3 100644
--- a/include/clang/Tooling/ASTDiff/ASTDiff.h
+++ b/include/clang/Tooling/ASTDiff/ASTDiff.h
@@ -57,8 +57,8 @@ struct Node {
ast_type_traits::DynTypedNode ASTNode;
SmallVector<NodeId, 4> Children;
- ast_type_traits::ASTNodeKind getType() const;
- StringRef getTypeLabel() const;
+ ast_type_traits::ASTNodeKind getType() const { return ASTNode.getNodeKind(); }
+ const StringRef getTypeLabel() const { return getType().asStringRef(); }
bool isLeaf() const { return Children.empty(); }
};
@@ -96,20 +96,15 @@ public:
SyntaxTree(SyntaxTree &&Other) = default;
~SyntaxTree();
- const ASTContext &getASTContext() const;
- StringRef getFilename() const;
-
const Node &getNode(NodeId Id) const;
- NodeId getRootId() const;
-
- // Returns the starting and ending offset of the node in its source file.
- std::pair<unsigned, unsigned> getSourceRangeOffsets(const Node &N) const;
/// Serialize the node attributes to a string representation. This should
/// uniquely distinguish nodes of the same kind. Note that this function just
/// returns a representation of the node value, not considering descendants.
std::string getNodeValue(const DynTypedNode &DTN) const;
+ void printAsJson(raw_ostream &OS);
+
class Impl;
std::unique_ptr<Impl> TreeImpl;
};
diff --git a/lib/Tooling/ASTDiff/ASTDiff.cpp b/lib/Tooling/ASTDiff/ASTDiff.cpp
index 0441dbc78d..3c8d0024dd 100644
--- a/lib/Tooling/ASTDiff/ASTDiff.cpp
+++ b/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<Node> 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<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};
-}
+void SyntaxTree::printAsJson(raw_ostream &OS) { TreeImpl->printAsJsonImpl(OS); }
std::string SyntaxTree::getNodeValue(const DynTypedNode &DTN) const {
return TreeImpl->getNodeValue(DTN);
diff --git a/test/Tooling/clang-diff-json.cpp b/test/Tooling/clang-diff-json.cpp
deleted file mode 100644
index ee67a6d1f3..0000000000
--- a/test/Tooling/clang-diff-json.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-// RUN: clang-diff -ast-dump %s -- \
-// RUN: | %python -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
-// RUN: | FileCheck %s
-
-// CHECK: "begin": 294,
-// CHECK: "type": "CXXRecordDecl",
-// CHECK: "type": "FieldDecl",
-// CHECK: "end": 314,
-class A {
- int x;
-};
-
-// CHECK: "children": [
-// CHECK-NEXT: {
-// CHECK-NEXT: "begin":
-// CHECK-NEXT: "children": []
-// CHECK-NEXT: "end":
-// CHECK-NEXT: "id":
-// CHECK-NEXT: "type": "CharacterLiteral"
-// CHECK-NEXT: }
-// CHECK: ]
-// CHECK: "type": "VarDecl",
-char nl = '\n';
-
-// CHECK: "value": "abc \n\t\u0000\u001f\u0123 \ub370\ubc15"
-char s[] = "abc \n\t\0\x1f\u0123 데박";
-
diff --git a/test/lit.cfg b/test/lit.cfg
index d87d803456..f2bcf99e5a 100644
--- a/test/lit.cfg
+++ b/test/lit.cfg
@@ -276,7 +276,6 @@ config.substitutions.append( ('%test_debuginfo', ' ' + config.llvm_src_root + '/
config.substitutions.append( ('%itanium_abi_triple', makeItaniumABITriple(config.target_triple)) )
config.substitutions.append( ('%ms_abi_triple', makeMSABITriple(config.target_triple)) )
config.substitutions.append( ('%resource_dir', getClangBuiltinIncludeDir(config.clang)) )
-config.substitutions.append( ('%python', config.python_executable) )
# The host triple might not be set, at least if we're compiling clang from
# an already installed llvm.
diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in
index 10186fddf8..532ede8424 100644
--- a/test/lit.site.cfg.in
+++ b/test/lit.site.cfg.in
@@ -25,7 +25,6 @@ config.enable_shared = @ENABLE_SHARED@
config.enable_backtrace = @ENABLE_BACKTRACES@
config.host_arch = "@HOST_ARCH@"
config.enable_abi_breaking_checks = "@LLVM_ENABLE_ABI_BREAKING_CHECKS@"
-config.python_executable = "@PYTHON_EXECUTABLE@"
# Support substitution of the tools and libs dirs with user parameters. This is
# used when we can't determine the tool dir at configuration time.
diff --git a/tools/clang-diff/ClangDiff.cpp b/tools/clang-diff/ClangDiff.cpp
index aec2b0f9b3..ea72183688 100644
--- a/tools/clang-diff/ClangDiff.cpp
+++ b/tools/clang-diff/ClangDiff.cpp
@@ -94,65 +94,6 @@ getAST(const std::unique_ptr<CompilationDatabase> &CommonCompilations,
return std::move(ASTs[0]);
}
-static char hexdigit(int N) { return N &= 0xf, N + (N < 10 ? '0' : 'a' - 10); }
-
-static void printJsonString(raw_ostream &OS, const StringRef Str) {
- for (char C : Str) {
- switch (C) {
- case '"':
- OS << R"(\")";
- break;
- case '\\':
- OS << R"(\\)";
- break;
- case '\n':
- OS << R"(\n)";
- break;
- case '\t':
- OS << R"(\t)";
- break;
- default:
- if ('\x00' <= C && C <= '\x1f') {
- OS << R"(\u00)" << hexdigit(C >> 4) << hexdigit(C);
- } else {
- OS << C;
- }
- }
- }
-}
-
-static void printNodeAttributes(raw_ostream &OS, diff::SyntaxTree &Tree,
- diff::NodeId Id) {
- const diff::Node &N = Tree.getNode(Id);
- OS << R"("id":)" << int(Id);
- OS << R"(,"type":")" << N.getTypeLabel() << '"';
- auto Offsets = Tree.getSourceRangeOffsets(N);
- OS << R"(,"begin":)" << Offsets.first;
- OS << R"(,"end":)" << Offsets.second;
- std::string Value = Tree.getNodeValue(N.ASTNode);
- if (!Value.empty()) {
- OS << R"(,"value":")";
- printJsonString(OS, Value);
- OS << '"';
- }
-}
-
-static void printNodeAsJson(raw_ostream &OS, diff::SyntaxTree &Tree,
- diff::NodeId Id) {
- const diff::Node &N = Tree.getNode(Id);
- OS << "{";
- printNodeAttributes(OS, Tree, Id);
- OS << R"(,"children":[)";
- if (N.Children.size() > 0) {
- printNodeAsJson(OS, Tree, N.Children[0]);
- for (size_t I = 1, E = N.Children.size(); I < E; ++I) {
- OS << ",";
- printNodeAsJson(OS, Tree, N.Children[I]);
- }
- }
- OS << "]}";
-}
-
int main(int argc, const char **argv) {
std::string ErrorMessage;
std::unique_ptr<CompilationDatabase> CommonCompilations =
@@ -176,11 +117,7 @@ int main(int argc, const char **argv) {
if (!AST)
return 1;
diff::SyntaxTree Tree(AST->getASTContext());
- llvm::outs() << R"({"filename":")";
- printJsonString(llvm::outs(), SourcePath);
- llvm::outs() << R"(","root":)";
- printNodeAsJson(llvm::outs(), Tree, Tree.getRootId());
- llvm::outs() << "}\n";
+ Tree.printAsJson(llvm::outs());
return 0;
}