diff options
| author | Johannes Altmanninger <aclopte@gmail.com> | 2017-08-19 00:57:38 +0000 |
|---|---|---|
| committer | Johannes Altmanninger <aclopte@gmail.com> | 2017-08-19 00:57:38 +0000 |
| commit | 0da12c841adee9718cd75e4ff4ddc37db89f6832 (patch) | |
| tree | 15b0d7f734e3649d28f65b4c82ff3cf8484deab7 /clang/tools/clang-diff | |
| parent | 91d8af53863aec05c28b38b81c69492d6276e98e (diff) | |
| download | bcm5719-llvm-0da12c841adee9718cd75e4ff4ddc37db89f6832.tar.gz bcm5719-llvm-0da12c841adee9718cd75e4ff4ddc37db89f6832.zip | |
Revert "Revert "[clang-diff] Move the JSON export function to clang-diff""
This reverts commit eac4c13ac9ea8f12bc049e040c7b9c8a517f54e7, the
original commit *should* not have caused the build failure.
llvm-svn: 311216
Diffstat (limited to 'clang/tools/clang-diff')
| -rw-r--r-- | clang/tools/clang-diff/ClangDiff.cpp | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/clang/tools/clang-diff/ClangDiff.cpp b/clang/tools/clang-diff/ClangDiff.cpp index ea721836885..aec2b0f9b3a 100644 --- a/clang/tools/clang-diff/ClangDiff.cpp +++ b/clang/tools/clang-diff/ClangDiff.cpp @@ -94,6 +94,65 @@ 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 = @@ -117,7 +176,11 @@ int main(int argc, const char **argv) { if (!AST) return 1; diff::SyntaxTree Tree(AST->getASTContext()); - Tree.printAsJson(llvm::outs()); + llvm::outs() << R"({"filename":")"; + printJsonString(llvm::outs(), SourcePath); + llvm::outs() << R"(","root":)"; + printNodeAsJson(llvm::outs(), Tree, Tree.getRootId()); + llvm::outs() << "}\n"; return 0; } |

