| 751 | } |
| 752 | |
| 753 | std::unique_ptr<Parser> FlatCompiler::GenerateCode(const FlatCOptions &options, |
| 754 | Parser &conform_parser) { |
| 755 | std::unique_ptr<Parser> parser = |
| 756 | std::unique_ptr<Parser>(new Parser(options.opts)); |
| 757 | |
| 758 | for (auto file_it = options.filenames.begin(); |
| 759 | file_it != options.filenames.end(); ++file_it) { |
| 760 | IDLOptions opts = options.opts; |
| 761 | |
| 762 | auto &filename = *file_it; |
| 763 | std::string contents; |
| 764 | if (!flatbuffers::LoadFile(filename.c_str(), true, &contents)) |
| 765 | Error("unable to load file: " + filename); |
| 766 | |
| 767 | bool is_binary = static_cast<size_t>(file_it - options.filenames.begin()) >= |
| 768 | options.binary_files_from; |
| 769 | auto ext = flatbuffers::GetExtension(filename); |
| 770 | const bool is_schema = ext == "fbs" || ext == "proto"; |
| 771 | if (is_schema && opts.project_root.empty()) { |
| 772 | opts.project_root = StripFileName(filename); |
| 773 | } |
| 774 | const bool is_binary_schema = ext == reflection::SchemaExtension(); |
| 775 | if (is_binary) { |
| 776 | parser->builder_.Clear(); |
| 777 | parser->builder_.PushFlatBuffer( |
| 778 | reinterpret_cast<const uint8_t *>(contents.c_str()), |
| 779 | contents.length()); |
| 780 | if (!options.raw_binary) { |
| 781 | // Generally reading binaries that do not correspond to the schema |
| 782 | // will crash, and sadly there's no way around that when the binary |
| 783 | // does not contain a file identifier. |
| 784 | // We'd expect that typically any binary used as a file would have |
| 785 | // such an identifier, so by default we require them to match. |
| 786 | if (!parser->file_identifier_.length()) { |
| 787 | Error("current schema has no file_identifier: cannot test if \"" + |
| 788 | filename + |
| 789 | "\" matches the schema, use --raw-binary to read this file" |
| 790 | " anyway."); |
| 791 | } else if (!flatbuffers::BufferHasIdentifier( |
| 792 | contents.c_str(), parser->file_identifier_.c_str(), |
| 793 | opts.size_prefixed)) { |
| 794 | Error("binary \"" + filename + |
| 795 | "\" does not have expected file_identifier \"" + |
| 796 | parser->file_identifier_ + |
| 797 | "\", use --raw-binary to read this file anyway."); |
| 798 | } |
| 799 | } |
| 800 | } else { |
| 801 | // Check if file contains 0 bytes. |
| 802 | if (!opts.use_flexbuffers && !is_binary_schema && |
| 803 | contents.length() != strlen(contents.c_str())) { |
| 804 | Error("input file appears to be binary: " + filename, true); |
| 805 | } |
| 806 | if (is_schema || is_binary_schema) { |
| 807 | // If we're processing multiple schemas, make sure to start each |
| 808 | // one from scratch. If it depends on previous schemas it must do |
| 809 | // so explicitly using an include. |
| 810 | parser.reset(new Parser(opts)); |
nothing calls this directly
no test coverage detected