Coverage Report

Created: 2025-12-14 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flatbuffers/include/flatbuffers/flatc.h
Line
Count
Source
1
/*
2
 * Copyright 2017 Google Inc. All rights reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#ifndef FLATBUFFERS_FLATC_H_
18
#define FLATBUFFERS_FLATC_H_
19
20
#include <functional>
21
#include <limits>
22
#include <list>
23
#include <memory>
24
#include <string>
25
26
#include "flatbuffers/code_generator.h"
27
#include "flatbuffers/flatbuffers.h"
28
#include "flatbuffers/idl.h"
29
#include "flatbuffers/util.h"
30
31
namespace flatbuffers {
32
33
extern void LogCompilerWarn(const std::string& warn);
34
extern void LogCompilerError(const std::string& err);
35
36
struct FlatCOptions {
37
  IDLOptions opts;
38
39
  std::string program_name;
40
41
  std::string output_path;
42
43
  std::vector<std::string> filenames;
44
45
  std::list<std::string> include_directories_storage;
46
  std::vector<const char*> include_directories;
47
  std::vector<const char*> conform_include_directories;
48
  std::vector<bool> generator_enabled;
49
  size_t binary_files_from = std::numeric_limits<size_t>::max();
50
  std::string conform_to_schema;
51
  std::string annotate_schema;
52
  bool annotate_include_vector_contents = true;
53
  bool any_generator = false;
54
  bool print_make_rules = false;
55
  bool raw_binary = false;
56
  bool schema_binary = false;
57
  bool grpc_enabled = false;
58
  bool requires_bfbs = false;
59
  bool file_names_only = false;
60
61
  std::vector<std::shared_ptr<CodeGenerator>> generators;
62
};
63
64
struct FlatCOption {
65
  std::string short_opt;
66
  std::string long_opt;
67
  std::string parameter;
68
  std::string description;
69
};
70
71
class FlatCompiler {
72
 public:
73
  typedef void (*WarnFn)(const FlatCompiler* flatc, const std::string& warn,
74
                         bool show_exe_name);
75
76
  typedef void (*ErrorFn)(const FlatCompiler* flatc, const std::string& err,
77
                          bool usage, bool show_exe_name);
78
79
  // Parameters required to initialize the FlatCompiler.
80
  struct InitParams {
81
0
    InitParams() : warn_fn(nullptr), error_fn(nullptr) {}
82
83
    WarnFn warn_fn;
84
    ErrorFn error_fn;
85
  };
86
87
0
  explicit FlatCompiler(const InitParams& params) : params_(params) {}
88
89
  bool RegisterCodeGenerator(const FlatCOption& option,
90
                             std::shared_ptr<CodeGenerator> code_generator);
91
92
  int Compile(const FlatCOptions& options);
93
94
  std::string GetShortUsageString(const std::string& program_name) const;
95
  std::string GetUsageString(const std::string& program_name) const;
96
97
  // Parse the FlatC options from command line arguments.
98
  FlatCOptions ParseFromCommandLineArguments(int argc, const char** argv);
99
100
 private:
101
  void ParseFile(flatbuffers::Parser& parser, const std::string& filename,
102
                 const std::string& contents,
103
                 const std::vector<const char*>& include_directories) const;
104
105
  void LoadBinarySchema(Parser& parser, const std::string& filename,
106
                        const std::string& contents);
107
108
  void Warn(const std::string& warn, bool show_exe_name = true) const;
109
110
  void Error(const std::string& err, bool usage = true,
111
             bool show_exe_name = true) const;
112
113
  void AnnotateBinaries(const uint8_t* binary_schema,
114
                        uint64_t binary_schema_size,
115
                        const FlatCOptions& options);
116
117
  void ValidateOptions(const FlatCOptions& options);
118
119
  Parser GetConformParser(const FlatCOptions& options);
120
121
  std::unique_ptr<Parser> GenerateCode(const FlatCOptions& options,
122
                                       Parser& conform_parser);
123
124
  std::map<std::string, std::shared_ptr<CodeGenerator>> code_generators_;
125
126
  InitParams params_;
127
};
128
129
}  // namespace flatbuffers
130
131
#endif  // FLATBUFFERS_FLATC_H_