mirror of
https://github.com/zebrajr/tensorflow.git
synced 2025-12-06 00:19:58 +01:00
Automated Code Change
PiperOrigin-RevId: 826451988
This commit is contained in:
parent
fe344908fa
commit
4d78e8088a
|
|
@ -45,8 +45,8 @@ static const char kPlaceholderFile[] =
|
||||||
"tensorflow/tools/proto_text/placeholder.txt";
|
"tensorflow/tools/proto_text/placeholder.txt";
|
||||||
|
|
||||||
bool IsPlaceholderFile(const char* s) {
|
bool IsPlaceholderFile(const char* s) {
|
||||||
string ph(kPlaceholderFile);
|
std::string ph(kPlaceholderFile);
|
||||||
string str(s);
|
std::string str(s);
|
||||||
return str.size() >= strlen(kPlaceholderFile) &&
|
return str.size() >= strlen(kPlaceholderFile) &&
|
||||||
ph == str.substr(str.size() - ph.size());
|
ph == str.substr(str.size() - ph.size());
|
||||||
}
|
}
|
||||||
|
|
@ -76,14 +76,15 @@ int MainImpl(int argc, char** argv) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string output_root = argv[1];
|
const std::string output_root = argv[1];
|
||||||
const string output_relative_path = kTensorFlowHeaderPrefix + string(argv[2]);
|
const std::string output_relative_path =
|
||||||
|
kTensorFlowHeaderPrefix + std::string(argv[2]);
|
||||||
|
|
||||||
string src_relative_path;
|
std::string src_relative_path;
|
||||||
bool has_placeholder = false;
|
bool has_placeholder = false;
|
||||||
for (int i = 3; i < argc; ++i) {
|
for (int i = 3; i < argc; ++i) {
|
||||||
if (IsPlaceholderFile(argv[i])) {
|
if (IsPlaceholderFile(argv[i])) {
|
||||||
const string s(argv[i]);
|
const std::string s(argv[i]);
|
||||||
src_relative_path = s.substr(0, s.size() - strlen(kPlaceholderFile));
|
src_relative_path = s.substr(0, s.size() - strlen(kPlaceholderFile));
|
||||||
has_placeholder = true;
|
has_placeholder = true;
|
||||||
}
|
}
|
||||||
|
|
@ -102,13 +103,14 @@ int MainImpl(int argc, char** argv) {
|
||||||
|
|
||||||
for (int i = 3; i < argc; i++) {
|
for (int i = 3; i < argc; i++) {
|
||||||
if (IsPlaceholderFile(argv[i])) continue;
|
if (IsPlaceholderFile(argv[i])) continue;
|
||||||
const string proto_path = string(argv[i]).substr(src_relative_path.size());
|
const std::string proto_path =
|
||||||
|
std::string(argv[i]).substr(src_relative_path.size());
|
||||||
|
|
||||||
const tensorflow::protobuf::FileDescriptor* fd =
|
const tensorflow::protobuf::FileDescriptor* fd =
|
||||||
importer.Import(proto_path);
|
importer.Import(proto_path);
|
||||||
|
|
||||||
const int index = proto_path.find_last_of('.');
|
const int index = proto_path.find_last_of('.');
|
||||||
string proto_path_no_suffix = proto_path.substr(0, index);
|
std::string proto_path_no_suffix = proto_path.substr(0, index);
|
||||||
|
|
||||||
proto_path_no_suffix =
|
proto_path_no_suffix =
|
||||||
proto_path_no_suffix.substr(output_relative_path.size());
|
proto_path_no_suffix.substr(output_relative_path.size());
|
||||||
|
|
@ -118,8 +120,8 @@ int MainImpl(int argc, char** argv) {
|
||||||
|
|
||||||
// Three passes, one for each output file.
|
// Three passes, one for each output file.
|
||||||
for (int pass = 0; pass < 3; ++pass) {
|
for (int pass = 0; pass < 3; ++pass) {
|
||||||
string suffix;
|
std::string suffix;
|
||||||
string data;
|
std::string data;
|
||||||
if (pass == 0) {
|
if (pass == 0) {
|
||||||
suffix = ".pb_text.h";
|
suffix = ".pb_text.h";
|
||||||
data = code.header;
|
data = code.header;
|
||||||
|
|
@ -131,7 +133,8 @@ int MainImpl(int argc, char** argv) {
|
||||||
data = code.cc;
|
data = code.cc;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string path = output_root + "/" + proto_path_no_suffix + suffix;
|
const std::string path =
|
||||||
|
output_root + "/" + proto_path_no_suffix + suffix;
|
||||||
FILE* f = fopen(path.c_str(), "w");
|
FILE* f = fopen(path.c_str(), "w");
|
||||||
if (f == nullptr) {
|
if (f == nullptr) {
|
||||||
// We don't expect this output to be generated. It was specified in the
|
// We don't expect this output to be generated. It was specified in the
|
||||||
|
|
|
||||||
|
|
@ -36,14 +36,14 @@ namespace tensorflow {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
string StrCat(const Args&... args) {
|
std::string StrCat(const Args&... args) {
|
||||||
std::ostringstream s;
|
std::ostringstream s;
|
||||||
std::vector<int> give_me_a_name{((s << args), 0)...};
|
std::vector<int> give_me_a_name{((s << args), 0)...};
|
||||||
return s.str();
|
return s.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
string StrAppend(string* to_append, const Args&... args) {
|
std::string StrAppend(std::string* to_append, const Args&... args) {
|
||||||
*to_append += StrCat(args...);
|
*to_append += StrCat(args...);
|
||||||
return *to_append;
|
return *to_append;
|
||||||
}
|
}
|
||||||
|
|
@ -60,7 +60,7 @@ string StrAppend(string* to_append, const Args&... args) {
|
||||||
// the field names (it's a loop over all names), and tracking of has_seen.
|
// the field names (it's a loop over all names), and tracking of has_seen.
|
||||||
class Generator {
|
class Generator {
|
||||||
public:
|
public:
|
||||||
explicit Generator(const string& tf_header_prefix)
|
explicit Generator(const std::string& tf_header_prefix)
|
||||||
: tf_header_prefix_(tf_header_prefix),
|
: tf_header_prefix_(tf_header_prefix),
|
||||||
header_(&code_.header),
|
header_(&code_.header),
|
||||||
header_impl_(&code_.header_impl),
|
header_impl_(&code_.header_impl),
|
||||||
|
|
@ -73,9 +73,9 @@ class Generator {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Section {
|
struct Section {
|
||||||
explicit Section(string* str) : str(str) {}
|
explicit Section(std::string* str) : str(str) {}
|
||||||
string* str;
|
std::string* str;
|
||||||
string indent;
|
std::string indent;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Switches the currently active section to <section>.
|
// Switches the currently active section to <section>.
|
||||||
|
|
@ -110,7 +110,7 @@ class Generator {
|
||||||
// <field_expr> is code that when emitted yields the field's value.
|
// <field_expr> is code that when emitted yields the field's value.
|
||||||
void AppendFieldValueAppend(const FieldDescriptor& field,
|
void AppendFieldValueAppend(const FieldDescriptor& field,
|
||||||
const bool omit_default,
|
const bool omit_default,
|
||||||
const string& field_expr);
|
const std::string& field_expr);
|
||||||
|
|
||||||
// Appends the print code for as single field.
|
// Appends the print code for as single field.
|
||||||
void AppendFieldAppend(const FieldDescriptor& field);
|
void AppendFieldAppend(const FieldDescriptor& field);
|
||||||
|
|
@ -135,17 +135,17 @@ class Generator {
|
||||||
void AddNamespaceToCurrentSection(absl::string_view package, bool open);
|
void AddNamespaceToCurrentSection(absl::string_view package, bool open);
|
||||||
|
|
||||||
// Appends the given headers as sorted #include lines.
|
// Appends the given headers as sorted #include lines.
|
||||||
void AddHeadersToCurrentSection(const std::vector<string>& headers);
|
void AddHeadersToCurrentSection(const std::vector<std::string>& headers);
|
||||||
|
|
||||||
// When adding #includes for tensorflow headers, prefix them with this.
|
// When adding #includes for tensorflow headers, prefix them with this.
|
||||||
const string tf_header_prefix_;
|
const std::string tf_header_prefix_;
|
||||||
ProtoTextFunctionCode code_;
|
ProtoTextFunctionCode code_;
|
||||||
Section* cur_ = nullptr;
|
Section* cur_ = nullptr;
|
||||||
Section header_;
|
Section header_;
|
||||||
Section header_impl_;
|
Section header_impl_;
|
||||||
Section cc_;
|
Section cc_;
|
||||||
|
|
||||||
std::unordered_set<string> map_append_signatures_included_;
|
std::unordered_set<std::string> map_append_signatures_included_;
|
||||||
|
|
||||||
Generator(const Generator&) = delete;
|
Generator(const Generator&) = delete;
|
||||||
void operator=(const Generator&) = delete;
|
void operator=(const Generator&) = delete;
|
||||||
|
|
@ -153,8 +153,8 @@ class Generator {
|
||||||
|
|
||||||
// Returns the prefix needed to reference objects defined in <fd>. E.g.
|
// Returns the prefix needed to reference objects defined in <fd>. E.g.
|
||||||
// "::tensorflow::test".
|
// "::tensorflow::test".
|
||||||
string GetPackageReferencePrefix(const FileDescriptor* fd) {
|
std::string GetPackageReferencePrefix(const FileDescriptor* fd) {
|
||||||
string result = "::";
|
std::string result = "::";
|
||||||
absl::string_view package = fd->package();
|
absl::string_view package = fd->package();
|
||||||
for (size_t i = 0; i < package.size(); ++i) {
|
for (size_t i = 0; i < package.size(); ++i) {
|
||||||
if (package[i] == '.') {
|
if (package[i] == '.') {
|
||||||
|
|
@ -168,67 +168,67 @@ string GetPackageReferencePrefix(const FileDescriptor* fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the name of the class generated by proto to represent <d>.
|
// Returns the name of the class generated by proto to represent <d>.
|
||||||
string GetClassName(const Descriptor& d) {
|
std::string GetClassName(const Descriptor& d) {
|
||||||
if (d.containing_type() == nullptr) return std::string(d.name());
|
if (d.containing_type() == nullptr) return std::string(d.name());
|
||||||
return StrCat(GetClassName(*d.containing_type()), "_", d.name());
|
return StrCat(GetClassName(*d.containing_type()), "_", d.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the name of the class generated by proto to represent <ed>.
|
// Returns the name of the class generated by proto to represent <ed>.
|
||||||
string GetClassName(const EnumDescriptor& ed) {
|
std::string GetClassName(const EnumDescriptor& ed) {
|
||||||
if (ed.containing_type() == nullptr) return std::string(ed.name());
|
if (ed.containing_type() == nullptr) return std::string(ed.name());
|
||||||
return StrCat(GetClassName(*ed.containing_type()), "_", ed.name());
|
return StrCat(GetClassName(*ed.containing_type()), "_", ed.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the qualified name that refers to the class generated by proto to
|
// Returns the qualified name that refers to the class generated by proto to
|
||||||
// represent <d>.
|
// represent <d>.
|
||||||
string GetQualifiedName(const Descriptor& d) {
|
std::string GetQualifiedName(const Descriptor& d) {
|
||||||
return StrCat(GetPackageReferencePrefix(d.file()), GetClassName(d));
|
return StrCat(GetPackageReferencePrefix(d.file()), GetClassName(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the qualified name that refers to the class generated by proto to
|
// Returns the qualified name that refers to the class generated by proto to
|
||||||
// represent <ed>.
|
// represent <ed>.
|
||||||
string GetQualifiedName(const EnumDescriptor& d) {
|
std::string GetQualifiedName(const EnumDescriptor& d) {
|
||||||
return StrCat(GetPackageReferencePrefix(d.file()), GetClassName(d));
|
return StrCat(GetPackageReferencePrefix(d.file()), GetClassName(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the qualified name that refers to the generated
|
// Returns the qualified name that refers to the generated
|
||||||
// AppendProtoDebugString function for <d>.
|
// AppendProtoDebugString function for <d>.
|
||||||
string GetQualifiedAppendFn(const Descriptor& d) {
|
std::string GetQualifiedAppendFn(const Descriptor& d) {
|
||||||
return StrCat(GetPackageReferencePrefix(d.file()),
|
return StrCat(GetPackageReferencePrefix(d.file()),
|
||||||
"internal::AppendProtoDebugString");
|
"internal::AppendProtoDebugString");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the name of the generated function that returns an enum value's
|
// Returns the name of the generated function that returns an enum value's
|
||||||
// string value.
|
// string value.
|
||||||
string GetEnumNameFn(const EnumDescriptor& enum_d) {
|
std::string GetEnumNameFn(const EnumDescriptor& enum_d) {
|
||||||
return StrCat("EnumName_", GetClassName(enum_d));
|
return StrCat("EnumName_", GetClassName(enum_d));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the qualified name of the function returned by GetEnumNameFn().
|
// Returns the qualified name of the function returned by GetEnumNameFn().
|
||||||
string GetQualifiedEnumNameFn(const EnumDescriptor& enum_d) {
|
std::string GetQualifiedEnumNameFn(const EnumDescriptor& enum_d) {
|
||||||
return StrCat(GetPackageReferencePrefix(enum_d.file()),
|
return StrCat(GetPackageReferencePrefix(enum_d.file()),
|
||||||
GetEnumNameFn(enum_d));
|
GetEnumNameFn(enum_d));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the name of a generated header file, either the public api (if impl
|
// Returns the name of a generated header file, either the public api (if impl
|
||||||
// is false) or the internal implementation header (if impl is true).
|
// is false) or the internal implementation header (if impl is true).
|
||||||
string GetProtoTextHeaderName(const FileDescriptor& fd, bool impl) {
|
std::string GetProtoTextHeaderName(const FileDescriptor& fd, bool impl) {
|
||||||
const int dot_index = fd.name().find_last_of('.');
|
const int dot_index = fd.name().find_last_of('.');
|
||||||
return StrCat(fd.name().substr(0, dot_index),
|
return StrCat(fd.name().substr(0, dot_index),
|
||||||
(impl ? ".pb_text-impl.h" : ".pb_text.h"));
|
(impl ? ".pb_text-impl.h" : ".pb_text.h"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the name of the header generated by the proto library for <fd>.
|
// Returns the name of the header generated by the proto library for <fd>.
|
||||||
string GetProtoHeaderName(const FileDescriptor& fd) {
|
std::string GetProtoHeaderName(const FileDescriptor& fd) {
|
||||||
const int dot_index = fd.name().find_last_of('.');
|
const int dot_index = fd.name().find_last_of('.');
|
||||||
return StrCat(fd.name().substr(0, dot_index), ".pb.h");
|
return StrCat(fd.name().substr(0, dot_index), ".pb.h");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the C++ class name for the given proto field.
|
// Returns the C++ class name for the given proto field.
|
||||||
string GetCppClass(const FieldDescriptor& d) {
|
std::string GetCppClass(const FieldDescriptor& d) {
|
||||||
string cpp_class = d.cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE
|
std::string cpp_class = d.cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE
|
||||||
? GetQualifiedName(*d.message_type())
|
? GetQualifiedName(*d.message_type())
|
||||||
: std::string(d.cpp_type_name());
|
: std::string(d.cpp_type_name());
|
||||||
|
|
||||||
// In open-source TensorFlow, the definition of int64 varies across
|
// In open-source TensorFlow, the definition of int64 varies across
|
||||||
// platforms. The following line, which is manipulated during internal-
|
// platforms. The following line, which is manipulated during internal-
|
||||||
|
|
@ -243,8 +243,8 @@ string GetCppClass(const FieldDescriptor& d) {
|
||||||
// Returns the string that can be used for a header guard for the generated
|
// Returns the string that can be used for a header guard for the generated
|
||||||
// headers for <fd>, either for the public api (if impl is false) or the
|
// headers for <fd>, either for the public api (if impl is false) or the
|
||||||
// internal implementation header (if impl is true).
|
// internal implementation header (if impl is true).
|
||||||
string GetHeaderGuard(const FileDescriptor& fd, bool impl) {
|
std::string GetHeaderGuard(const FileDescriptor& fd, bool impl) {
|
||||||
string s(fd.name());
|
std::string s(fd.name());
|
||||||
std::replace(s.begin(), s.end(), '/', '_');
|
std::replace(s.begin(), s.end(), '/', '_');
|
||||||
std::replace(s.begin(), s.end(), '.', '_');
|
std::replace(s.begin(), s.end(), '.', '_');
|
||||||
return s + (impl ? "_IMPL_H_" : "_H_");
|
return s + (impl ? "_IMPL_H_" : "_H_");
|
||||||
|
|
@ -252,7 +252,7 @@ string GetHeaderGuard(const FileDescriptor& fd, bool impl) {
|
||||||
|
|
||||||
void Generator::AppendFieldValueAppend(const FieldDescriptor& field,
|
void Generator::AppendFieldValueAppend(const FieldDescriptor& field,
|
||||||
const bool omit_default,
|
const bool omit_default,
|
||||||
const string& field_expr) {
|
const std::string& field_expr) {
|
||||||
// This does not emit code with proper presence semantics (e.g. it doesn't
|
// This does not emit code with proper presence semantics (e.g. it doesn't
|
||||||
// check 'has' fields on non-messages).
|
// check 'has' fields on non-messages).
|
||||||
CHECK(!field.has_presence() || field.containing_oneof() != nullptr ||
|
CHECK(!field.has_presence() || field.containing_oneof() != nullptr ||
|
||||||
|
|
@ -351,7 +351,7 @@ void Generator::AppendFieldAppend(const FieldDescriptor& field) {
|
||||||
} else {
|
} else {
|
||||||
const auto* oneof = field.containing_oneof();
|
const auto* oneof = field.containing_oneof();
|
||||||
if (oneof != nullptr) {
|
if (oneof != nullptr) {
|
||||||
string camel_name(field.camelcase_name());
|
std::string camel_name(field.camelcase_name());
|
||||||
camel_name[0] = toupper(camel_name[0]);
|
camel_name[0] = toupper(camel_name[0]);
|
||||||
Print("if (msg.", oneof->name(), "_case() == ",
|
Print("if (msg.", oneof->name(), "_case() == ",
|
||||||
GetQualifiedName(*oneof->containing_type()), "::k", camel_name,
|
GetQualifiedName(*oneof->containing_type()), "::k", camel_name,
|
||||||
|
|
@ -369,10 +369,11 @@ void Generator::AppendFieldAppend(const FieldDescriptor& field) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Generator::AppendEnumFunctions(const EnumDescriptor& enum_d) {
|
void Generator::AppendEnumFunctions(const EnumDescriptor& enum_d) {
|
||||||
const string sig = StrCat("const char* ", GetEnumNameFn(enum_d), "(\n ",
|
const std::string sig =
|
||||||
GetQualifiedName(enum_d), " value)");
|
StrCat("const char* ", GetEnumNameFn(enum_d), "(\n ",
|
||||||
|
GetQualifiedName(enum_d), " value)");
|
||||||
SetOutput(&header_);
|
SetOutput(&header_);
|
||||||
Print().Print("// Enum text output for ", string(enum_d.full_name()));
|
Print().Print("// Enum text output for ", std::string(enum_d.full_name()));
|
||||||
Print(sig, ";");
|
Print(sig, ";");
|
||||||
|
|
||||||
SetOutput(&cc_);
|
SetOutput(&cc_);
|
||||||
|
|
@ -389,7 +390,7 @@ void Generator::AppendEnumFunctions(const EnumDescriptor& enum_d) {
|
||||||
|
|
||||||
void Generator::AppendParseMessageFunction(const Descriptor& md) {
|
void Generator::AppendParseMessageFunction(const Descriptor& md) {
|
||||||
const bool map_append = (md.options().map_entry());
|
const bool map_append = (md.options().map_entry());
|
||||||
string sig;
|
std::string sig;
|
||||||
if (!map_append) {
|
if (!map_append) {
|
||||||
sig = StrCat("bool ProtoParseFromString(\n const string& s,\n ",
|
sig = StrCat("bool ProtoParseFromString(\n const string& s,\n ",
|
||||||
GetQualifiedName(md), "* msg)");
|
GetQualifiedName(md), "* msg)");
|
||||||
|
|
@ -476,8 +477,8 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
|
||||||
for (int i = 0; i < md.field_count(); ++i) {
|
for (int i = 0; i < md.field_count(); ++i) {
|
||||||
const FieldDescriptor* field = md.field(i);
|
const FieldDescriptor* field = md.field(i);
|
||||||
absl::string_view field_name = field->name();
|
absl::string_view field_name = field->name();
|
||||||
string mutable_value_expr;
|
std::string mutable_value_expr;
|
||||||
string set_value_prefix;
|
std::string set_value_prefix;
|
||||||
if (map_append) {
|
if (map_append) {
|
||||||
mutable_value_expr = StrCat("&map_", field_name);
|
mutable_value_expr = StrCat("&map_", field_name);
|
||||||
set_value_prefix = StrCat("map_", field_name, " = ");
|
set_value_prefix = StrCat("map_", field_name, " = ");
|
||||||
|
|
@ -551,7 +552,7 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
|
||||||
"Scanner::LETTER_DIGIT_DASH_UNDERSCORE)."
|
"Scanner::LETTER_DIGIT_DASH_UNDERSCORE)."
|
||||||
"GetResult(nullptr, &value)) return false;");
|
"GetResult(nullptr, &value)) return false;");
|
||||||
const auto* enum_d = field->enum_type();
|
const auto* enum_d = field->enum_type();
|
||||||
string value_prefix;
|
std::string value_prefix;
|
||||||
if (enum_d->containing_type() == nullptr) {
|
if (enum_d->containing_type() == nullptr) {
|
||||||
value_prefix = GetPackageReferencePrefix(enum_d->file());
|
value_prefix = GetPackageReferencePrefix(enum_d->file());
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -561,7 +562,7 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
|
||||||
for (int enum_i = 0; enum_i < enum_d->value_count(); ++enum_i) {
|
for (int enum_i = 0; enum_i < enum_d->value_count(); ++enum_i) {
|
||||||
const auto* value_d = enum_d->value(enum_i);
|
const auto* value_d = enum_d->value(enum_i);
|
||||||
absl::string_view value_name = value_d->name();
|
absl::string_view value_name = value_d->name();
|
||||||
string condition = StrCat("value == \"", value_name, "\"");
|
std::string condition = StrCat("value == \"", value_name, "\"");
|
||||||
|
|
||||||
Print(enum_i == 0 ? "" : "} else ", "if (", condition, ") {");
|
Print(enum_i == 0 ? "" : "} else ", "if (", condition, ") {");
|
||||||
Nest();
|
Nest();
|
||||||
|
|
@ -628,14 +629,14 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
|
||||||
void Generator::AppendDebugStringFunctions(const Descriptor& md) {
|
void Generator::AppendDebugStringFunctions(const Descriptor& md) {
|
||||||
SetOutput(&header_impl_).Print();
|
SetOutput(&header_impl_).Print();
|
||||||
SetOutput(&header_).Print().Print("// Message-text conversion for ",
|
SetOutput(&header_).Print().Print("// Message-text conversion for ",
|
||||||
string(md.full_name()));
|
std::string(md.full_name()));
|
||||||
|
|
||||||
// Append the two debug string functions for <md>.
|
// Append the two debug string functions for <md>.
|
||||||
for (int short_pass = 0; short_pass < 2; ++short_pass) {
|
for (int short_pass = 0; short_pass < 2; ++short_pass) {
|
||||||
const bool short_debug = (short_pass == 1);
|
const bool short_debug = (short_pass == 1);
|
||||||
|
|
||||||
// Make the Get functions.
|
// Make the Get functions.
|
||||||
const string sig = StrCat(
|
const std::string sig = StrCat(
|
||||||
"string ", short_debug ? "ProtoShortDebugString" : "ProtoDebugString",
|
"string ", short_debug ? "ProtoShortDebugString" : "ProtoDebugString",
|
||||||
"(\n const ", GetQualifiedName(md), "& msg)");
|
"(\n const ", GetQualifiedName(md), "& msg)");
|
||||||
SetOutput(&header_).Print(sig, ";");
|
SetOutput(&header_).Print(sig, ";");
|
||||||
|
|
@ -652,7 +653,7 @@ void Generator::AppendDebugStringFunctions(const Descriptor& md) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the Append function.
|
// Make the Append function.
|
||||||
const string sig =
|
const std::string sig =
|
||||||
StrCat("void AppendProtoDebugString(\n",
|
StrCat("void AppendProtoDebugString(\n",
|
||||||
" ::tensorflow::strings::ProtoTextOutput* o,\n const ",
|
" ::tensorflow::strings::ProtoTextOutput* o,\n const ",
|
||||||
GetQualifiedName(md), "& msg)");
|
GetQualifiedName(md), "& msg)");
|
||||||
|
|
@ -703,7 +704,7 @@ void Generator::AppendMessageFunctions(const Descriptor& md) {
|
||||||
void Generator::AddNamespaceToCurrentSection(absl::string_view package,
|
void Generator::AddNamespaceToCurrentSection(absl::string_view package,
|
||||||
bool open) {
|
bool open) {
|
||||||
Print();
|
Print();
|
||||||
std::vector<string> parts = {""};
|
std::vector<std::string> parts = {""};
|
||||||
for (size_t i = 0; i < package.size(); ++i) {
|
for (size_t i = 0; i < package.size(); ++i) {
|
||||||
if (package[i] == '.') {
|
if (package[i] == '.') {
|
||||||
parts.resize(parts.size() + 1);
|
parts.resize(parts.size() + 1);
|
||||||
|
|
@ -722,8 +723,9 @@ void Generator::AddNamespaceToCurrentSection(absl::string_view package,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Generator::AddHeadersToCurrentSection(const std::vector<string>& headers) {
|
void Generator::AddHeadersToCurrentSection(
|
||||||
std::vector<string> sorted = headers;
|
const std::vector<std::string>& headers) {
|
||||||
|
std::vector<std::string> sorted = headers;
|
||||||
std::sort(sorted.begin(), sorted.end());
|
std::sort(sorted.begin(), sorted.end());
|
||||||
for (const auto& h : sorted) {
|
for (const auto& h : sorted) {
|
||||||
Print("#include \"", h, "\"");
|
Print("#include \"", h, "\"");
|
||||||
|
|
@ -783,7 +785,7 @@ void Generator::Generate(const FileDescriptor& fd) {
|
||||||
std::set<const Descriptor*> all_d;
|
std::set<const Descriptor*> all_d;
|
||||||
GetAllFileDescriptorsFromFile(&fd, &all_fd, &all_d);
|
GetAllFileDescriptorsFromFile(&fd, &all_fd, &all_d);
|
||||||
|
|
||||||
std::vector<string> headers;
|
std::vector<std::string> headers;
|
||||||
|
|
||||||
// Add header to header file.
|
// Add header to header file.
|
||||||
SetOutput(&header_);
|
SetOutput(&header_);
|
||||||
|
|
@ -862,8 +864,8 @@ void Generator::Generate(const FileDescriptor& fd) {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
ProtoTextFunctionCode GetProtoTextFunctionCode(const FileDescriptor& fd,
|
ProtoTextFunctionCode GetProtoTextFunctionCode(
|
||||||
const string& tf_header_prefix) {
|
const FileDescriptor& fd, const std::string& tf_header_prefix) {
|
||||||
Generator gen(tf_header_prefix);
|
Generator gen(tf_header_prefix);
|
||||||
gen.Generate(fd);
|
gen.Generate(fd);
|
||||||
return gen.code();
|
return gen.code();
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ limitations under the License.
|
||||||
namespace tensorflow {
|
namespace tensorflow {
|
||||||
|
|
||||||
struct ProtoTextFunctionCode {
|
struct ProtoTextFunctionCode {
|
||||||
string header; // for a file named proto_name + ".pb_text.h"
|
std::string header; // for a file named proto_name + ".pb_text.h"
|
||||||
string header_impl; // for a file named proto_name + ".pb_text-impl.h"
|
std::string header_impl; // for a file named proto_name + ".pb_text-impl.h"
|
||||||
string cc; // for a file named proto_name + ".pb_text.cc"
|
std::string cc; // for a file named proto_name + ".pb_text.cc"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns the generated source code for a proto file descriptor.
|
// Returns the generated source code for a proto file descriptor.
|
||||||
|
|
@ -46,7 +46,7 @@ struct ProtoTextFunctionCode {
|
||||||
// in proto.
|
// in proto.
|
||||||
ProtoTextFunctionCode GetProtoTextFunctionCode(
|
ProtoTextFunctionCode GetProtoTextFunctionCode(
|
||||||
const tensorflow::protobuf::FileDescriptor& fd,
|
const tensorflow::protobuf::FileDescriptor& fd,
|
||||||
const string& tf_header_prefix);
|
const std::string& tf_header_prefix);
|
||||||
|
|
||||||
} // namespace tensorflow
|
} // namespace tensorflow
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ std::string PrintTextFormat(const tensorflow::protobuf::Message& message) {
|
||||||
// new message using the generated parse function. Return the new message.
|
// new message using the generated parse function. Return the new message.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T RoundtripParseProtoOrDie(const T& input, bool short_text) {
|
T RoundtripParseProtoOrDie(const T& input, bool short_text) {
|
||||||
const string s =
|
const std::string s =
|
||||||
short_text ? PrintShortTextFormat(input) : PrintTextFormat(input);
|
short_text ? PrintShortTextFormat(input) : PrintTextFormat(input);
|
||||||
T t;
|
T t;
|
||||||
EXPECT_TRUE(ProtoParseFromString(s, &t)) << "Failed to parse " << s;
|
EXPECT_TRUE(ProtoParseFromString(s, &t)) << "Failed to parse " << s;
|
||||||
|
|
@ -120,10 +120,10 @@ TEST(CreateProtoDebugStringLibTest, ValidSimpleTypes) {
|
||||||
|
|
||||||
// Max numeric values.
|
// Max numeric values.
|
||||||
proto.Clear();
|
proto.Clear();
|
||||||
proto.set_optional_int32(std::numeric_limits<int32>::max());
|
proto.set_optional_int32(std::numeric_limits<int32_t>::max());
|
||||||
proto.set_optional_int64(std::numeric_limits<protobuf_int64>::max());
|
proto.set_optional_int64(std::numeric_limits<protobuf_int64>::max());
|
||||||
proto.set_optional_uint32(std::numeric_limits<uint32>::max());
|
proto.set_optional_uint32(std::numeric_limits<uint32_t>::max());
|
||||||
proto.set_optional_uint64(std::numeric_limits<uint64>::max());
|
proto.set_optional_uint64(std::numeric_limits<uint64_t>::max());
|
||||||
// TODO(b/67475677): Re-enable after resolving float precision issue
|
// TODO(b/67475677): Re-enable after resolving float precision issue
|
||||||
// proto.set_optional_float(std::numeric_limits<float>::max());
|
// proto.set_optional_float(std::numeric_limits<float>::max());
|
||||||
proto.set_optional_double(std::numeric_limits<double>::max());
|
proto.set_optional_double(std::numeric_limits<double>::max());
|
||||||
|
|
@ -138,7 +138,7 @@ TEST(CreateProtoDebugStringLibTest, ValidSimpleTypes) {
|
||||||
|
|
||||||
// Lowest numeric values.
|
// Lowest numeric values.
|
||||||
proto.Clear();
|
proto.Clear();
|
||||||
proto.set_optional_int32(std::numeric_limits<int32>::lowest());
|
proto.set_optional_int32(std::numeric_limits<int32_t>::lowest());
|
||||||
proto.set_optional_int64(std::numeric_limits<protobuf_int64>::lowest());
|
proto.set_optional_int64(std::numeric_limits<protobuf_int64>::lowest());
|
||||||
// TODO(b/67475677): Re-enable after resolving float precision issue
|
// TODO(b/67475677): Re-enable after resolving float precision issue
|
||||||
// proto.set_optional_float(std::numeric_limits<float>::lowest());
|
// proto.set_optional_float(std::numeric_limits<float>::lowest());
|
||||||
|
|
@ -361,14 +361,15 @@ TEST(CreateProtoDebugStringLibTest, RecursiveMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ParseProto(const string& value_text_proto) {
|
T ParseProto(const std::string& value_text_proto) {
|
||||||
T value;
|
T value;
|
||||||
EXPECT_TRUE(protobuf::TextFormat::ParseFromString(value_text_proto, &value))
|
EXPECT_TRUE(protobuf::TextFormat::ParseFromString(value_text_proto, &value))
|
||||||
<< value_text_proto;
|
<< value_text_proto;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
TestAllTypes::NestedMessage ParseNestedMessage(const string& value_text_proto) {
|
TestAllTypes::NestedMessage ParseNestedMessage(
|
||||||
|
const std::string& value_text_proto) {
|
||||||
return ParseProto<TestAllTypes::NestedMessage>(value_text_proto);
|
return ParseProto<TestAllTypes::NestedMessage>(value_text_proto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -494,11 +495,11 @@ TEST(CreateProtoDebugStringLibTest, Enums) {
|
||||||
EXPECT_PARSE_FAILURE("optional_nested_enum: 'BAR'");
|
EXPECT_PARSE_FAILURE("optional_nested_enum: 'BAR'");
|
||||||
EXPECT_PARSE_FAILURE("optional_nested_enum: \"BAR\" ");
|
EXPECT_PARSE_FAILURE("optional_nested_enum: \"BAR\" ");
|
||||||
|
|
||||||
EXPECT_EQ(string("BAR"),
|
EXPECT_EQ(std::string("BAR"),
|
||||||
string(EnumName_TestAllTypes_NestedEnum(TestAllTypes::BAR)));
|
std::string(EnumName_TestAllTypes_NestedEnum(TestAllTypes::BAR)));
|
||||||
// out of range - returns empty string (see NameOfEnum in proto library).
|
// out of range - returns empty string (see NameOfEnum in proto library).
|
||||||
EXPECT_EQ(string(""), string(EnumName_TestAllTypes_NestedEnum(
|
EXPECT_EQ(std::string(""), std::string(EnumName_TestAllTypes_NestedEnum(
|
||||||
static_cast<TestAllTypes_NestedEnum>(123))));
|
static_cast<TestAllTypes_NestedEnum>(123))));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(CreateProtoDebugStringLibTest, Oneof) {
|
TEST(CreateProtoDebugStringLibTest, Oneof) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user