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

Side by Side Diff: courgette/courgette_flow.cc

Issue 2827103002: [Courgette] Refactor: Add CourgetteFlow; improve courgette_tool.cc help text. (Closed)
Patch Set: Add virtual destructor to BasicBuffer and its implementations. Created 3 years, 8 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 | « courgette/courgette_flow.h ('k') | courgette/courgette_tool.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "courgette/courgette_flow.h"
6
7 #include <stdarg.h>
8
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "courgette/assembly_program.h"
14 #include "courgette/encoded_program.h"
15 #include "courgette/program_detector.h"
16
17 namespace courgette {
18
19 /******** CourgetteFlow::Data ********/
20
21 CourgetteFlow::Data::Data() = default;
22
23 CourgetteFlow::Data::~Data() = default;
24
25 /******** CourgetteFlow ********/
26
27 CourgetteFlow::CourgetteFlow() = default;
28
29 CourgetteFlow::~CourgetteFlow() = default;
30
31 // static
32 const char* CourgetteFlow::name(Group group) {
33 switch (group) {
34 case ONLY:
35 return "input";
36 case OLD:
37 return "'old' input";
38 case NEW:
39 return "'new' input";
40 default:
41 NOTREACHED();
42 break;
43 }
44 return nullptr;
45 }
46
47 CourgetteFlow::Data* CourgetteFlow::data(Group group) {
48 switch (group) {
49 case ONLY:
50 return &data_only_;
51 case OLD:
52 return &data_old_;
53 case NEW:
54 return &data_new_;
55 default:
56 NOTREACHED();
57 break;
58 }
59 return nullptr;
60 }
61
62 bool CourgetteFlow::ok() {
63 return status_ == C_OK;
64 }
65
66 bool CourgetteFlow::failed() {
67 return status_ != C_OK;
68 }
69
70 Status CourgetteFlow::status() {
71 return status_;
72 }
73
74 const std::string& CourgetteFlow::message() {
75 return message_;
76 }
77
78 void CourgetteFlow::ReadSourceStreamSetFromBuffer(Group group,
79 const BasicBuffer& buffer) {
80 if (failed())
81 return;
82 Data* d = data(group);
83 if (!check(d->sources.Init(buffer.data(), buffer.length()),
84 C_GENERAL_ERROR)) {
85 setMessage("Cannot read %s as SourceStreamSet.", name(group));
86 }
87 }
88
89 void CourgetteFlow::ReadAssemblyProgramFromBuffer(Group group,
90 const BasicBuffer& buffer,
91 bool annotate) {
92 if (failed())
93 return;
94 Data* d = data(group);
95 auto parser = annotate ? ParseDetectedExecutableWithAnnotation
96 : ParseDetectedExecutable;
97 if (!check(parser(buffer.data(), buffer.length(), &d->program)))
98 setMessage("Cannot parse %s (code = %d).", name(group), status_);
99 }
100
101 void CourgetteFlow::ReadEncodedProgramFromSourceStreamSet(
102 Group group,
103 SourceStreamSet* opt_sources /* nullptr */) {
104 if (failed())
105 return;
106 Data* d = data(group);
107 SourceStreamSet* sources = opt_sources ? opt_sources : &d->sources;
108 if (!check(ReadEncodedProgram(sources, &d->encoded)))
109 setMessage("Cannot read %s as encoded program.", name(group));
110 }
111
112 void CourgetteFlow::CreateEncodedProgramFromAssemblyProgram(Group group) {
113 if (failed())
114 return;
115 Data* d = data(group);
116 if (!check(Encode(*d->program, &d->encoded)))
117 setMessage("Cannot encode %s (code = %d).", name(group), status_);
118 }
119
120 void CourgetteFlow::WriteSinkStreamFromSinkStreamSet(Group group,
121 SinkStream* sink) {
122 DCHECK(sink);
123 if (failed())
124 return;
125 if (!check(data(group)->sinks.CopyTo(sink), C_GENERAL_ERROR))
126 setMessage("Cannnot combine serialized streams for %s.", name(group));
127 }
128
129 void CourgetteFlow::WriteSinkStreamSetFromEncodedProgram(
130 Group group,
131 SinkStreamSet* opt_sinks /* nullptr */) {
132 if (failed())
133 return;
134 Data* d = data(group);
135 SinkStreamSet* sinks = opt_sinks ? opt_sinks : &d->sinks;
136 if (!check(WriteEncodedProgram(d->encoded.get(), sinks)))
137 setMessage("Cannot serialize encoded %s.", name(group));
138 }
139
140 void CourgetteFlow::WriteExecutableFromEncodedProgram(Group group,
141 SinkStream* sink) {
142 DCHECK(sink);
143 if (failed())
144 return;
145 if (!check(Assemble(data(group)->encoded.get(), sink)))
146 setMessage("Cannot assemble %s.", name(group));
147 }
148
149 void CourgetteFlow::AdjustNewAssemblyProgramToMatchOld() {
150 if (failed())
151 return;
152 if (!check(Adjust(*data_old_.program, data_new_.program.get())))
153 setMessage("Cannot adjust %s to match %s.", name(OLD), name(NEW));
154 }
155
156 void CourgetteFlow::DestroyAssemblyProgram(Group group) {
157 if (failed())
158 return;
159 data(group)->program.reset();
160 }
161
162 void CourgetteFlow::DestroyEncodedProgram(Group group) {
163 if (failed())
164 return;
165 data(group)->encoded.reset();
166 }
167
168 bool CourgetteFlow::check(Status new_status) {
169 if (new_status == C_OK)
170 return true;
171 status_ = new_status;
172 return false;
173 }
174
175 bool CourgetteFlow::check(bool success, Status failure_mode) {
176 if (success)
177 return true;
178 status_ = failure_mode;
179 return false;
180 }
181
182 void CourgetteFlow::setMessage(const char* format, ...) {
183 va_list args;
184 va_start(args, format);
185 message_ = base::StringPrintV(format, args);
186 va_end(args);
187 }
188
189 } // namespace courgette
OLDNEW
« no previous file with comments | « courgette/courgette_flow.h ('k') | courgette/courgette_tool.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698