Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: tools/clang/blink_gc_plugin/JsonWriter.h

Issue 2009293002: Cleanup: Pass std::string as const reference from tools/clang/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert tools/clang/empty_string/tests/test-original.cc Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Helper to write information for the points-to graph. 10 // Helper to write information for the points-to graph.
11 class JsonWriter { 11 class JsonWriter {
12 public: 12 public:
13 static JsonWriter* from(std::unique_ptr<llvm::raw_ostream> os) { 13 static JsonWriter* from(std::unique_ptr<llvm::raw_ostream> os) {
14 return os ? new JsonWriter(std::move(os)) : 0; 14 return os ? new JsonWriter(std::move(os)) : 0;
15 } 15 }
16 void OpenList() { 16 void OpenList() {
17 Separator(); 17 Separator();
18 *os_ << "["; 18 *os_ << "[";
19 state_.push(false); 19 state_.push(false);
20 } 20 }
21 void OpenList(const std::string key) { 21 void OpenList(const std::string& key) {
22 Write(key); 22 Write(key);
23 *os_ << ":"; 23 *os_ << ":";
24 OpenList(); 24 OpenList();
25 } 25 }
26 void CloseList() { 26 void CloseList() {
27 *os_ << "]"; 27 *os_ << "]";
28 state_.pop(); 28 state_.pop();
29 } 29 }
30 void OpenObject() { 30 void OpenObject() {
31 Separator(); 31 Separator();
32 *os_ << "{"; 32 *os_ << "{";
33 state_.push(false); 33 state_.push(false);
34 } 34 }
35 void CloseObject() { 35 void CloseObject() {
36 *os_ << "}\n"; 36 *os_ << "}\n";
37 state_.pop(); 37 state_.pop();
38 } 38 }
39 void Write(const size_t val) { 39 void Write(const size_t val) {
40 Separator(); 40 Separator();
41 *os_ << val; 41 *os_ << val;
42 } 42 }
43 void Write(const std::string val) { 43 void Write(const std::string& val) {
44 Separator(); 44 Separator();
45 *os_ << "\"" << val << "\""; 45 *os_ << "\"" << val << "\"";
46 } 46 }
47 void Write(const std::string key, const size_t val) { 47 void Write(const std::string& key, const size_t val) {
48 Separator(); 48 Separator();
49 *os_ << "\"" << key << "\":" << val; 49 *os_ << "\"" << key << "\":" << val;
50 } 50 }
51 void Write(const std::string key, const std::string val) { 51 void Write(const std::string& key, const std::string val) {
hans 2016/05/25 17:45:57 Should probably pass 'val' by reference to while w
ki.stfu 2016/05/25 17:53:07 Missed that, thanks :)
52 Separator(); 52 Separator();
53 *os_ << "\"" << key << "\":\"" << val << "\""; 53 *os_ << "\"" << key << "\":\"" << val << "\"";
54 } 54 }
55 private: 55 private:
56 JsonWriter(std::unique_ptr<llvm::raw_ostream> os) : os_(std::move(os)) {} 56 JsonWriter(std::unique_ptr<llvm::raw_ostream> os) : os_(std::move(os)) {}
57 void Separator() { 57 void Separator() {
58 if (state_.empty()) 58 if (state_.empty())
59 return; 59 return;
60 if (state_.top()) { 60 if (state_.top()) {
61 *os_ << ","; 61 *os_ << ",";
62 return; 62 return;
63 } 63 }
64 state_.top() = true; 64 state_.top() = true;
65 } 65 }
66 std::unique_ptr<llvm::raw_ostream> os_; 66 std::unique_ptr<llvm::raw_ostream> os_;
67 std::stack<bool> state_; 67 std::stack<bool> state_;
68 }; 68 };
69 69
70 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_ 70 #endif // TOOLS_BLINK_GC_PLUGIN_JSON_WRITER_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698