• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_
6 #define TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_
7 
8 #include "llvm/Support/raw_ostream.h"
9 
10 // Helper to write information for the points-to graph.
11 class JsonWriter {
12  public:
from(llvm::raw_fd_ostream * os)13   static JsonWriter* from(llvm::raw_fd_ostream* os) {
14     return os ? new JsonWriter(os) : 0;
15   }
~JsonWriter()16   ~JsonWriter() {
17     os_.close();
18   }
OpenList()19   void OpenList() {
20     Separator();
21     os_ << "[";
22     state_.push(false);
23   }
OpenList(const std::string key)24   void OpenList(const std::string key) {
25     Write(key);
26     os_ << ":";
27     OpenList();
28   }
CloseList()29   void CloseList() {
30     os_ << "]";
31     state_.pop();
32   }
OpenObject()33   void OpenObject() {
34     Separator();
35     os_ << "{";
36     state_.push(false);
37   }
CloseObject()38   void CloseObject() {
39     os_ << "}\n";
40     state_.pop();
41   }
Write(const size_t val)42   void Write(const size_t val) {
43     Separator();
44     os_ << val;
45   }
Write(const std::string val)46   void Write(const std::string val) {
47     Separator();
48     os_ << "\"" << val << "\"";
49   }
Write(const std::string key,const size_t val)50   void Write(const std::string key, const size_t val) {
51     Separator();
52     os_ << "\"" << key << "\":" << val;
53   }
Write(const std::string key,const std::string val)54   void Write(const std::string key, const std::string val) {
55     Separator();
56     os_ << "\"" << key << "\":\"" << val << "\"";
57   }
58  private:
JsonWriter(llvm::raw_fd_ostream * os)59   JsonWriter(llvm::raw_fd_ostream* os) : os_(*os) {}
Separator()60   void Separator() {
61     if (state_.empty())
62       return;
63     if (state_.top()) {
64       os_ << ",";
65       return;
66     }
67     state_.top() = true;
68   }
69   llvm::raw_fd_ostream& os_;
70   std::stack<bool> state_;
71 };
72 
73 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_
74