Implement --file-names-only (#8788)

* flatc builds and seems to work, some of the extra targets are having linker errors

* fix build system

* pipeline failures

* un-rename files

* refactor to use unique_ptr

* typo

* rm make_unique, add comments

* fix cmake

---------

Co-authored-by: Wouter van Oortmerssen <aardappel@gmail.com>
This commit is contained in:
Justin Davis
2025-12-02 23:37:06 -05:00
committed by GitHub
parent 0b60686e3d
commit a1e125af11
34 changed files with 207 additions and 210 deletions

View File

@@ -19,12 +19,14 @@
#include <string>
#include "flatbuffers/file_manager.h"
#include "flatbuffers/idl.h"
namespace flatbuffers {
struct CodeGenOptions {
std::string output_path;
FileSaver* file_saver{nullptr};
};
// A code generator interface for producing converting flatbuffer schema into

View File

@@ -17,30 +17,52 @@
#ifndef FLATBUFFERS_FILE_MANAGER_H_
#define FLATBUFFERS_FILE_MANAGER_H_
#include <cstddef>
#include <set>
#include <string>
#include "flatbuffers/util.h"
namespace flatbuffers {
// A File interface to write data to file by default or
// save only file names
class FileManager {
class FileSaver {
public:
FileManager() = default;
virtual ~FileManager() = default;
FileSaver() = default;
virtual ~FileSaver() = default;
virtual bool SaveFile(const std::string& absolute_file_name,
const std::string& content) = 0;
virtual bool SaveFile(const char* name, const char* buf, size_t len,
bool binary) = 0;
virtual bool LoadFile(const std::string& absolute_file_name,
std::string* buf) = 0;
bool SaveFile(const char* name, const std::string& buf, bool binary) {
return SaveFile(name, buf.c_str(), buf.size(), binary);
}
virtual void Finish() {}
private:
// Copying is not supported.
FileManager(const FileManager&) = delete;
FileManager& operator=(const FileManager&) = delete;
FileSaver(const FileSaver&) = delete;
FileSaver& operator=(const FileSaver&) = delete;
// Rule of 5
FileSaver(FileSaver&&) = default;
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_{};
};
} // namespace flatbuffers

View File

@@ -25,6 +25,7 @@
#include <vector>
#include "flatbuffers/base.h"
#include "flatbuffers/file_manager.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/flexbuffers.h"
#include "flatbuffers/hash.h"
@@ -634,6 +635,10 @@ inline bool operator<(const IncludedFile& a, const IncludedFile& b) {
// Container of options that may apply to any of the source/text generators.
struct IDLOptions {
// file saver
// shared pointer since this object gets copied and modified.
FileSaver* file_saver = nullptr;
// field case style options for C++
enum CaseStyle { CaseStyle_Unchanged = 0, CaseStyle_Upper, CaseStyle_Lower };
enum class ProtoIdGapAction { NO_OP, WARNING, ERROR };

View File

@@ -448,12 +448,16 @@ bool LoadFile(const char* name, bool binary, std::string* buf);
// If "binary" is false data is written using ifstream's
// text mode, otherwise data is written with no
// transcoding.
// NOTE: this function is deprecated in favor of FileSaver class,
// but left here for compatibility.
bool SaveFile(const char* name, const char* buf, size_t len, bool binary);
// Save data "buf" into file "name" returning true if
// successful, false otherwise. If "binary" is false
// data is written using ifstream's text mode, otherwise
// data is written with no transcoding.
// NOTE: this function is deprecated in favor of FileSaver class,
// but left here for compatibility.
inline bool SaveFile(const char* name, const std::string& buf, bool binary) {
return SaveFile(name, buf.c_str(), buf.size(), binary);
}