diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-08-01 12:59:39 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2016-08-04 12:40:43 +0000 |
commit | 28b1110370900897ab652cb420c371fab8857ad4 (patch) | |
tree | 41b32127d23b0df4f2add2a27e12dc87bddb260e /chromium/third_party/flatbuffers | |
parent | 399c965b6064c440ddcf4015f5f8e9d131c7a0a6 (diff) | |
download | qtwebengine-chromium-28b1110370900897ab652cb420c371fab8857ad4.tar.gz |
BASELINE: Update Chromium to 53.0.2785.41
Also adds a few extra files for extensions.
Change-Id: Iccdd55d98660903331cf8b7b29188da781830af4
Reviewed-by: Michael Brüning <michael.bruning@qt.io>
Diffstat (limited to 'chromium/third_party/flatbuffers')
38 files changed, 831 insertions, 542 deletions
diff --git a/chromium/third_party/flatbuffers/BUILD.gn b/chromium/third_party/flatbuffers/BUILD.gn index d470eb1fbfd..a6dc8f42ec8 100644 --- a/chromium/third_party/flatbuffers/BUILD.gn +++ b/chromium/third_party/flatbuffers/BUILD.gn @@ -22,8 +22,9 @@ source_set("flatbuffers") { source_set("compiler_files") { sources = [ - "src/include/idl.h", - "src/include/util.h", + "src/include/flatbuffers/code_generators.h", + "src/include/flatbuffers/idl.h", + "src/include/flatbuffers/util.h", "src/src/idl_gen_cpp.cpp", "src/src/idl_gen_fbs.cpp", "src/src/idl_gen_general.cpp", diff --git a/chromium/third_party/flatbuffers/README.chromium b/chromium/third_party/flatbuffers/README.chromium index 2b88217844f..b1e099bdaa8 100644 --- a/chromium/third_party/flatbuffers/README.chromium +++ b/chromium/third_party/flatbuffers/README.chromium @@ -1,8 +1,8 @@ Name: FlatBuffers Short Name: flatbuffers URL: https://github.com/google/flatbuffers -Version: 6f751d5d26fd566f8c9060358101a2596677f9cf -Date: 2016-02-20 +Version: e92ae5199d52fd59540a800bec7eef46cd778257 +Date: 2016-06-03 License: Apache 2.0 License File: LICENSE Security Critical: yes diff --git a/chromium/third_party/flatbuffers/flatbuffers.gyp b/chromium/third_party/flatbuffers/flatbuffers.gyp index 215864f1c5a..61f79fb7e8f 100644 --- a/chromium/third_party/flatbuffers/flatbuffers.gyp +++ b/chromium/third_party/flatbuffers/flatbuffers.gyp @@ -22,8 +22,9 @@ 'src/src/idl_parser.cpp', 'src/src/reflection.cpp', 'src/src/util.cpp', - 'src/include/idl.h', - 'src/include/util.h', + 'src/include/flatbuffers/code_generators.h', + 'src/include/flatbuffers/idl.h', + 'src/include/flatbuffers/util.h', ], }, { diff --git a/chromium/third_party/flatbuffers/src/CMakeLists.txt b/chromium/third_party/flatbuffers/src/CMakeLists.txt index 8449ffb610b..b1e56d6b0e7 100644 --- a/chromium/third_party/flatbuffers/src/CMakeLists.txt +++ b/chromium/third_party/flatbuffers/src/CMakeLists.txt @@ -17,6 +17,7 @@ if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) endif() set(FlatBuffers_Library_SRCS + include/flatbuffers/code_generators.h include/flatbuffers/flatbuffers.h include/flatbuffers/hash.h include/flatbuffers/idl.h @@ -135,6 +136,9 @@ endif() if(FLATBUFFERS_BUILD_FLATC) add_executable(flatc ${FlatBuffers_Compiler_SRCS}) + if(NOT FLATBUFFERS_FLATC_EXECUTABLE) + set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>) + endif() endif() if(FLATBUFFERS_BUILD_FLATHASH) diff --git a/chromium/third_party/flatbuffers/src/android/build_apk.sh b/chromium/third_party/flatbuffers/src/android/build_apk.sh index 4967ee6ddf2..1b9f4a45143 100755 --- a/chromium/third_party/flatbuffers/src/android/build_apk.sh +++ b/chromium/third_party/flatbuffers/src/android/build_apk.sh @@ -1,4 +1,5 @@ #!/bin/bash -eu +# # Copyright (c) 2013 Google, Inc. # # This software is provided 'as-is', without any express or implied diff --git a/chromium/third_party/flatbuffers/src/biicode/support/bii-travis.sh b/chromium/third_party/flatbuffers/src/biicode/support/bii-travis.sh index 0f5d2f96092..67854c7ce38 100755 --- a/chromium/third_party/flatbuffers/src/biicode/support/bii-travis.sh +++ b/chromium/third_party/flatbuffers/src/biicode/support/bii-travis.sh @@ -1,4 +1,18 @@ #!/bin/bash +# +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. sudo apt-get update -qq sudo apt-get install libglu1-mesa-dev xorg-dev diff --git a/chromium/third_party/flatbuffers/src/docs/source/CppUsage.md b/chromium/third_party/flatbuffers/src/docs/source/CppUsage.md index 8c7f6a7d474..d4abe66e4b7 100755 --- a/chromium/third_party/flatbuffers/src/docs/source/CppUsage.md +++ b/chromium/third_party/flatbuffers/src/docs/source/CppUsage.md @@ -26,8 +26,7 @@ your program by including the header. As noted, this header relies on The code for the FlatBuffers C++ library can be found at `flatbuffers/include/flatbuffers`. You can browse the library code on the -[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/ -include/flatbuffers). +[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/include/flatbuffers). ## Testing the FlatBuffers C++ library diff --git a/chromium/third_party/flatbuffers/src/docs/source/FlatBuffers.md b/chromium/third_party/flatbuffers/src/docs/source/FlatBuffers.md index 46d4c12014e..d69f404fbe7 100644 --- a/chromium/third_party/flatbuffers/src/docs/source/FlatBuffers.md +++ b/chromium/third_party/flatbuffers/src/docs/source/FlatBuffers.md @@ -145,7 +145,7 @@ sections provide a more in-depth usage guide. - [GitHub repository](http://github.com/google/flatbuffers) - [Landing page](http://google.github.io/flatbuffers) -- [FlatBuffers Google Group](http://group.google.com/group/flatbuffers) +- [FlatBuffers Google Group](https://groups.google.com/forum/#!forum/flatbuffers) - [FlatBuffers Issues Tracker](http://github.com/google/flatbuffers/issues) - Independent implementations & tools: - [FlatCC](https://github.com/dvidelabs/flatcc) Alternative FlatBuffers diff --git a/chromium/third_party/flatbuffers/src/go/table.go b/chromium/third_party/flatbuffers/src/go/table.go index 695b92da8f1..976a7dba1fa 100644 --- a/chromium/third_party/flatbuffers/src/go/table.go +++ b/chromium/third_party/flatbuffers/src/go/table.go @@ -26,7 +26,8 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT { // String gets a string from data stored inside the flatbuffer. func (t *Table) String(off UOffsetT) string { - return string(t.ByteVector(off)) + b := t.ByteVector(off) + return byteSliceToString(b) } // ByteVector gets a byte slice from data stored inside the flatbuffer. diff --git a/chromium/third_party/flatbuffers/src/go/unsafe.go b/chromium/third_party/flatbuffers/src/go/unsafe.go index e90ccb09d0d..566e8b0bb20 100644 --- a/chromium/third_party/flatbuffers/src/go/unsafe.go +++ b/chromium/third_party/flatbuffers/src/go/unsafe.go @@ -43,3 +43,8 @@ var ( // SizeVOffsetT is the byte size of an VOffsetT. SizeVOffsetT = int(unsafe.Sizeof(VOffsetT(0))) ) + +// byteSliceToString converts a []byte to string without a heap allocation. +func byteSliceToString(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} diff --git a/chromium/third_party/flatbuffers/src/include/flatbuffers/code_generators.h b/chromium/third_party/flatbuffers/src/include/flatbuffers/code_generators.h new file mode 100644 index 00000000000..95fa0c1ac5a --- /dev/null +++ b/chromium/third_party/flatbuffers/src/include/flatbuffers/code_generators.h @@ -0,0 +1,92 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLATBUFFERS_CODE_GENERATORS_H_ +#define FLATBUFFERS_CODE_GENERATORS_H_ + +namespace flatbuffers { + +class BaseGenerator { + public: + virtual bool generate() = 0; + + static const std::string NamespaceDir(const Parser &parser, + const std::string &path) { + EnsureDirExists(path.c_str()); + if (parser.opts.one_file) return path; + std::string namespace_dir = path; // Either empty or ends in separator. + auto &namespaces = parser.namespaces_.back()->components; + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { + namespace_dir += *it + kPathSeparator; + EnsureDirExists(namespace_dir.c_str()); + } + return namespace_dir; + } + + protected: + BaseGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : parser_(parser), + path_(path), + file_name_(file_name), + namespace_dir_(BaseGenerator::NamespaceDir(parser, path)){}; + virtual ~BaseGenerator(){}; + + // No copy/assign. + BaseGenerator &operator=(const BaseGenerator &); + BaseGenerator(const BaseGenerator &); + + const char *FlatBuffersGeneratedWarning() { + return "automatically generated by the FlatBuffers compiler," + " do not modify\n\n"; + } + + bool IsEverythingGenerated() { + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + if (!(*it)->generated) return false; + } + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + if (!(*it)->generated) return false; + } + return true; + } + + std::string FullNamespace(const char *separator) { + std::string namespace_name; + auto &namespaces = parser_.namespaces_.back()->components; + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { + if (namespace_name.length()) namespace_name += separator; + namespace_name += *it; + } + return namespace_name; + } + + const std::string LastNamespacePart() { + auto &namespaces = parser_.namespaces_.back()->components; + if (namespaces.size()) return *(namespaces.end() - 1); else return std::string(""); + } + + const Parser &parser_; + const std::string &path_; + const std::string &file_name_; + const std::string namespace_dir_; +}; + +} // namespace flatbuffers + +#endif // FLATBUFFERS_CODE_GENERATORS_H_ diff --git a/chromium/third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h b/chromium/third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h index a33092f3d31..272901163fc 100644 --- a/chromium/third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h +++ b/chromium/third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h @@ -333,7 +333,7 @@ public: // Change elements if you have a non-const pointer to this object. // Scalars only. See reflection.h, and the documentation. - void Mutate(uoffset_t i, T val) { + void Mutate(uoffset_t i, const T& val) { assert(i < size()); WriteScalar(data() + i, val); } @@ -1151,6 +1151,17 @@ template<typename T> const T *GetRoot(const void *buf) { return GetMutableRoot<T>(const_cast<void *>(buf)); } +/// Helpers to get a typed pointer to objects that are currently beeing built. +/// @warning Creating new objects will lead to reallocations and invalidates the pointer! +template<typename T> T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) { + return reinterpret_cast<T *>(fbb.GetCurrentBufferPointer() + + fbb.GetSize() - offset.o); +} + +template<typename T> const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) { + return GetMutableTemporaryPointer<T>(fbb, offset); +} + // Helper to see if the identifier in a buffer has the expected value. inline bool BufferHasIdentifier(const void *buf, const char *identifier) { return strncmp(reinterpret_cast<const char *>(buf) + sizeof(uoffset_t), @@ -1222,7 +1233,8 @@ class Verifier FLATBUFFERS_FINAL_CLASS { // must be 0. auto size = ReadScalar<uoffset_t>(vec); auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size; - Check(size < max_elems); // Protect against byte_size overflowing. + if (!Check(size < max_elems)) + return false; // Protect against byte_size overflowing. auto byte_size = sizeof(size) + elem_size * size; *end = vec + byte_size; return Verify(vec, byte_size); @@ -1382,6 +1394,7 @@ class Table { // Check the vtable size field, then check vtable fits in its entirety. return verifier.VerifyComplexity() && verifier.Verify<voffset_t>(vtable) && + (ReadScalar<voffset_t>(vtable) & (sizeof(voffset_t) - 1)) == 0 && verifier.Verify(vtable, ReadScalar<voffset_t>(vtable)); } @@ -1483,6 +1496,35 @@ volatile __attribute__((weak)) const char *flatbuffer_version_string = #endif // !defined(_WIN32) && !defined(__CYGWIN__) +#define DEFINE_BITMASK_OPERATORS(E, T)\ + inline E operator | (E lhs, E rhs){\ + return E(T(lhs) | T(rhs));\ + }\ + inline E operator & (E lhs, E rhs){\ + return E(T(lhs) & T(rhs));\ + }\ + inline E operator ^ (E lhs, E rhs){\ + return E(T(lhs) ^ T(rhs));\ + }\ + inline E operator ~ (E lhs){\ + return E(~T(lhs));\ + }\ + inline E operator |= (E &lhs, E rhs){\ + lhs = lhs | rhs;\ + return lhs;\ + }\ + inline E operator &= (E &lhs, E rhs){\ + lhs = lhs & rhs;\ + return lhs;\ + }\ + inline E operator ^= (E &lhs, E rhs){\ + lhs = lhs ^ rhs;\ + return lhs;\ + }\ + inline bool operator !(E rhs) \ + {\ + return !bool(T(rhs)); \ + } /// @endcond } // namespace flatbuffers diff --git a/chromium/third_party/flatbuffers/src/net/FlatBuffers/ByteBuffer.cs b/chromium/third_party/flatbuffers/src/net/FlatBuffers/ByteBuffer.cs index 7ca491dc590..5fa1ac7a51d 100755 --- a/chromium/third_party/flatbuffers/src/net/FlatBuffers/ByteBuffer.cs +++ b/chromium/third_party/flatbuffers/src/net/FlatBuffers/ByteBuffer.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferBuilder.cs b/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferBuilder.cs index c320ea8ccdf..590e6ac0d74 100644 --- a/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferBuilder.cs +++ b/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferBuilder.cs @@ -80,9 +80,9 @@ namespace FlatBuffers /// <summary> /// Gets and sets a Boolean to disable the optimization when serializing /// default values to a Table. - /// + /// /// In order to save space, fields that are set to their default value - /// don't get serialized into the buffer. + /// don't get serialized into the buffer. /// </summary> public bool ForceDefaults { get; set; } @@ -349,7 +349,7 @@ namespace FlatBuffers /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param> /// <param name="d">The default value to compare the value against</param> public void AddBool(int o, bool x, bool d) { if (ForceDefaults || x != d) { AddBool(x); Slot(o); } } - + /// <summary> /// Adds a SByte to the Table at index `o` in its vtable using the value `x` and default `d` /// </summary> @@ -358,7 +358,7 @@ namespace FlatBuffers /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param> /// <param name="d">The default value to compare the value against</param> public void AddSbyte(int o, sbyte x, sbyte d) { if (ForceDefaults || x != d) { AddSbyte(x); Slot(o); } } - + /// <summary> /// Adds a Byte to the Table at index `o` in its vtable using the value `x` and default `d` /// </summary> diff --git a/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferConstants.cs b/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferConstants.cs index 162ed192343..ab3092c484c 100644 --- a/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferConstants.cs +++ b/chromium/third_party/flatbuffers/src/net/FlatBuffers/FlatBufferConstants.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/chromium/third_party/flatbuffers/src/net/FlatBuffers/Properties/AssemblyInfo.cs b/chromium/third_party/flatbuffers/src/net/FlatBuffers/Properties/AssemblyInfo.cs index 1065ff8f28c..1edfac47aae 100644 --- a/chromium/third_party/flatbuffers/src/net/FlatBuffers/Properties/AssemblyInfo.cs +++ b/chromium/third_party/flatbuffers/src/net/FlatBuffers/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("FlatBuffers")] -[assembly: AssemblyCopyright("Copyright © 2015 Google Inc")] +[assembly: AssemblyCopyright("Copyright (c) 2015 Google Inc")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/chromium/third_party/flatbuffers/src/net/FlatBuffers/Struct.cs b/chromium/third_party/flatbuffers/src/net/FlatBuffers/Struct.cs index 4cd280124b1..ab16f28cbee 100644 --- a/chromium/third_party/flatbuffers/src/net/FlatBuffers/Struct.cs +++ b/chromium/third_party/flatbuffers/src/net/FlatBuffers/Struct.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/chromium/third_party/flatbuffers/src/python/flatbuffers/builder.py b/chromium/third_party/flatbuffers/src/python/flatbuffers/builder.py index 18cfab9c70d..552d0e2b780 100644 --- a/chromium/third_party/flatbuffers/src/python/flatbuffers/builder.py +++ b/chromium/third_party/flatbuffers/src/python/flatbuffers/builder.py @@ -401,7 +401,7 @@ class Builder(object): if isinstance(s, compat.string_types): x = s.encode(encoding, errors) - elif isinstance(s, compat.binary_type): + elif isinstance(s, compat.binary_types): x = s else: raise TypeError("non-string passed to CreateString") diff --git a/chromium/third_party/flatbuffers/src/python/flatbuffers/compat.py b/chromium/third_party/flatbuffers/src/python/flatbuffers/compat.py index 345e38cbf2a..e0315351d74 100644 --- a/chromium/third_party/flatbuffers/src/python/flatbuffers/compat.py +++ b/chromium/third_party/flatbuffers/src/python/flatbuffers/compat.py @@ -1,3 +1,17 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + """ A tiny version of `six` to help with backwards compability. """ import sys @@ -11,13 +25,16 @@ PY34 = sys.version_info[0:2] >= (3, 4) if PY3: string_types = (str,) - binary_type = bytes + binary_types = (bytes,bytearray) range_func = range memoryview_type = memoryview struct_bool_decl = "?" else: - string_types = (basestring,) - binary_type = str + string_types = (unicode,) + if PY26 or PY27: + binary_types = (str,bytearray) + else: + binary_types = (str,) range_func = xrange if PY26 or (PY27 and not PY275): memoryview_type = buffer diff --git a/chromium/third_party/flatbuffers/src/python/flatbuffers/packer.py b/chromium/third_party/flatbuffers/src/python/flatbuffers/packer.py index c525f180b07..20ee9f11680 100644 --- a/chromium/third_party/flatbuffers/src/python/flatbuffers/packer.py +++ b/chromium/third_party/flatbuffers/src/python/flatbuffers/packer.py @@ -1,3 +1,17 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + """ Provide pre-compiled struct packers for encoding and decoding. diff --git a/chromium/third_party/flatbuffers/src/python/setup.py b/chromium/third_party/flatbuffers/src/python/setup.py index 822c92626af..39afef34bce 100644 --- a/chromium/third_party/flatbuffers/src/python/setup.py +++ b/chromium/third_party/flatbuffers/src/python/setup.py @@ -1,3 +1,17 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from setuptools import setup setup( diff --git a/chromium/third_party/flatbuffers/src/reflection/generate_code.sh b/chromium/third_party/flatbuffers/src/reflection/generate_code.sh index 90f655d22f1..da8a18130cc 100644 --- a/chromium/third_party/flatbuffers/src/reflection/generate_code.sh +++ b/chromium/third_party/flatbuffers/src/reflection/generate_code.sh @@ -1 +1,17 @@ +#!/bin/bash +# +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + ../flatc -c --no-prefix -o ../include/flatbuffers reflection.fbs diff --git a/chromium/third_party/flatbuffers/src/samples/android/build_apk.sh b/chromium/third_party/flatbuffers/src/samples/android/build_apk.sh index 4967ee6ddf2..1b9f4a45143 100755 --- a/chromium/third_party/flatbuffers/src/samples/android/build_apk.sh +++ b/chromium/third_party/flatbuffers/src/samples/android/build_apk.sh @@ -1,4 +1,5 @@ #!/bin/bash -eu +# # Copyright (c) 2013 Google, Inc. # # This software is provided 'as-is', without any express or implied diff --git a/chromium/third_party/flatbuffers/src/samples/android_sample.sh b/chromium/third_party/flatbuffers/src/samples/android_sample.sh index 9e746fb2ace..ead8fd807a9 100755 --- a/chromium/third_party/flatbuffers/src/samples/android_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/android_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script requires the Android NDK and Android SDK to be installed. # It also requires an Android device to be connected for installing and # running the applicaton. diff --git a/chromium/third_party/flatbuffers/src/samples/csharp_sample.sh b/chromium/third_party/flatbuffers/src/samples/csharp_sample.sh index 5c86f7b78d7..ea472ed9c05 100755 --- a/chromium/third_party/flatbuffers/src/samples/csharp_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/csharp_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script runs on Mac and Linux. It requires `mono` to be installed # and `flatc` to be built (using `cmake` in the root directory). diff --git a/chromium/third_party/flatbuffers/src/samples/go_sample.sh b/chromium/third_party/flatbuffers/src/samples/go_sample.sh index 2a0605f203e..13a96c12932 100755 --- a/chromium/third_party/flatbuffers/src/samples/go_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/go_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script runs on Mac and Linux. It requires `go` to be installed # and 'flatc' to be built (using `cmake` in the root directory). diff --git a/chromium/third_party/flatbuffers/src/samples/java_sample.sh b/chromium/third_party/flatbuffers/src/samples/java_sample.sh index 3e7ebcfe1a2..bd1315f4ce4 100755 --- a/chromium/third_party/flatbuffers/src/samples/java_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/java_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script runs on Mac and Linux. It requires `java` to be installed # and `flatc` to be built (using `cmake` in the root directory). diff --git a/chromium/third_party/flatbuffers/src/samples/javascript_sample.sh b/chromium/third_party/flatbuffers/src/samples/javascript_sample.sh index 794ec4b7c72..4bbc478d7f5 100755 --- a/chromium/third_party/flatbuffers/src/samples/javascript_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/javascript_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script runs on Mac and Linux. It requires `Node.js` to be installed # and `flatc` to be built (using `cmake` in the root directory). diff --git a/chromium/third_party/flatbuffers/src/samples/monster_generated.h b/chromium/third_party/flatbuffers/src/samples/monster_generated.h index cf45bbb91fd..1a16126a564 100644 --- a/chromium/third_party/flatbuffers/src/samples/monster_generated.h +++ b/chromium/third_party/flatbuffers/src/samples/monster_generated.h @@ -5,12 +5,13 @@ #include "flatbuffers/flatbuffers.h" - namespace MyGame { namespace Sample { struct Vec3; + struct Monster; + struct Weapon; enum Color { @@ -55,8 +56,11 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { : x_(flatbuffers::EndianScalar(_x)), y_(flatbuffers::EndianScalar(_y)), z_(flatbuffers::EndianScalar(_z)) { } float x() const { return flatbuffers::EndianScalar(x_); } + void mutate_x(float _x) { flatbuffers::WriteScalar(&x_, _x); } float y() const { return flatbuffers::EndianScalar(y_); } + void mutate_y(float _y) { flatbuffers::WriteScalar(&y_, _y); } float z() const { return flatbuffers::EndianScalar(z_); } + void mutate_z(float _z) { flatbuffers::WriteScalar(&z_, _z); } }; STRUCT_END(Vec3, 12); @@ -73,14 +77,23 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_EQUIPPED = 22 }; const Vec3 *pos() const { return GetStruct<const Vec3 *>(VT_POS); } + Vec3 *mutable_pos() { return GetStruct<Vec3 *>(VT_POS); } int16_t mana() const { return GetField<int16_t>(VT_MANA, 150); } + bool mutate_mana(int16_t _mana) { return SetField(VT_MANA, _mana); } int16_t hp() const { return GetField<int16_t>(VT_HP, 100); } + bool mutate_hp(int16_t _hp) { return SetField(VT_HP, _hp); } const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(VT_NAME); } + flatbuffers::String *mutable_name() { return GetPointer<flatbuffers::String *>(VT_NAME); } const flatbuffers::Vector<uint8_t> *inventory() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_INVENTORY); } + flatbuffers::Vector<uint8_t> *mutable_inventory() { return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_INVENTORY); } Color color() const { return static_cast<Color>(GetField<int8_t>(VT_COLOR, 2)); } + bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast<int8_t>(_color)); } const flatbuffers::Vector<flatbuffers::Offset<Weapon>> *weapons() const { return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<Weapon>> *>(VT_WEAPONS); } + flatbuffers::Vector<flatbuffers::Offset<Weapon>> *mutable_weapons() { return GetPointer<flatbuffers::Vector<flatbuffers::Offset<Weapon>> *>(VT_WEAPONS); } Equipment equipped_type() const { return static_cast<Equipment>(GetField<uint8_t>(VT_EQUIPPED_TYPE, 0)); } + bool mutate_equipped_type(Equipment _equipped_type) { return SetField(VT_EQUIPPED_TYPE, static_cast<uint8_t>(_equipped_type)); } const void *equipped() const { return GetPointer<const void *>(VT_EQUIPPED); } + void *mutable_equipped() { return GetPointer<void *>(VT_EQUIPPED); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField<Vec3>(verifier, VT_POS) && @@ -150,7 +163,9 @@ struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_DAMAGE = 6 }; const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(VT_NAME); } + flatbuffers::String *mutable_name() { return GetPointer<flatbuffers::String *>(VT_NAME); } int16_t damage() const { return GetField<int16_t>(VT_DAMAGE, 0); } + bool mutate_damage(int16_t _damage) { return SetField(VT_DAMAGE, _damage); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField<flatbuffers::uoffset_t>(verifier, VT_NAME) && @@ -192,6 +207,8 @@ inline bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *union_o inline const MyGame::Sample::Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<MyGame::Sample::Monster>(buf); } +inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot<Monster>(buf); } + inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer<MyGame::Sample::Monster>(); } inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<MyGame::Sample::Monster> root) { fbb.Finish(root); } diff --git a/chromium/third_party/flatbuffers/src/samples/php_sample.sh b/chromium/third_party/flatbuffers/src/samples/php_sample.sh index 8a55e2a053b..c23edc380f9 100755 --- a/chromium/third_party/flatbuffers/src/samples/php_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/php_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script runs on Mac and Linux. It requires `php` to be installed # and `flatc` to be built (using `cmake` in the root directory). diff --git a/chromium/third_party/flatbuffers/src/samples/python_sample.sh b/chromium/third_party/flatbuffers/src/samples/python_sample.sh index 9148c7d81dc..ca5de2b03fd 100755 --- a/chromium/third_party/flatbuffers/src/samples/python_sample.sh +++ b/chromium/third_party/flatbuffers/src/samples/python_sample.sh @@ -1,4 +1,5 @@ #!/bin/bash +# # Copyright 2015 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +# # Note: This script runs on Mac and Linux. It requires `python` to be installed # and `flatc` to be built (using `cmake` in the root directory). diff --git a/chromium/third_party/flatbuffers/src/src/idl_gen_cpp.cpp b/chromium/third_party/flatbuffers/src/src/idl_gen_cpp.cpp index c091ee12d1b..7efa1334e58 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_gen_cpp.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_gen_cpp.cpp @@ -19,6 +19,7 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "flatbuffers/code_generators.h" namespace flatbuffers { namespace cpp { @@ -165,6 +166,7 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, if (parser.opts.scoped_enums) code += " : " + GenTypeBasic(enum_def.underlying_type, false); code += " {\n"; + int64_t anyv = 0; EnumVal *minv = nullptr, *maxv = nullptr; for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); @@ -175,13 +177,26 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, code += NumToString(ev.value) + ",\n"; minv = !minv || minv->value > ev.value ? &ev : minv; maxv = !maxv || maxv->value < ev.value ? &ev : maxv; + anyv |= ev.value; + } + if (parser.opts.scoped_enums || parser.opts.prefixed_enums) { + assert(minv && maxv); + if (enum_def.attributes.Lookup("bit_flags")) { + if (minv->value != 0) // If the user didn't defined NONE value + code += " " + GenEnumVal(enum_def, "NONE", parser.opts) + " = 0,\n"; + if (maxv->value != anyv) // If the user didn't defined ANY value + code += " " + GenEnumVal(enum_def, "ANY", parser.opts) + " = " + NumToString(anyv) + "\n"; + } else { // MIN & MAX are useless for bit_flags + code += " " + GenEnumVal(enum_def, "MIN", parser.opts) + " = "; + code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n"; + code += " " + GenEnumVal(enum_def, "MAX", parser.opts) + " = "; + code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n"; + } } - assert(minv && maxv); - code += " " + GenEnumVal(enum_def, "MIN", parser.opts) + " = "; - code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n"; - code += " " + GenEnumVal(enum_def, "MAX", parser.opts) + " = "; - code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n"; - code += "};\n\n"; + code += "};\n"; + if (parser.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags")) + code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " + GenTypeBasic(enum_def.underlying_type, false) + ")\n"; + code += "\n"; // Generate a generate string table for enum values. // Problem is, if values are very sparse that could generate really big @@ -714,197 +729,189 @@ struct IsAlnum { } }; -// Iterate through all definitions we haven't generate code for (enums, structs, -// and tables) and output them to a single file. -std::string GenerateCPP(const Parser &parser, - const std::string &file_name) { - // Check if we have any code to generate at all, to avoid an empty header. - for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); - ++it) { - if (!(*it)->generated) goto generate_code; - } - for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); - ++it) { - if (!(*it)->generated) goto generate_code; - } - // No code to generate, exit: - return std::string(); - - generate_code: - - using namespace cpp; - - std::string code; - code = "// automatically generated by the FlatBuffers compiler," - " do not modify\n\n"; - - // Generate include guard. - std::string include_guard_ident = file_name; - // Remove any non-alpha-numeric characters that may appear in a filename. - include_guard_ident.erase( - std::remove_if(include_guard_ident.begin(), - include_guard_ident.end(), - IsAlnum()), - include_guard_ident.end()); - std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident; - include_guard += "_"; - // For further uniqueness, also add the namespace. - auto name_space = parser.namespaces_.back(); - for (auto it = name_space->components.begin(); - it != name_space->components.end(); ++it) { - include_guard += *it + "_"; - } - include_guard += "H_"; - std::transform(include_guard.begin(), include_guard.end(), - include_guard.begin(), ::toupper); - code += "#ifndef " + include_guard + "\n"; - code += "#define " + include_guard + "\n\n"; - - code += "#include \"flatbuffers/flatbuffers.h\"\n\n"; - - if (parser.opts.include_dependence_headers) { - int num_includes = 0; - for (auto it = parser.included_files_.begin(); - it != parser.included_files_.end(); ++it) { - auto basename = flatbuffers::StripPath( - flatbuffers::StripExtension(it->first)); - if (basename != file_name) { - code += "#include \"" + basename + "_generated.h\"\n"; - num_includes++; +static std::string GeneratedFileName(const std::string &path, + const std::string &file_name) { + return path + file_name + "_generated.h"; +} + +namespace cpp { +class CppGenerator : public BaseGenerator { + public: + CppGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + // Iterate through all definitions we haven't generate code for (enums, + // structs, + // and tables) and output them to a single file. + bool generate() { + if (IsEverythingGenerated()) return true; + + std::string code; + code = code + "// " + FlatBuffersGeneratedWarning(); + + // Generate include guard. + std::string include_guard_ident = file_name_; + // Remove any non-alpha-numeric characters that may appear in a filename. + include_guard_ident.erase( + std::remove_if(include_guard_ident.begin(), include_guard_ident.end(), + IsAlnum()), + include_guard_ident.end()); + std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident; + include_guard += "_"; + // For further uniqueness, also add the namespace. + auto name_space = parser_.namespaces_.back(); + for (auto it = name_space->components.begin(); + it != name_space->components.end(); ++it) { + include_guard += *it + "_"; + } + include_guard += "H_"; + std::transform(include_guard.begin(), include_guard.end(), + include_guard.begin(), ::toupper); + code += "#ifndef " + include_guard + "\n"; + code += "#define " + include_guard + "\n\n"; + + code += "#include \"flatbuffers/flatbuffers.h\"\n\n"; + + if (parser_.opts.include_dependence_headers) { + int num_includes = 0; + for (auto it = parser_.included_files_.begin(); + it != parser_.included_files_.end(); ++it) { + auto basename = + flatbuffers::StripPath(flatbuffers::StripExtension(it->first)); + if (basename != file_name_) { + code += "#include \"" + basename + "_generated.h\"\n"; + num_includes++; + } } + if (num_includes) code += "\n"; } - if (num_includes) code += "\n"; - } - assert(!code_generator_cur_name_space); + assert(!code_generator_cur_name_space); - // Generate forward declarations for all structs/tables, since they may - // have circular references. - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - auto &struct_def = **it; - if (!struct_def.generated) { - CheckNameSpace(struct_def, &code); - code += "struct " + struct_def.name + ";\n\n"; + // Generate forward declarations for all structs/tables, since they may + // have circular references. + for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); + ++it) { + auto &struct_def = **it; + if (!struct_def.generated) { + CheckNameSpace(struct_def, &code); + code += "struct " + struct_def.name + ";\n\n"; + } } - } - // Generate code for all the enum declarations. - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - auto &enum_def = **it; - if (!enum_def.generated) { - CheckNameSpace(**it, &code); - GenEnum(parser, **it, &code); + // Generate code for all the enum declarations. + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + auto &enum_def = **it; + if (!enum_def.generated) { + CheckNameSpace(**it, &code); + GenEnum(parser_, **it, &code); + } } - } - // Generate code for all structs, then all tables. - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - auto &struct_def = **it; - if (struct_def.fixed && !struct_def.generated) { - CheckNameSpace(struct_def, &code); - GenStruct(parser, struct_def, &code); + // Generate code for all structs, then all tables. + for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); + ++it) { + auto &struct_def = **it; + if (struct_def.fixed && !struct_def.generated) { + CheckNameSpace(struct_def, &code); + GenStruct(parser_, struct_def, &code); + } } - } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - auto &struct_def = **it; - if (!struct_def.fixed && !struct_def.generated) { - CheckNameSpace(struct_def, &code); - GenTable(parser, struct_def, &code); + for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); + ++it) { + auto &struct_def = **it; + if (!struct_def.fixed && !struct_def.generated) { + CheckNameSpace(struct_def, &code); + GenTable(parser_, struct_def, &code); + } } - } - // Generate code for union verifiers. - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - auto &enum_def = **it; - if (enum_def.is_union && !enum_def.generated) { - CheckNameSpace(enum_def, &code); - GenEnumPost(parser, enum_def, &code); + // Generate code for union verifiers. + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + auto &enum_def = **it; + if (enum_def.is_union && !enum_def.generated) { + CheckNameSpace(enum_def, &code); + GenEnumPost(parser_, enum_def, &code); + } } - } - // Generate convenient global helper functions: - if (parser.root_struct_def_) { - CheckNameSpace(*parser.root_struct_def_, &code); - auto &name = parser.root_struct_def_->name; - std::string qualified_name = - parser.namespaces_.back()->GetFullyQualifiedName(name); - std::string cpp_qualified_name = TranslateNameSpace(qualified_name); - - // The root datatype accessor: - code += "inline const " + cpp_qualified_name + " *Get"; - code += name; - code += "(const void *buf) { return flatbuffers::GetRoot<"; - code += cpp_qualified_name + ">(buf); }\n\n"; - if (parser.opts.mutable_buffer) { - code += "inline " + name + " *GetMutable"; - code += name; - code += "(void *buf) { return flatbuffers::GetMutableRoot<"; - code += name + ">(buf); }\n\n"; - } + // Generate convenient global helper functions: + if (parser_.root_struct_def_) { + CheckNameSpace(*parser_.root_struct_def_, &code); + auto &name = parser_.root_struct_def_->name; + std::string qualified_name = + parser_.namespaces_.back()->GetFullyQualifiedName(name); + std::string cpp_qualified_name = TranslateNameSpace(qualified_name); - // The root verifier: - code += "inline bool Verify"; - code += name; - code += "Buffer(flatbuffers::Verifier &verifier) { " - "return verifier.VerifyBuffer<"; - code += cpp_qualified_name + ">(); }\n\n"; - - if (parser.file_identifier_.length()) { - // Return the identifier - code += "inline const char *" + name; - code += "Identifier() { return \"" + parser.file_identifier_; - code += "\"; }\n\n"; - - // Check if a buffer has the identifier. - code += "inline bool " + name; - code += "BufferHasIdentifier(const void *buf) { return flatbuffers::"; - code += "BufferHasIdentifier(buf, "; - code += name + "Identifier()); }\n\n"; - } + // The root datatype accessor: + code += "inline const " + cpp_qualified_name + " *Get"; + code += name; + code += "(const void *buf) { return flatbuffers::GetRoot<"; + code += cpp_qualified_name + ">(buf); }\n\n"; + if (parser_.opts.mutable_buffer) { + code += "inline " + name + " *GetMutable"; + code += name; + code += "(void *buf) { return flatbuffers::GetMutableRoot<"; + code += name + ">(buf); }\n\n"; + } - if (parser.file_extension_.length()) { - // Return the extension - code += "inline const char *" + name; - code += "Extension() { return \"" + parser.file_extension_; - code += "\"; }\n\n"; - } + // The root verifier: + code += "inline bool Verify"; + code += name; + code += + "Buffer(flatbuffers::Verifier &verifier) { " + "return verifier.VerifyBuffer<"; + code += cpp_qualified_name + ">(); }\n\n"; + + if (parser_.file_identifier_.length()) { + // Return the identifier + code += "inline const char *" + name; + code += "Identifier() { return \"" + parser_.file_identifier_; + code += "\"; }\n\n"; + + // Check if a buffer has the identifier. + code += "inline bool " + name; + code += "BufferHasIdentifier(const void *buf) { return flatbuffers::"; + code += "BufferHasIdentifier(buf, "; + code += name + "Identifier()); }\n\n"; + } - // Finish a buffer with a given root object: - code += "inline void Finish" + name; - code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<"; - code += cpp_qualified_name + "> root) { fbb.Finish(root"; - if (parser.file_identifier_.length()) - code += ", " + name + "Identifier()"; - code += "); }\n\n"; - } + if (parser_.file_extension_.length()) { + // Return the extension + code += "inline const char *" + name; + code += "Extension() { return \"" + parser_.file_extension_; + code += "\"; }\n\n"; + } - assert(code_generator_cur_name_space); - CloseNestedNameSpaces(code_generator_cur_name_space, &code); + // Finish a buffer with a given root object: + code += "inline void Finish" + name; + code += + "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<"; + code += cpp_qualified_name + "> root) { fbb.Finish(root"; + if (parser_.file_identifier_.length()) + code += ", " + name + "Identifier()"; + code += "); }\n\n"; + } - code_generator_cur_name_space = nullptr; + assert(code_generator_cur_name_space); + CloseNestedNameSpaces(code_generator_cur_name_space, &code); - // Close the include guard. - code += "\n#endif // " + include_guard + "\n"; + code_generator_cur_name_space = nullptr; - return code; -} + // Close the include guard. + code += "\n#endif // " + include_guard + "\n"; -static std::string GeneratedFileName(const std::string &path, - const std::string &file_name) { - return path + file_name + "_generated.h"; -} + return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); + } +}; +} // namespace cpp -bool GenerateCPP(const Parser &parser, - const std::string &path, +bool GenerateCPP(const Parser &parser, const std::string &path, const std::string &file_name) { - auto code = GenerateCPP(parser, file_name); - return !code.length() || - SaveFile(GeneratedFileName(path, file_name).c_str(), code, false); + cpp::CppGenerator generator(parser, path, file_name); + return generator.generate(); } std::string CPPMakeRule(const Parser &parser, diff --git a/chromium/third_party/flatbuffers/src/src/idl_gen_general.cpp b/chromium/third_party/flatbuffers/src/src/idl_gen_general.cpp index 91f1aeb6597..a0b837a20c0 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_gen_general.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_gen_general.cpp @@ -19,6 +19,7 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "flatbuffers/code_generators.h" #include <algorithm> namespace flatbuffers { @@ -1114,124 +1115,97 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += "};\n\n"; } -// Save out the generated code for a single class while adding -// declaration boilerplate. -static bool SaveClass(const LanguageParameters &lang, const Parser &parser, - const std::string &defname, const std::string &classcode, - const std::string &path, bool needs_includes, bool onefile) { - if (!classcode.length()) return true; - - std::string namespace_general; - std::string namespace_dir = path; // Either empty or ends in separator. - auto &namespaces = parser.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_general.length()) { - namespace_general += "."; - } - namespace_general += *it; - if (!onefile) { - namespace_dir += *it + kPathSeparator; +namespace general { +class GeneralGenerator : public BaseGenerator { + public: + GeneralGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + bool generate() { + assert(parser_.opts.lang <= IDLOptions::kMAX); + auto lang = language_parameters[parser_.opts.lang]; + std::string one_file_code; + + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + std::string enumcode; + GenEnum(lang, parser_, **it, &enumcode); + if (parser_.opts.one_file) { + one_file_code += enumcode; + } else { + if (!SaveType(lang, (**it).name, enumcode, false)) return false; + } } - } - EnsureDirExists(namespace_dir); - - std::string code = "// automatically generated, do not modify\n\n"; - if (!namespace_general.empty()) { - code += lang.namespace_ident + namespace_general + lang.namespace_begin; - code += "\n\n"; - } - if (needs_includes) code += lang.includes; - code += classcode; - if (!namespace_general.empty()) code += lang.namespace_end; - auto filename = namespace_dir + defname + lang.file_extension; - return SaveFile(filename.c_str(), code, false); -} - -bool GenerateGeneral(const Parser &parser, - const std::string &path, - const std::string & file_name) { - - assert(parser.opts.lang <= IDLOptions::kMAX); - auto lang = language_parameters[parser.opts.lang]; - std::string one_file_code; - - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - std::string enumcode; - GenEnum(lang, parser, **it, &enumcode); - if (parser.opts.one_file) { - one_file_code += enumcode; - } - else { - if (!SaveClass(lang, parser, (**it).name, enumcode, path, false, false)) - return false; + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + std::string declcode; + GenStruct(lang, parser_, **it, &declcode); + if (parser_.opts.one_file) { + one_file_code += declcode; + } else { + if (!SaveType(lang, (**it).name, declcode, true)) return false; + } } - } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - std::string declcode; - GenStruct(lang, parser, **it, &declcode); - if (parser.opts.one_file) { - one_file_code += declcode; + if (parser_.opts.one_file) { + return SaveType(lang, file_name_, one_file_code, true); } - else { - if (!SaveClass(lang, parser, (**it).name, declcode, path, true, false)) - return false; - } - } - - if (parser.opts.one_file) { - return SaveClass(lang, parser, file_name, one_file_code,path, true, true); + return true; } - return true; -} -static std::string ClassFileName(const LanguageParameters &lang, - const Parser &parser, const Definition &def, - const std::string &path) { - std::string namespace_general; - std::string namespace_dir = path; - auto &namespaces = parser.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_general.length()) { - namespace_general += "."; - namespace_dir += kPathSeparator; + // Save out the generated code for a single class while adding + // declaration boilerplate. + bool SaveType(const LanguageParameters &lang, const std::string &defname, + const std::string &classcode, bool needs_includes) { + if (!classcode.length()) return true; + + std::string code; + code = code + "// " + FlatBuffersGeneratedWarning(); + std::string namespace_name = FullNamespace("."); + if (!namespace_name.empty()) { + code += lang.namespace_ident + namespace_name + lang.namespace_begin; + code += "\n\n"; } - namespace_general += *it; - namespace_dir += *it; + if (needs_includes) code += lang.includes; + code += classcode; + if (!namespace_name.empty()) code += lang.namespace_end; + auto filename = namespace_dir_ + defname + lang.file_extension; + return SaveFile(filename.c_str(), code, false); } +}; +} // namespace general - return namespace_dir + kPathSeparator + def.name + lang.file_extension; +bool GenerateGeneral(const Parser &parser, const std::string &path, + const std::string &file_name) { + general::GeneralGenerator generator(parser, path, file_name); + return generator.generate(); } -std::string GeneralMakeRule(const Parser &parser, - const std::string &path, +std::string GeneralMakeRule(const Parser &parser, const std::string &path, const std::string &file_name) { assert(parser.opts.lang <= IDLOptions::kMAX); auto lang = language_parameters[parser.opts.lang]; std::string make_rule; + std::string directory = + BaseGenerator::NamespaceDir(parser, path) + kPathSeparator; - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - if (make_rule != "") - make_rule += " "; - make_rule += ClassFileName(lang, parser, **it, path); + for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); + ++it) { + if (make_rule != "") make_rule += " "; + make_rule += directory + (**it).name + lang.file_extension; } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - if (make_rule != "") - make_rule += " "; - make_rule += ClassFileName(lang, parser, **it, path); + for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); + ++it) { + if (make_rule != "") make_rule += " "; + make_rule += directory + (**it).name + lang.file_extension; } make_rule += ": "; auto included_files = parser.GetIncludedFilesRecursive(file_name); - for (auto it = included_files.begin(); - it != included_files.end(); ++it) { + for (auto it = included_files.begin(); it != included_files.end(); ++it) { make_rule += " " + *it; } return make_rule; diff --git a/chromium/third_party/flatbuffers/src/src/idl_gen_go.cpp b/chromium/third_party/flatbuffers/src/src/idl_gen_go.cpp index 88ce1f2a58c..5841e121c74 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_gen_go.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_gen_go.cpp @@ -21,6 +21,7 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "flatbuffers/code_generators.h" #ifdef _WIN32 #include <direct.h> @@ -52,20 +53,6 @@ std::string OffsetPrefix(const FieldDef &field) { "))\n\tif o != 0 {\n"; } -// Begin by declaring namespace and imports. -static void BeginFile(const std::string name_space_name, - const bool needs_imports, - std::string *code_ptr) { - std::string &code = *code_ptr; - code += "// automatically generated, do not modify\n\n"; - code += "package " + name_space_name + "\n\n"; - if (needs_imports) { - code += "import (\n"; - code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; - code += ")\n"; - } -} - // Begin a class declaration. static void BeginClass(const StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; @@ -588,32 +575,6 @@ static std::string GenMethod(const FieldDef &field) { : (IsStruct(field.value.type) ? "Struct" : "UOffsetT"); } - -// Save out the generated code for a Go Table type. -static bool SaveType(const Parser &parser, const Definition &def, - const std::string &classcode, const std::string &path, - bool needs_imports) { - if (!classcode.length()) return true; - - std::string namespace_name; - std::string namespace_dir = path; // Either empty or ends in separator. - auto &namespaces = parser.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_name.length()) { - namespace_name += "."; - } - namespace_name = *it; - namespace_dir += *it + kPathSeparator; - } - EnsureDirExists(namespace_dir); - - std::string code = ""; - BeginFile(namespace_name, needs_imports, &code); - code += classcode; - std::string filename = namespace_dir + def.name + ".go"; - return SaveFile(filename.c_str(), code, false); -} - static std::string GenTypeBasic(const Type &type) { static const char *ctypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ @@ -660,28 +621,61 @@ static void GenStructBuilder(const StructDef &struct_def, EndBuilderBody(code_ptr); } -} // namespace go +class GoGenerator : public BaseGenerator { + public: + GoGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + bool generate() { + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + std::string enumcode; + go::GenEnum(**it, &enumcode); + if (!SaveType(**it, enumcode, false)) return false; + } + + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + std::string declcode; + go::GenStruct(**it, &declcode, parser_.root_struct_def_); + if (!SaveType(**it, declcode, true)) return false; + } -bool GenerateGo(const Parser &parser, - const std::string &path, - const std::string & /*file_name*/) { - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - std::string enumcode; - go::GenEnum(**it, &enumcode); - if (!go::SaveType(parser, **it, enumcode, path, false)) - return false; + return true; } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - std::string declcode; - go::GenStruct(**it, &declcode, parser.root_struct_def_); - if (!go::SaveType(parser, **it, declcode, path, true)) - return false; + private: + // Begin by declaring namespace and imports. + void BeginFile(const std::string name_space_name, const bool needs_imports, + std::string *code_ptr) { + std::string &code = *code_ptr; + code = code + "// " + FlatBuffersGeneratedWarning(); + code += "package " + name_space_name + "\n\n"; + if (needs_imports) { + code += "import (\n"; + code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; + code += ")\n"; + } } - return true; + // Save out the generated code for a Go Table type. + bool SaveType(const Definition &def, const std::string &classcode, + bool needs_imports) { + if (!classcode.length()) return true; + + std::string code = ""; + BeginFile(LastNamespacePart(), needs_imports, &code); + code += classcode; + std::string filename = namespace_dir_ + def.name + ".go"; + return SaveFile(filename.c_str(), code, false); + } +}; +} // namespace go + +bool GenerateGo(const Parser &parser, const std::string &path, + const std::string &file_name) { + go::GoGenerator generator(parser, path, file_name); + return generator.generate(); } } // namespace flatbuffers diff --git a/chromium/third_party/flatbuffers/src/src/idl_gen_js.cpp b/chromium/third_party/flatbuffers/src/src/idl_gen_js.cpp index f824bc6883a..d724b80f44d 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_gen_js.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_gen_js.cpp @@ -19,6 +19,7 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "flatbuffers/code_generators.h" namespace flatbuffers { namespace js { @@ -661,60 +662,72 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, } // namespace js +static std::string GeneratedFileName(const std::string &path, + const std::string &file_name) { + return path + file_name + "_generated.js"; +} + +namespace js { // Iterate through all definitions we haven't generate code for (enums, structs, // and tables) and output them to a single file. -std::string GenerateJS(const Parser &parser) { - using namespace js; - - // Generate code for all the enum declarations. - std::string enum_code, exports_code; - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - GenEnum(**it, &enum_code, &exports_code); - } - - // Generate code for all structs, then all tables. - std::string decl_code; - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - GenStruct(parser, **it, &decl_code, &exports_code); - } - - // Only output file-level code if there were any declarations. - if (enum_code.length() || decl_code.length()) { - std::string code; - code = "// automatically generated by the FlatBuffers compiler," - " do not modify\n\n"; +class JsGenerator : public BaseGenerator { + public: + JsGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + // Iterate through all definitions we haven't generate code for (enums, + // structs, and tables) and output them to a single file. + bool generate() { + if (IsEverythingGenerated()) return true; + + std::string enum_code, struct_code, exports_code, code; + generateEnums(&enum_code, &exports_code); + generateStructs(&struct_code, &exports_code); + + code = code + "// " + FlatBuffersGeneratedWarning(); // Generate code for all the namespace declarations. - GenNamespaces(parser, &code, &exports_code); + GenNamespaces(parser_, &code, &exports_code); // Output the main declaration code from above. code += enum_code; - code += decl_code; + code += struct_code; - if (!exports_code.empty() && !parser.opts.skip_js_exports) { + if (!exports_code.empty() && !parser_.opts.skip_js_exports) { code += "// Exports for Node.js and RequireJS\n"; code += exports_code; } - return code; + return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); } - return std::string(); -} + private: + // Generate code for all enums. + void generateEnums(std::string *enum_code_ptr, + std::string *exports_code_ptr) { + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + auto &enum_def = **it; + GenEnum(enum_def, enum_code_ptr, exports_code_ptr); + } + } -static std::string GeneratedFileName(const std::string &path, - const std::string &file_name) { - return path + file_name + "_generated.js"; -} + // Generate code for all structs. + void generateStructs(std::string *decl_code_ptr, + std::string *exports_code_ptr) { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + auto &struct_def = **it; + GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr); + } + } +}; +} // namespace js -bool GenerateJS(const Parser &parser, - const std::string &path, +bool GenerateJS(const Parser &parser, const std::string &path, const std::string &file_name) { - auto code = GenerateJS(parser); - return !code.length() || - SaveFile(GeneratedFileName(path, file_name).c_str(), code, false); + js::JsGenerator generator(parser, path, file_name); + return generator.generate(); } std::string JSMakeRule(const Parser &parser, diff --git a/chromium/third_party/flatbuffers/src/src/idl_gen_php.cpp b/chromium/third_party/flatbuffers/src/src/idl_gen_php.cpp index 5b904a0773d..599d5571d04 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_gen_php.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_gen_php.cpp @@ -21,6 +21,7 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "flatbuffers/code_generators.h" namespace flatbuffers { namespace php { @@ -53,24 +54,6 @@ namespace php { // Hardcode spaces per indentation. const std::string Indent = " "; - // Begin by declaring namespace and imports. - static void BeginFile(const std::string name_space_name, - const bool needs_imports, - std::string *code_ptr) { - std::string &code = *code_ptr; - code += "<?php\n"; - code += "// automatically generated, do not modify\n\n"; - code += "namespace " + name_space_name + ";\n\n"; - - if (needs_imports) { - code += "use \\Google\\FlatBuffers\\Struct;\n"; - code += "use \\Google\\FlatBuffers\\Table;\n"; - code += "use \\Google\\FlatBuffers\\ByteBuffer;\n"; - code += "use \\Google\\FlatBuffers\\FlatBufferBuilder;\n"; - code += "\n"; - } - } - // Begin a class declaration. static void BeginClass(const StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; @@ -866,35 +849,6 @@ namespace php { : (IsStruct(field.value.type) ? "Struct" : "Offset"); } - - // Save out the generated code for a Php Table type. - static bool SaveType(const Parser &parser, const Definition &def, - const std::string &classcode, const std::string &path, - bool needs_imports) { - if (!classcode.length()) return true; - - std::string namespace_name; - std::string namespace_dir = path; - - auto &namespaces = parser.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_name.length()) { - namespace_name += "\\"; - namespace_dir += kPathSeparator; - } - namespace_name += *it; - namespace_dir += *it; - EnsureDirExists(namespace_dir.c_str()); - } - - std::string code = ""; - BeginFile(namespace_name, needs_imports, &code); - code += classcode; - - std::string filename = namespace_dir + kPathSeparator + def.name + ".php"; - return SaveFile(filename.c_str(), code, false); - } - static std::string GenTypeBasic(const Type &type) { static const char *ctypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ @@ -974,29 +928,76 @@ namespace php { code += Indent + "}\n"; } -} // namespace php + class PhpGenerator : public BaseGenerator { + public: + PhpGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + bool generate() { + if (!generateEnums()) return false; + if (!generateStructs()) return false; + return true; + } - bool GeneratePhp(const Parser &parser, - const std::string &path, - const std::string & /*file_name*/) { - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - std::string enumcode; - php::GenEnum(**it, &enumcode); + private: + bool generateEnums() { + for (auto it = parser_.enums_.vec.begin(); + it != parser_.enums_.vec.end(); ++it) { + auto &enum_def = **it; + std::string enumcode; + GenEnum(enum_def, &enumcode); + if (!SaveType(enum_def, enumcode, false)) return false; + } + return true; + } - if (!php::SaveType(parser, **it, enumcode, path, false)) - return false; - } + bool generateStructs() { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + auto &struct_def = **it; + std::string declcode; + GenStruct(parser_, struct_def, &declcode); + if (!SaveType(struct_def, declcode, true)) return false; + } + return true; + } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - std::string declcode; - php::GenStruct(parser, **it, &declcode); + // Begin by declaring namespace and imports. + void BeginFile(const std::string name_space_name, + const bool needs_imports, std::string *code_ptr) { + std::string &code = *code_ptr; + code += "<?php\n"; + code = code + "// " + FlatBuffersGeneratedWarning(); + code += "namespace " + name_space_name + ";\n\n"; + + if (needs_imports) { + code += "use \\Google\\FlatBuffers\\Struct;\n"; + code += "use \\Google\\FlatBuffers\\Table;\n"; + code += "use \\Google\\FlatBuffers\\ByteBuffer;\n"; + code += "use \\Google\\FlatBuffers\\FlatBufferBuilder;\n"; + code += "\n"; + } + } - if (!php::SaveType(parser, **it, declcode, path, true)) - return false; - } + // Save out the generated code for a Php Table type. + bool SaveType(const Definition &def, const std::string &classcode, + bool needs_imports) { + if (!classcode.length()) return true; + + std::string code = ""; + BeginFile(FullNamespace("\\"), needs_imports, &code); + code += classcode; - return true; -} -} // namespace flatbuffers + std::string filename = + namespace_dir_ + kPathSeparator + def.name + ".php"; + return SaveFile(filename.c_str(), code, false); + } + }; + } // namespace php + + bool GeneratePhp(const Parser &parser, const std::string &path, + const std::string &file_name) { + php::PhpGenerator generator(parser, path, file_name); + return generator.generate(); + } + } // namespace flatbuffers diff --git a/chromium/third_party/flatbuffers/src/src/idl_gen_python.cpp b/chromium/third_party/flatbuffers/src/src/idl_gen_python.cpp index 7cd09cfcdc9..7b8d168c1d9 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_gen_python.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_gen_python.cpp @@ -21,6 +21,7 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "flatbuffers/code_generators.h" namespace flatbuffers { namespace python { @@ -48,18 +49,6 @@ std::string OffsetPrefix(const FieldDef &field) { "))\n" + Indent + Indent + "if o != 0:\n"; } -// Begin by declaring namespace and imports. -static void BeginFile(const std::string name_space_name, - const bool needs_imports, - std::string *code_ptr) { - std::string &code = *code_ptr; - code += "# automatically generated, do not modify\n\n"; - code += "# namespace: " + name_space_name + "\n\n"; - if (needs_imports) { - code += "import flatbuffers\n\n"; - } -} - // Begin a class declaration. static void BeginClass(const StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; @@ -557,37 +546,6 @@ static std::string GenMethod(const FieldDef &field) { : (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative"); } - -// Save out the generated code for a Python Table type. -static bool SaveType(const Parser &parser, const Definition &def, - const std::string &classcode, const std::string &path, - bool needs_imports) { - if (!classcode.length()) return true; - - std::string namespace_name; - std::string namespace_dir = path; - auto &namespaces = parser.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_name.length()) { - namespace_name += "."; - namespace_dir += kPathSeparator; - } - namespace_name = *it; - namespace_dir += *it; - EnsureDirExists(namespace_dir.c_str()); - - std::string init_py_filename = namespace_dir + "/__init__.py"; - SaveFile(init_py_filename.c_str(), "", false); - } - - - std::string code = ""; - BeginFile(namespace_name, needs_imports, &code); - code += classcode; - std::string filename = namespace_dir + kPathSeparator + def.name + ".py"; - return SaveFile(filename.c_str(), code, false); -} - static std::string GenTypeBasic(const Type &type) { static const char *ctypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ @@ -634,28 +592,79 @@ static void GenStructBuilder(const StructDef &struct_def, EndBuilderBody(code_ptr); } -} // namespace python +class PythonGenerator : public BaseGenerator { + public: + PythonGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + bool generate() { + if (!generateEnums()) return false; + if (!generateStructs()) return false; + return true; + } -bool GeneratePython(const Parser &parser, - const std::string &path, - const std::string & /*file_name*/) { - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - std::string enumcode; - python::GenEnum(**it, &enumcode); - if (!python::SaveType(parser, **it, enumcode, path, false)) - return false; + private: + bool generateEnums() { + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + auto &enum_def = **it; + std::string enumcode; + GenEnum(enum_def, &enumcode); + if (!SaveType(enum_def, enumcode, false)) return false; + } + return true; } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - std::string declcode; - python::GenStruct(**it, &declcode, parser.root_struct_def_); - if (!python::SaveType(parser, **it, declcode, path, true)) - return false; + bool generateStructs() { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + auto &struct_def = **it; + std::string declcode; + GenStruct(struct_def, &declcode, parser_.root_struct_def_); + if (!SaveType(struct_def, declcode, true)) return false; + } + return true; + } + + // Begin by declaring namespace and imports. + void BeginFile(const std::string name_space_name, const bool needs_imports, + std::string *code_ptr) { + std::string &code = *code_ptr; + code = code + "# " + FlatBuffersGeneratedWarning(); + code += "# namespace: " + name_space_name + "\n\n"; + if (needs_imports) { + code += "import flatbuffers\n\n"; + } } - return true; + // Save out the generated code for a Python Table type. + bool SaveType(const Definition &def, const std::string &classcode, + bool needs_imports) { + if (!classcode.length()) return true; + + std::string namespace_dir = path_; + auto &namespaces = parser_.namespaces_.back()->components; + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { + if (it != namespaces.begin()) namespace_dir += kPathSeparator; + namespace_dir += *it; + std::string init_py_filename = namespace_dir + "/__init__.py"; + SaveFile(init_py_filename.c_str(), "", false); + } + + std::string code = ""; + BeginFile(LastNamespacePart(), needs_imports, &code); + code += classcode; + std::string filename = namespace_dir_ + kPathSeparator + def.name + ".py"; + return SaveFile(filename.c_str(), code, false); + } +}; + +} // namespace python + +bool GeneratePython(const Parser &parser, const std::string &path, + const std::string &file_name) { + python::PythonGenerator generator(parser, path, file_name); + return generator.generate(); } } // namespace flatbuffers diff --git a/chromium/third_party/flatbuffers/src/src/idl_parser.cpp b/chromium/third_party/flatbuffers/src/src/idl_parser.cpp index 28fd6a3773d..f5badab16b9 100644 --- a/chromium/third_party/flatbuffers/src/src/idl_parser.cpp +++ b/chromium/third_party/flatbuffers/src/src/idl_parser.cpp @@ -69,13 +69,16 @@ inline CheckedError NoError() { return CheckedError(false); } // Ensure that integer values we parse fit inside the declared integer type. CheckedError Parser::CheckBitsFit(int64_t val, size_t bits) { - // Bits we allow to be used. - auto mask = static_cast<int64_t>((1ull << bits) - 1); - if (bits < 64 && - (val & ~mask) != 0 && // Positive or unsigned. - (val | mask) != -1) // Negative. - return Error("constant does not fit in a " + NumToString(bits) + - "-bit field"); + // Left-shifting a 64-bit value by 64 bits or more is undefined + // behavior (C99 6.5.7), so check *before* we shift. + if (bits < 64) { + // Bits we allow to be used. + auto mask = static_cast<int64_t>((1ull << bits) - 1); + if ((val & ~mask) != 0 && // Positive or unsigned. + (val | mask) != -1) // Negative. + return Error("constant does not fit in a " + NumToString(bits) + + "-bit field"); + } return NoError(); } @@ -205,8 +208,10 @@ CheckedError Parser::ParseHexNum(int nibbles, int64_t *val) { CheckedError Parser::SkipByteOrderMark() { if (static_cast<unsigned char>(*cursor_) != 0xef) return NoError(); cursor_++; - if (static_cast<unsigned char>(*cursor_++) != 0xbb) return Error("invalid utf-8 byte order mark"); - if (static_cast<unsigned char>(*cursor_++) != 0xbf) return Error("invalid utf-8 byte order mark"); + if (static_cast<unsigned char>(*cursor_) != 0xbb) return Error("invalid utf-8 byte order mark"); + cursor_++; + if (static_cast<unsigned char>(*cursor_) != 0xbf) return Error("invalid utf-8 byte order mark"); + cursor_++; return NoError(); } @@ -231,12 +236,19 @@ CheckedError Parser::Next() { if(!isdigit(static_cast<const unsigned char>(*cursor_))) return NoError(); return Error("floating point constant can\'t start with \".\""); case '\"': - case '\'': + case '\'': { + int unicode_high_surrogate = -1; + while (*cursor_ != c) { if (*cursor_ < ' ' && *cursor_ >= 0) return Error("illegal character in string constant"); if (*cursor_ == '\\') { cursor_++; + if (unicode_high_surrogate != -1 && + *cursor_ != 'u') { + return Error( + "illegal Unicode sequence (unpaired high surrogate)"); + } switch (*cursor_) { case 'n': attribute_ += '\n'; cursor_++; break; case 't': attribute_ += '\t'; cursor_++; break; @@ -258,18 +270,51 @@ CheckedError Parser::Next() { cursor_++; int64_t val; ECHECK(ParseHexNum(4, &val)); - ToUTF8(static_cast<int>(val), &attribute_); + if (val >= 0xD800 && val <= 0xDBFF) { + if (unicode_high_surrogate != -1) { + return Error( + "illegal Unicode sequence (multiple high surrogates)"); + } else { + unicode_high_surrogate = static_cast<int>(val); + } + } else if (val >= 0xDC00 && val <= 0xDFFF) { + if (unicode_high_surrogate == -1) { + return Error( + "illegal Unicode sequence (unpaired low surrogate)"); + } else { + int code_point = 0x10000 + + ((unicode_high_surrogate & 0x03FF) << 10) + + (val & 0x03FF); + ToUTF8(code_point, &attribute_); + unicode_high_surrogate = -1; + } + } else { + if (unicode_high_surrogate != -1) { + return Error( + "illegal Unicode sequence (unpaired high surrogate)"); + } + ToUTF8(static_cast<int>(val), &attribute_); + } break; } default: return Error("unknown escape code in string constant"); } } else { // printable chars + UTF-8 bytes + if (unicode_high_surrogate != -1) { + return Error( + "illegal Unicode sequence (unpaired high surrogate)"); + } attribute_ += *cursor_++; } } + if (unicode_high_surrogate != -1) { + return Error( + "illegal Unicode sequence (unpaired high surrogate)"); + } cursor_++; token_ = kTokenStringConstant; return NoError(); + } case '/': if (*cursor_ == '/') { const char *start = ++cursor_; @@ -543,10 +588,10 @@ CheckedError Parser::ParseField(StructDef &struct_def) { if (!IsScalar(type.base_type)) return Error("default values currently only supported for scalars"); ECHECK(ParseSingleValue(field->value)); - if (IsFloat(field->value.type.base_type)) { - if (!strpbrk(field->value.constant.c_str(), ".eE")) - field->value.constant += ".0"; - } + } + if (IsFloat(field->value.type.base_type)) { + if (!strpbrk(field->value.constant.c_str(), ".eE")) + field->value.constant += ".0"; } if (type.enum_def && @@ -1956,14 +2001,14 @@ flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset< Definition::SerializeAttributes(FlatBufferBuilder *builder, const Parser &parser) const { std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs; - for (auto kv : attributes.dict) { - auto it = parser.known_attributes_.find(kv.first); + for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) { + auto it = parser.known_attributes_.find(kv->first); assert(it != parser.known_attributes_.end()); if (!it->second) { // Custom attribute. attrs.push_back( - reflection::CreateKeyValue(*builder, builder->CreateString(kv.first), + reflection::CreateKeyValue(*builder, builder->CreateString(kv->first), builder->CreateString( - kv.second->constant))); + kv->second->constant))); } } if (attrs.size()) { |