Skip to content

Commit ff6ac66

Browse files
authored
Return unique_ptr from openFile for clear ownership semantics (p4lang#5480)
Signed-off-by: Attaullah Ansari <mdattaullahansari152@gmail.com>
1 parent ce64442 commit ff6ac66

21 files changed

Lines changed: 83 additions & 74 deletions

File tree

backends/bmv2/pna_nic/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ int main(int argc, char *const argv[]) {
101101
toplevel = midEnd.process(program);
102102
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
103103
return 1;
104-
if (options.dumpJsonFile.empty())
105-
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
104+
if (options.dumpJsonFile.empty()) {
105+
auto dumpJsonStream = openFile(options.dumpJsonFile, true);
106+
JSONGenerator(*dumpJsonStream, true).emit(program);
107+
}
106108
} catch (const std::exception &bug) {
107109
std::cerr << bug.what() << std::endl;
108110
return 1;
@@ -123,8 +125,7 @@ int main(int argc, char *const argv[]) {
123125
if (::P4::errorCount() > 0) return 1;
124126

125127
if (!options.outputFile.empty()) {
126-
std::ostream *out = openFile(options.outputFile, false);
127-
if (out != nullptr) {
128+
if (auto out = openFile(options.outputFile, false)) {
128129
backend->serialize(*out);
129130
out->flush();
130131
}

backends/bmv2/psa_switch/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ int main(int argc, char *const argv[]) {
101101
toplevel = midEnd.process(program);
102102
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
103103
return 1;
104-
if (!options.dumpJsonFile.empty())
105-
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
104+
if (!options.dumpJsonFile.empty()) {
105+
auto dumpJsonStream = openFile(options.dumpJsonFile, true);
106+
JSONGenerator(*dumpJsonStream, true).emit(program);
107+
}
106108
} catch (const std::exception &bug) {
107109
std::cerr << bug.what() << std::endl;
108110
return 1;
@@ -123,8 +125,7 @@ int main(int argc, char *const argv[]) {
123125
if (::P4::errorCount() > 0) return 1;
124126

125127
if (!options.outputFile.empty()) {
126-
std::ostream *out = openFile(options.outputFile, false);
127-
if (out != nullptr) {
128+
if (auto out = openFile(options.outputFile, false)) {
128129
backend->serialize(*out);
129130
out->flush();
130131
}

backends/bmv2/simple_switch/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ int main(int argc, char *const argv[]) {
104104
toplevel = midEnd.process(program);
105105
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
106106
return 1;
107-
if (!options.dumpJsonFile.empty() && !options.loadIRFromJson)
108-
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
107+
if (!options.dumpJsonFile.empty() && !options.loadIRFromJson) {
108+
auto dumpJsonStream = openFile(options.dumpJsonFile, true);
109+
JSONGenerator(*dumpJsonStream, true).emit(program);
110+
}
109111
} catch (const std::exception &bug) {
110112
std::cerr << bug.what() << std::endl;
111113
return 1;
@@ -126,8 +128,7 @@ int main(int argc, char *const argv[]) {
126128
if (::P4::errorCount() > 0) return 1;
127129

128130
if (!options.outputFile.empty()) {
129-
std::ostream *out = openFile(options.outputFile, false);
130-
if (out != nullptr) {
131+
if (auto out = openFile(options.outputFile, false)) {
131132
backend->serialize(*out);
132133
out->flush();
133134
}

backends/dpdk/backend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ void DpdkBackend::convert(const IR::ToplevelBlock *tlb) {
116116
new VisitFunctor([this, genContextJson] {
117117
// Serialize context json object into user specified file
118118
if (!options.ctxtFile.empty()) {
119-
if (std::ostream *out = openFile(options.ctxtFile, false)) {
120-
genContextJson->serializeContextJson(out);
119+
if (auto out = openFile(options.ctxtFile, false)) {
120+
genContextJson->serializeContextJson(out.get());
121121
out->flush();
122122
} else
123123
::P4::error(ErrorType::ERR_IO, "Could not open file: %1%", options.ctxtFile);

backends/dpdk/main.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ void generateTDIBfrtJson(bool isTDI, const IR::P4Program *program, DPDK::DpdkOpt
5454

5555
std::filesystem::path filename = isTDI ? options.tdiFile : options.bfRtSchema;
5656
auto p4rt = new P4::BFRT::BFRuntimeSchemaGenerator(*p4Runtime.p4Info, isTDI, options);
57-
std::ostream *out = openFile(filename, false);
58-
if (!out) {
57+
if (auto out = openFile(filename, false)) {
58+
p4rt->serializeBFRuntimeSchema(out.get());
59+
} else {
5960
::P4::error(ErrorType::ERR_IO, "Could not open file: %1%", filename);
60-
return;
6161
}
62-
p4rt->serializeBFRuntimeSchema(out);
6362
}
6463

6564
int main(int argc, char *const argv[]) {
@@ -134,8 +133,10 @@ int main(int argc, char *const argv[]) {
134133
toplevel = midEnd.process(program);
135134
if (::P4::errorCount() > 1 || toplevel == nullptr || toplevel->getMain() == nullptr)
136135
return 1;
137-
if (!options.dumpJsonFile.empty())
138-
JSONGenerator(*openFile(options.dumpJsonFile, true), true).emit(program);
136+
if (!options.dumpJsonFile.empty()) {
137+
auto dumpJsonStream = openFile(options.dumpJsonFile, true);
138+
JSONGenerator(*dumpJsonStream, true).emit(program);
139+
}
139140
} catch (const std::exception &bug) {
140141
std::cerr << bug.what() << std::endl;
141142
return 1;
@@ -148,8 +149,7 @@ int main(int argc, char *const argv[]) {
148149
if (::P4::errorCount() > 0) return 1;
149150

150151
if (!options.outputFile.empty()) {
151-
std::ostream *out = openFile(options.outputFile, false);
152-
if (out != nullptr) {
152+
if (auto out = openFile(options.outputFile, false)) {
153153
backend->codegen(*out);
154154
out->flush();
155155
}

backends/ebpf/ebpfBackend.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ void emitFilterModel(const EbpfOptions &options, Target *target, const IR::Tople
3838

3939
if (options.outputFile.empty()) return;
4040

41-
auto *cstream = openFile(options.outputFile, false);
41+
auto cstream = openFile(options.outputFile, false);
4242
if (cstream == nullptr) return;
4343

4444
std::filesystem::path hfile = options.outputFile;
4545
hfile.replace_extension(".h");
46-
auto *hstream = openFile(hfile, false);
46+
auto hstream = openFile(hfile, false);
4747
if (hstream == nullptr) return;
4848

4949
ebpfprog->emitH(&h, hfile);
@@ -94,11 +94,10 @@ void run_ebpf_backend(const EbpfOptions &options, const IR::ToplevelBlock *tople
9494

9595
if (options.outputFile.empty()) return;
9696

97-
auto cstream = openFile(options.outputFile, false);
98-
if (cstream == nullptr) return;
99-
100-
backend->codegen(*cstream);
101-
cstream->flush();
97+
if (auto cstream = openFile(options.outputFile, false)) {
98+
backend->codegen(*cstream);
99+
cstream->flush();
100+
}
102101
} else {
103102
::P4::error(ErrorType::ERR_UNKNOWN,
104103
"Unknown architecture %s; legal choices are 'filter', and 'psa'", options.arch);

backends/ebpf/p4c-ebpf.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ void compile(EbpfOptions &options) {
8383
EBPF::MidEnd midend;
8484
midend.addDebugHook(hook);
8585
auto toplevel = midend.run(options, program);
86-
if (!options.dumpJsonFile.empty())
87-
JSONGenerator(*openFile(options.dumpJsonFile, true)).emit(program);
86+
if (!options.dumpJsonFile.empty()) {
87+
auto dumpJsonStream = openFile(options.dumpJsonFile, true);
88+
JSONGenerator(*dumpJsonStream).emit(program);
89+
}
8890
if (::P4::errorCount() > 0) return;
8991

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

backends/graphs/graph_visitor.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ namespace P4::graphs {
2323

2424
void Graph_visitor::writeGraphToFile(const Graph &g, const std::string &name) {
2525
auto path = graphsDir / (name + ".dot");
26-
auto out = openFile(path, false);
27-
if (out == nullptr) {
26+
if (auto out = openFile(path, false)) {
27+
// Custom label writers not supported with subgraphs, so we populate
28+
// *_attribute_t properties instead using our GraphAttributeSetter class.
29+
boost::write_graphviz(*out, g);
30+
} else {
2831
::P4::error(ErrorType::ERR_IO, "Failed to open file %1%", path);
29-
return;
3032
}
31-
// Custom label writers not supported with subgraphs, so we populate
32-
// *_attribute_t properties instead using our GraphAttributeSetter class.
33-
boost::write_graphviz(*out, g);
3433
}
3534

3635
const char *Graph_visitor::getType(const VertexType &v_type) {

backends/graphs/p4c-graphs.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ int main(int argc, char *const argv[]) {
180180
const IR::ToplevelBlock *top = nullptr;
181181
try {
182182
top = midEnd.process(program);
183-
if (!options.dumpJsonFile.empty())
184-
JSONGenerator(*openFile(options.dumpJsonFile, true)).emit(program);
183+
if (!options.dumpJsonFile.empty()) {
184+
auto dumpJsonStream = openFile(options.dumpJsonFile, true);
185+
JSONGenerator(*dumpJsonStream).emit(program);
186+
}
185187
} catch (const std::exception &bug) {
186188
std::cerr << bug.what() << std::endl;
187189
return 1;

backends/p4fmt/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cstdlib>
22
#include <filesystem>
3+
#include <memory>
34
#include <sstream>
45

56
#include "lib/nullstream.h"
@@ -22,17 +23,19 @@ int main(int argc, char *const argv[]) {
2223
};
2324

2425
std::ostream *out = nullptr;
26+
std::unique_ptr<std::ostream> outFile;
2527
// Write to stdout in absence of an output file.
2628
if (options.outputFile().empty()) {
2729
out = &std::cout;
2830
} else {
29-
out = openFile(options.outputFile(), false);
30-
if ((out == nullptr) || !(*out)) {
31+
outFile = openFile(options.outputFile(), false);
32+
if ((outFile == nullptr) || !(*outFile)) {
3133
::P4::error(ErrorType::ERR_NOT_FOUND, "%2%: No such file or directory.",
3234
options.outputFile().string());
3335
options.usage();
3436
return EXIT_FAILURE;
3537
}
38+
out = outFile.get();
3639
}
3740

3841
(*out) << formattedOutput.str();

0 commit comments

Comments
 (0)