| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 5 #ifndef TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
| 6 #define TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 6 #define TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
| 7 | 7 |
| 8 #include "llvm/Support/raw_ostream.h" | 8 #include "llvm/Support/raw_ostream.h" |
| 9 | 9 |
| 10 // TODO(hans): Remove this #ifdef after Clang is rolled past r234897. | |
| 11 #ifdef LLVM_FORCE_HEAD_REVISION | |
| 12 #define JSON_WRITER_STREAM std::unique_ptr<llvm::raw_ostream> | |
| 13 #else | |
| 14 #define JSON_WRITER_STREAM llvm::raw_fd_ostream* | |
| 15 #endif | |
| 16 | |
| 17 // Helper to write information for the points-to graph. | 10 // Helper to write information for the points-to graph. |
| 18 class JsonWriter { | 11 class JsonWriter { |
| 19 public: | 12 public: |
| 20 static JsonWriter* from(JSON_WRITER_STREAM os) { | 13 static JsonWriter* from(std::unique_ptr<llvm::raw_ostream> os) { |
| 21 return os ? new JsonWriter(std::move(os)) : 0; | 14 return os ? new JsonWriter(std::move(os)) : 0; |
| 22 } | 15 } |
| 23 #ifndef LLVM_FORCE_HEAD_REVISION | |
| 24 ~JsonWriter() { | |
| 25 delete os_; | |
| 26 } | |
| 27 #endif | |
| 28 void OpenList() { | 16 void OpenList() { |
| 29 Separator(); | 17 Separator(); |
| 30 *os_ << "["; | 18 *os_ << "["; |
| 31 state_.push(false); | 19 state_.push(false); |
| 32 } | 20 } |
| 33 void OpenList(const std::string key) { | 21 void OpenList(const std::string key) { |
| 34 Write(key); | 22 Write(key); |
| 35 *os_ << ":"; | 23 *os_ << ":"; |
| 36 OpenList(); | 24 OpenList(); |
| 37 } | 25 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 } | 46 } |
| 59 void Write(const std::string key, const size_t val) { | 47 void Write(const std::string key, const size_t val) { |
| 60 Separator(); | 48 Separator(); |
| 61 *os_ << "\"" << key << "\":" << val; | 49 *os_ << "\"" << key << "\":" << val; |
| 62 } | 50 } |
| 63 void Write(const std::string key, const std::string val) { | 51 void Write(const std::string key, const std::string val) { |
| 64 Separator(); | 52 Separator(); |
| 65 *os_ << "\"" << key << "\":\"" << val << "\""; | 53 *os_ << "\"" << key << "\":\"" << val << "\""; |
| 66 } | 54 } |
| 67 private: | 55 private: |
| 68 JsonWriter(JSON_WRITER_STREAM os) : os_(std::move(os)) {} | 56 JsonWriter(std::unique_ptr<llvm::raw_ostream> os) : os_(std::move(os)) {} |
| 69 void Separator() { | 57 void Separator() { |
| 70 if (state_.empty()) | 58 if (state_.empty()) |
| 71 return; | 59 return; |
| 72 if (state_.top()) { | 60 if (state_.top()) { |
| 73 *os_ << ","; | 61 *os_ << ","; |
| 74 return; | 62 return; |
| 75 } | 63 } |
| 76 state_.top() = true; | 64 state_.top() = true; |
| 77 } | 65 } |
| 78 JSON_WRITER_STREAM os_; | 66 std::unique_ptr<llvm::raw_ostream> os_; |
| 79 std::stack<bool> state_; | 67 std::stack<bool> state_; |
| 80 }; | 68 }; |
| 81 | 69 |
| 82 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 70 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
| OLD | NEW |