| 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 |
| 10 // Helper to write information for the points-to graph. | 17 // Helper to write information for the points-to graph. |
| 11 class JsonWriter { | 18 class JsonWriter { |
| 12 public: | 19 public: |
| 13 static JsonWriter* from(llvm::raw_fd_ostream* os) { | 20 static JsonWriter* from(JSON_WRITER_STREAM os) { |
| 14 return os ? new JsonWriter(os) : 0; | 21 return os ? new JsonWriter(std::move(os)) : 0; |
| 15 } | 22 } |
| 23 #ifndef LLVM_FORCE_HEAD_REVISION |
| 16 ~JsonWriter() { | 24 ~JsonWriter() { |
| 17 os_.close(); | 25 delete os_; |
| 18 } | 26 } |
| 27 #endif |
| 19 void OpenList() { | 28 void OpenList() { |
| 20 Separator(); | 29 Separator(); |
| 21 os_ << "["; | 30 *os_ << "["; |
| 22 state_.push(false); | 31 state_.push(false); |
| 23 } | 32 } |
| 24 void OpenList(const std::string key) { | 33 void OpenList(const std::string key) { |
| 25 Write(key); | 34 Write(key); |
| 26 os_ << ":"; | 35 *os_ << ":"; |
| 27 OpenList(); | 36 OpenList(); |
| 28 } | 37 } |
| 29 void CloseList() { | 38 void CloseList() { |
| 30 os_ << "]"; | 39 *os_ << "]"; |
| 31 state_.pop(); | 40 state_.pop(); |
| 32 } | 41 } |
| 33 void OpenObject() { | 42 void OpenObject() { |
| 34 Separator(); | 43 Separator(); |
| 35 os_ << "{"; | 44 *os_ << "{"; |
| 36 state_.push(false); | 45 state_.push(false); |
| 37 } | 46 } |
| 38 void CloseObject() { | 47 void CloseObject() { |
| 39 os_ << "}\n"; | 48 *os_ << "}\n"; |
| 40 state_.pop(); | 49 state_.pop(); |
| 41 } | 50 } |
| 42 void Write(const size_t val) { | 51 void Write(const size_t val) { |
| 43 Separator(); | 52 Separator(); |
| 44 os_ << val; | 53 *os_ << val; |
| 45 } | 54 } |
| 46 void Write(const std::string val) { | 55 void Write(const std::string val) { |
| 47 Separator(); | 56 Separator(); |
| 48 os_ << "\"" << val << "\""; | 57 *os_ << "\"" << val << "\""; |
| 49 } | 58 } |
| 50 void Write(const std::string key, const size_t val) { | 59 void Write(const std::string key, const size_t val) { |
| 51 Separator(); | 60 Separator(); |
| 52 os_ << "\"" << key << "\":" << val; | 61 *os_ << "\"" << key << "\":" << val; |
| 53 } | 62 } |
| 54 void Write(const std::string key, const std::string val) { | 63 void Write(const std::string key, const std::string val) { |
| 55 Separator(); | 64 Separator(); |
| 56 os_ << "\"" << key << "\":\"" << val << "\""; | 65 *os_ << "\"" << key << "\":\"" << val << "\""; |
| 57 } | 66 } |
| 58 private: | 67 private: |
| 59 JsonWriter(llvm::raw_fd_ostream* os) : os_(*os) {} | 68 JsonWriter(JSON_WRITER_STREAM os) : os_(std::move(os)) {} |
| 60 void Separator() { | 69 void Separator() { |
| 61 if (state_.empty()) | 70 if (state_.empty()) |
| 62 return; | 71 return; |
| 63 if (state_.top()) { | 72 if (state_.top()) { |
| 64 os_ << ","; | 73 *os_ << ","; |
| 65 return; | 74 return; |
| 66 } | 75 } |
| 67 state_.top() = true; | 76 state_.top() = true; |
| 68 } | 77 } |
| 69 llvm::raw_fd_ostream& os_; | 78 JSON_WRITER_STREAM os_; |
| 70 std::stack<bool> state_; | 79 std::stack<bool> state_; |
| 71 }; | 80 }; |
| 72 | 81 |
| 73 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 82 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
| OLD | NEW |