Skip to content

Commit 79d1a85

Browse files
authored
JSONGenerator/Loader cleanups (#5146)
Signed-off-by: Chris Dodd <cdodd@nvidia.com>
1 parent 3ffac66 commit 79d1a85

File tree

31 files changed

+525
-487
lines changed

31 files changed

+525
-487
lines changed

backends/bmv2/pna_nic/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int main(int argc, char *const argv[]) {
8484
}
8585
std::istream inJson(&fb);
8686
JSONLoader jsonFileLoader(inJson);
87-
if (jsonFileLoader.json == nullptr) {
87+
if (!jsonFileLoader) {
8888
::P4::error(ErrorType::ERR_IO, "%s: Not valid input file", options.file);
8989
return 1;
9090
}
@@ -102,7 +102,7 @@ int main(int argc, char *const argv[]) {
102102
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
103103
return 1;
104104
if (options.dumpJsonFile.empty())
105-
JSONGenerator(*openFile(options.dumpJsonFile, true), true) << program << std::endl;
105+
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
106106
} catch (const std::exception &bug) {
107107
std::cerr << bug.what() << std::endl;
108108
return 1;

backends/bmv2/psa_switch/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int main(int argc, char *const argv[]) {
8484
}
8585
std::istream inJson(&fb);
8686
JSONLoader jsonFileLoader(inJson);
87-
if (jsonFileLoader.json == nullptr) {
87+
if (!jsonFileLoader) {
8888
::P4::error(ErrorType::ERR_IO, "%s: Not valid input file", options.file);
8989
return 1;
9090
}
@@ -102,7 +102,7 @@ int main(int argc, char *const argv[]) {
102102
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
103103
return 1;
104104
if (!options.dumpJsonFile.empty())
105-
JSONGenerator(*openFile(options.dumpJsonFile, true), true) << program << std::endl;
105+
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
106106
} catch (const std::exception &bug) {
107107
std::cerr << bug.what() << std::endl;
108108
return 1;

backends/bmv2/simple_switch/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ int main(int argc, char *const argv[]) {
8787
}
8888
std::istream inJson(&fb);
8989
JSONLoader jsonFileLoader(inJson);
90-
if (jsonFileLoader.json == nullptr) {
90+
if (!jsonFileLoader) {
9191
::P4::error(ErrorType::ERR_IO, "%s: Not valid json input file", options.file);
9292
return 1;
9393
}
@@ -105,7 +105,7 @@ int main(int argc, char *const argv[]) {
105105
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
106106
return 1;
107107
if (!options.dumpJsonFile.empty() && !options.loadIRFromJson)
108-
JSONGenerator(*openFile(options.dumpJsonFile, true), true) << program << std::endl;
108+
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
109109
} catch (const std::exception &bug) {
110110
std::cerr << bug.what() << std::endl;
111111
return 1;

backends/dpdk/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int main(int argc, char *const argv[]) {
105105
}
106106
std::istream inJson(&fb);
107107
JSONLoader jsonFileLoader(inJson);
108-
if (jsonFileLoader.json == nullptr) {
108+
if (!jsonFileLoader) {
109109
::P4::error(ErrorType::ERR_INVALID, "Not valid input file");
110110
return 1;
111111
}
@@ -136,7 +136,7 @@ int main(int argc, char *const argv[]) {
136136
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
137137
return 1;
138138
if (!options.dumpJsonFile.empty())
139-
JSONGenerator(*openFile(options.dumpJsonFile, true), true) << program << std::endl;
139+
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
140140
} catch (const std::exception &bug) {
141141
std::cerr << bug.what() << std::endl;
142142
return 1;

backends/ebpf/p4c-ebpf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void compile(EbpfOptions &options) {
5656

5757
std::istream inJson(&fb);
5858
JSONLoader jsonFileLoader(inJson);
59-
if (jsonFileLoader.json == nullptr) {
59+
if (!jsonFileLoader) {
6060
::P4::error(ErrorType::ERR_IO, "%s: Not valid input file", options.file);
6161
return;
6262
}
@@ -84,7 +84,7 @@ void compile(EbpfOptions &options) {
8484
midend.addDebugHook(hook);
8585
auto toplevel = midend.run(options, program);
8686
if (!options.dumpJsonFile.empty())
87-
JSONGenerator(*openFile(options.dumpJsonFile, true)) << program << std::endl;
87+
JSONGenerator(*openFile(options.dumpJsonFile, true)).emit(program);
8888
if (::P4::errorCount() > 0) return;
8989

9090
EBPF::run_ebpf_backend(options, toplevel, &midend.refMap, &midend.typeMap);

backends/graphs/p4c-graphs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int main(int argc, char *const argv[]) {
151151

152152
std::istream inJson(&fb);
153153
JSONLoader jsonFileLoader(inJson);
154-
if (jsonFileLoader.json == nullptr) {
154+
if (!jsonFileLoader) {
155155
::P4::error(ErrorType::ERR_IO, "Not valid input file");
156156
return 1;
157157
}
@@ -181,7 +181,7 @@ int main(int argc, char *const argv[]) {
181181
try {
182182
top = midEnd.process(program);
183183
if (!options.dumpJsonFile.empty())
184-
JSONGenerator(*openFile(options.dumpJsonFile, true)) << program << std::endl;
184+
JSONGenerator(*openFile(options.dumpJsonFile, true)).emit(program);
185185
} catch (const std::exception &bug) {
186186
std::cerr << bug.what() << std::endl;
187187
return 1;

backends/p4test/p4test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,17 @@ int main(int argc, char *const argv[]) {
185185
}
186186
if (program) {
187187
if (!options.dumpJsonFile.empty())
188-
JSONGenerator(*openFile(options.dumpJsonFile, true), true) << program << std::endl;
188+
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
189189
if (options.debugJson) {
190190
std::stringstream ss1, ss2;
191191
JSONGenerator gen1(ss1), gen2(ss2);
192-
gen1 << program;
192+
gen1.emit(program);
193193

194194
const IR::Node *node = nullptr;
195195
JSONLoader loader(ss1);
196196
loader >> node;
197197

198-
gen2 << node;
198+
gen2.emit(node);
199199
if (ss1.str() != ss2.str()) {
200200
error(ErrorType::ERR_UNEXPECTED, "json mismatch");
201201
std::ofstream t1("t1.json"), t2("t2.json");

backends/p4tools/common/core/z3_solver.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,11 @@ const IR::Literal *Z3Solver::toLiteral(const z3::expr &e, const IR::Type *type)
299299
}
300300

301301
void Z3Solver::toJSON(JSONGenerator &json) const {
302-
json << json.indent << "{\n";
303-
json.indent++;
304-
json << json.indent << "\"checkpoints\" : " << checkpoints;
305-
json << ",\n";
306-
json << json.indent << "\"declarations\" : " << declaredVarsById;
307-
json << ",\n";
308-
json << json.indent << "\"assertions\" : " << p4Assertions;
309-
json.indent--;
310-
json << json.indent << "}\n";
302+
auto state = json.begin_object();
303+
json.emit("checkpoints", checkpoints);
304+
json.emit("declarations", declaredVarsById);
305+
json.emit("assertions", p4Assertions);
306+
json.end_object(state);
311307
}
312308

313309
void Z3Solver::addZ3Pushes(size_t &chkIndex, size_t asrtIndex) {
@@ -344,13 +340,13 @@ Z3Solver::Z3Solver(bool isIncremental, std::optional<std::istream *> inOpt)
344340
JSONLoader loader(*inOpt.value());
345341

346342
JSONLoader solverCheckpoints(loader, "checkpoints");
347-
BUG_CHECK(solverCheckpoints.json->is<JsonVector>(),
343+
BUG_CHECK(solverCheckpoints.is<JsonVector>(),
348344
"Z3 solver loading: can't find list of checkpoint");
349345
solverCheckpoints >> checkpoints;
350346

351347
// loading all assertions
352348
JSONLoader solverAssertions(loader, "assertions");
353-
BUG_CHECK(solverAssertions.json->is<JsonVector>(),
349+
BUG_CHECK(solverAssertions.is<JsonVector>(),
354350
"Z3 solver loading: can't find list of assertions");
355351
safe_vector<const Constraint *> assertions;
356352
solverAssertions >> assertions;

backends/tc/tc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main(int argc, char *const argv[]) {
8383
return 1;
8484
}
8585
if (!options.dumpJsonFile.empty())
86-
JSONGenerator(*openFile(options.dumpJsonFile, true)) << toplevel << std::endl;
86+
JSONGenerator(*openFile(options.dumpJsonFile, true)).emit(toplevel);
8787
} catch (const Util::P4CExceptionBase &bug) {
8888
std::cerr << bug.what() << std::endl;
8989
return 1;

backends/tofino/bf-p4c/ir/arch.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ class P4Thread {
101101
const IR::P4Control *mau = nullptr;
102102
const IR::P4Control *deparser = nullptr;
103103
toJSON {
104-
json << json.indent << "\"parsers\" : " << parsers << "," << std::endl
105-
<< json.indent << "\"mau\" : " << mau << "," << std::endl
106-
<< json.indent << "\"deparser\" : " << deparser; }
104+
json.emit("parsers", parsers);
105+
json.emit("mau", mau);
106+
json.emit("deparser", deparser); }
107107
fromJSON {
108108
IR::BFN::P4Thread * thread = new IR::BFN::P4Thread();
109109
json.load("parsers", thread->parsers);

0 commit comments

Comments
 (0)