Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions include/flatbuffers/file_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define FLATBUFFERS_FILE_MANAGER_H_

#include <cstddef>
#include <memory>
#include <set>
#include <string>

Expand Down Expand Up @@ -48,22 +49,8 @@ class FileSaver {
FileSaver& operator=(FileSaver&&) = default;
};

class RealFileSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final;
};

class FileNameSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final;

void Finish() final;

private:
std::set<std::string> file_names_{};
};
std::unique_ptr<FileSaver> CreateFileSaver();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These kind of helper functions don't seem necessary to me.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok I guess they're needed because of the inheritance.
In that case this is all boilerplate, and I don't have a lot of opinions on what is the nicest boilerplate. I would pick whatever the simplest code is :)

std::unique_ptr<FileSaver> CreateFileNameCollector();

} // namespace flatbuffers

Expand Down
26 changes: 20 additions & 6 deletions src/file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@

namespace flatbuffers {

bool RealFileSaver::SaveFile(const char* name, const char* buf, size_t len,
bool binary) {
std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
if (!ofs.is_open()) return false;
ofs.write(buf, len);
return !ofs.bad();
class RealFileSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final {
std::ofstream ofs{name,
binary ? std::ofstream::binary : std::ofstream::out};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all these empty lines here and elsewhere, this is not in the style of the rest of the codebase and the google styleguide

if (!ofs.is_open()) {
return false;
}

ofs.write(buf, len);

return !ofs.bad();
}
};

std::unique_ptr<FileSaver> CreateFileSaver() {
// compiler limitations mean we can't use std::make_unique
return std::unique_ptr<FileSaver>{new RealFileSaver()};
}

} // namespace flatbuffers
41 changes: 26 additions & 15 deletions src/file_name_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,35 @@

namespace flatbuffers {

bool FileNameSaver::SaveFile(const char* name, const char* buf, size_t len,
bool binary) {
(void)buf;
(void)len;
(void)binary;
class FileNameSaver final : public FileSaver {
public:
bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) final {
(void)buf;
(void)len;
(void)binary;

std::ignore = file_names_.insert(name);

// we want to simulate always successful save
return true;
}

std::ignore = file_names_.insert(name);
void Finish() final {
for (const auto& file_name : file_names_) {
// Just print the file names to standard output.
// No actual file is created.
std::cout << file_name << "\n";
}
}

// we want to simulate always successful save
return true;
}
private:
std::set<std::string> file_names_{};
};

void FileNameSaver::Finish() {
for (const auto& file_name : file_names_) {
// Just print the file names to standard output.
// No actual file is created.
std::cout << file_name << "\n";
}
std::unique_ptr<FileSaver> CreateFileNameCollector() {
// compiler limitations mean we can't use std::make_unique
return std::unique_ptr<FileSaver>{new FileNameSaver()};
}

} // namespace flatbuffers
4 changes: 2 additions & 2 deletions src/flatc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ int main(int argc, const char* argv[]) {
// this exists here to ensure file_saver outlives the compilation process
std::unique_ptr<flatbuffers::FileSaver> file_saver;
if (options.file_names_only) {
file_saver.reset(new flatbuffers::FileNameSaver{});
file_saver = flatbuffers::CreateFileNameCollector();
} else {
file_saver.reset(new flatbuffers::RealFileSaver{});
file_saver = flatbuffers::CreateFileSaver();
}

options.opts.file_saver = file_saver.get();
Expand Down
3 changes: 3 additions & 0 deletions src/idl_gen_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ static std::string BinaryFileName(const Parser& parser, const std::string& path,

static bool GenerateBinary(const Parser& parser, const std::string& path,
const std::string& file_name) {
// sanity check we have a file saver
if (!parser.opts.file_saver) return false;

if (parser.opts.use_flexbuffers) {
auto data_vec = parser.flex_builder_.GetBuffer();
auto data_ptr = reinterpret_cast<char*>(data(data_vec));
Expand Down
4 changes: 4 additions & 0 deletions tests/fuzzer/flatbuffers_codegen_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
(flags & flags_skip_unexpected_fields_in_json);
opts.allow_non_utf8 = (flags & flags_allow_non_utf8);

// make sure we have a file saver
auto saver = flatbuffers::CreateFileSaver();
opts.file_saver = saver.get();

flatbuffers::Parser parser(opts);

// Guarantee 0-termination in the input.
Expand Down
Loading