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(std::unique_ptr<llvm::raw_ostream> os) { | 20 static JsonWriter* from(JSON_WRITER_STREAM os) { |
14 return os ? new JsonWriter(std::move(os)) : 0; | 21 return os ? new JsonWriter(std::move(os)) : 0; |
15 } | 22 } |
| 23 #ifndef LLVM_FORCE_HEAD_REVISION |
| 24 ~JsonWriter() { |
| 25 delete os_; |
| 26 } |
| 27 #endif |
16 void OpenList() { | 28 void OpenList() { |
17 Separator(); | 29 Separator(); |
18 *os_ << "["; | 30 *os_ << "["; |
19 state_.push(false); | 31 state_.push(false); |
20 } | 32 } |
21 void OpenList(const std::string key) { | 33 void OpenList(const std::string key) { |
22 Write(key); | 34 Write(key); |
23 *os_ << ":"; | 35 *os_ << ":"; |
24 OpenList(); | 36 OpenList(); |
25 } | 37 } |
(...skipping 20 matching lines...) Expand all Loading... |
46 } | 58 } |
47 void Write(const std::string key, const size_t val) { | 59 void Write(const std::string key, const size_t val) { |
48 Separator(); | 60 Separator(); |
49 *os_ << "\"" << key << "\":" << val; | 61 *os_ << "\"" << key << "\":" << val; |
50 } | 62 } |
51 void Write(const std::string key, const std::string val) { | 63 void Write(const std::string key, const std::string val) { |
52 Separator(); | 64 Separator(); |
53 *os_ << "\"" << key << "\":\"" << val << "\""; | 65 *os_ << "\"" << key << "\":\"" << val << "\""; |
54 } | 66 } |
55 private: | 67 private: |
56 JsonWriter(std::unique_ptr<llvm::raw_ostream> os) : os_(std::move(os)) {} | 68 JsonWriter(JSON_WRITER_STREAM os) : os_(std::move(os)) {} |
57 void Separator() { | 69 void Separator() { |
58 if (state_.empty()) | 70 if (state_.empty()) |
59 return; | 71 return; |
60 if (state_.top()) { | 72 if (state_.top()) { |
61 *os_ << ","; | 73 *os_ << ","; |
62 return; | 74 return; |
63 } | 75 } |
64 state_.top() = true; | 76 state_.top() = true; |
65 } | 77 } |
66 std::unique_ptr<llvm::raw_ostream> os_; | 78 JSON_WRITER_STREAM os_; |
67 std::stack<bool> state_; | 79 std::stack<bool> state_; |
68 }; | 80 }; |
69 | 81 |
70 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ | 82 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ |
OLD | NEW |