Automated Code Change

PiperOrigin-RevId: 826451988
This commit is contained in:
A. Unique TensorFlower 2025-10-31 05:44:56 -07:00 committed by TensorFlower Gardener
parent fe344908fa
commit 4d78e8088a
4 changed files with 78 additions and 72 deletions

View File

@ -45,8 +45,8 @@ static const char kPlaceholderFile[] =
"tensorflow/tools/proto_text/placeholder.txt";
bool IsPlaceholderFile(const char* s) {
string ph(kPlaceholderFile);
string str(s);
std::string ph(kPlaceholderFile);
std::string str(s);
return str.size() >= strlen(kPlaceholderFile) &&
ph == str.substr(str.size() - ph.size());
}
@ -76,14 +76,15 @@ int MainImpl(int argc, char** argv) {
return -1;
}
const string output_root = argv[1];
const string output_relative_path = kTensorFlowHeaderPrefix + string(argv[2]);
const std::string output_root = argv[1];
const std::string output_relative_path =
kTensorFlowHeaderPrefix + std::string(argv[2]);
string src_relative_path;
std::string src_relative_path;
bool has_placeholder = false;
for (int i = 3; i < argc; ++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));
has_placeholder = true;
}
@ -102,13 +103,14 @@ int MainImpl(int argc, char** argv) {
for (int i = 3; i < argc; i++) {
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 =
importer.Import(proto_path);
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.substr(output_relative_path.size());
@ -118,8 +120,8 @@ int MainImpl(int argc, char** argv) {
// Three passes, one for each output file.
for (int pass = 0; pass < 3; ++pass) {
string suffix;
string data;
std::string suffix;
std::string data;
if (pass == 0) {
suffix = ".pb_text.h";
data = code.header;
@ -131,7 +133,8 @@ int MainImpl(int argc, char** argv) {
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");
if (f == nullptr) {
// We don't expect this output to be generated. It was specified in the

View File

@ -36,14 +36,14 @@ namespace tensorflow {
namespace {
template <typename... Args>
string StrCat(const Args&... args) {
std::string StrCat(const Args&... args) {
std::ostringstream s;
std::vector<int> give_me_a_name{((s << args), 0)...};
return s.str();
}
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...);
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.
class Generator {
public:
explicit Generator(const string& tf_header_prefix)
explicit Generator(const std::string& tf_header_prefix)
: tf_header_prefix_(tf_header_prefix),
header_(&code_.header),
header_impl_(&code_.header_impl),
@ -73,9 +73,9 @@ class Generator {
private:
struct Section {
explicit Section(string* str) : str(str) {}
string* str;
string indent;
explicit Section(std::string* str) : str(str) {}
std::string* str;
std::string indent;
};
// 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.
void AppendFieldValueAppend(const FieldDescriptor& field,
const bool omit_default,
const string& field_expr);
const std::string& field_expr);
// Appends the print code for as single field.
void AppendFieldAppend(const FieldDescriptor& field);
@ -135,17 +135,17 @@ class Generator {
void AddNamespaceToCurrentSection(absl::string_view package, bool open);
// 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.
const string tf_header_prefix_;
const std::string tf_header_prefix_;
ProtoTextFunctionCode code_;
Section* cur_ = nullptr;
Section header_;
Section header_impl_;
Section cc_;
std::unordered_set<string> map_append_signatures_included_;
std::unordered_set<std::string> map_append_signatures_included_;
Generator(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.
// "::tensorflow::test".
string GetPackageReferencePrefix(const FileDescriptor* fd) {
string result = "::";
std::string GetPackageReferencePrefix(const FileDescriptor* fd) {
std::string result = "::";
absl::string_view package = fd->package();
for (size_t i = 0; i < package.size(); ++i) {
if (package[i] == '.') {
@ -168,65 +168,65 @@ string GetPackageReferencePrefix(const FileDescriptor* fd) {
}
// 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());
return StrCat(GetClassName(*d.containing_type()), "_", d.name());
}
// 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());
return StrCat(GetClassName(*ed.containing_type()), "_", ed.name());
}
// Returns the qualified name that refers to the class generated by proto to
// represent <d>.
string GetQualifiedName(const Descriptor& d) {
std::string GetQualifiedName(const Descriptor& d) {
return StrCat(GetPackageReferencePrefix(d.file()), GetClassName(d));
}
// Returns the qualified name that refers to the class generated by proto to
// represent <ed>.
string GetQualifiedName(const EnumDescriptor& d) {
std::string GetQualifiedName(const EnumDescriptor& d) {
return StrCat(GetPackageReferencePrefix(d.file()), GetClassName(d));
}
// Returns the qualified name that refers to the generated
// AppendProtoDebugString function for <d>.
string GetQualifiedAppendFn(const Descriptor& d) {
std::string GetQualifiedAppendFn(const Descriptor& d) {
return StrCat(GetPackageReferencePrefix(d.file()),
"internal::AppendProtoDebugString");
}
// Returns the name of the generated function that returns an enum value's
// string value.
string GetEnumNameFn(const EnumDescriptor& enum_d) {
std::string GetEnumNameFn(const EnumDescriptor& enum_d) {
return StrCat("EnumName_", GetClassName(enum_d));
}
// 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()),
GetEnumNameFn(enum_d));
}
// 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).
string GetProtoTextHeaderName(const FileDescriptor& fd, bool impl) {
std::string GetProtoTextHeaderName(const FileDescriptor& fd, bool impl) {
const int dot_index = fd.name().find_last_of('.');
return StrCat(fd.name().substr(0, dot_index),
(impl ? ".pb_text-impl.h" : ".pb_text.h"));
}
// 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('.');
return StrCat(fd.name().substr(0, dot_index), ".pb.h");
}
// Returns the C++ class name for the given proto field.
string GetCppClass(const FieldDescriptor& d) {
string cpp_class = d.cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE
std::string GetCppClass(const FieldDescriptor& d) {
std::string cpp_class = d.cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE
? GetQualifiedName(*d.message_type())
: std::string(d.cpp_type_name());
@ -243,8 +243,8 @@ string GetCppClass(const FieldDescriptor& d) {
// 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
// internal implementation header (if impl is true).
string GetHeaderGuard(const FileDescriptor& fd, bool impl) {
string s(fd.name());
std::string GetHeaderGuard(const FileDescriptor& fd, bool impl) {
std::string s(fd.name());
std::replace(s.begin(), s.end(), '/', '_');
std::replace(s.begin(), s.end(), '.', '_');
return s + (impl ? "_IMPL_H_" : "_H_");
@ -252,7 +252,7 @@ string GetHeaderGuard(const FileDescriptor& fd, bool impl) {
void Generator::AppendFieldValueAppend(const FieldDescriptor& field,
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
// check 'has' fields on non-messages).
CHECK(!field.has_presence() || field.containing_oneof() != nullptr ||
@ -351,7 +351,7 @@ void Generator::AppendFieldAppend(const FieldDescriptor& field) {
} else {
const auto* oneof = field.containing_oneof();
if (oneof != nullptr) {
string camel_name(field.camelcase_name());
std::string camel_name(field.camelcase_name());
camel_name[0] = toupper(camel_name[0]);
Print("if (msg.", oneof->name(), "_case() == ",
GetQualifiedName(*oneof->containing_type()), "::k", camel_name,
@ -369,10 +369,11 @@ void Generator::AppendFieldAppend(const FieldDescriptor& field) {
}
void Generator::AppendEnumFunctions(const EnumDescriptor& enum_d) {
const string sig = StrCat("const char* ", GetEnumNameFn(enum_d), "(\n ",
const std::string sig =
StrCat("const char* ", GetEnumNameFn(enum_d), "(\n ",
GetQualifiedName(enum_d), " value)");
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, ";");
SetOutput(&cc_);
@ -389,7 +390,7 @@ void Generator::AppendEnumFunctions(const EnumDescriptor& enum_d) {
void Generator::AppendParseMessageFunction(const Descriptor& md) {
const bool map_append = (md.options().map_entry());
string sig;
std::string sig;
if (!map_append) {
sig = StrCat("bool ProtoParseFromString(\n const string& s,\n ",
GetQualifiedName(md), "* msg)");
@ -476,8 +477,8 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
for (int i = 0; i < md.field_count(); ++i) {
const FieldDescriptor* field = md.field(i);
absl::string_view field_name = field->name();
string mutable_value_expr;
string set_value_prefix;
std::string mutable_value_expr;
std::string set_value_prefix;
if (map_append) {
mutable_value_expr = 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)."
"GetResult(nullptr, &value)) return false;");
const auto* enum_d = field->enum_type();
string value_prefix;
std::string value_prefix;
if (enum_d->containing_type() == nullptr) {
value_prefix = GetPackageReferencePrefix(enum_d->file());
} else {
@ -561,7 +562,7 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
for (int enum_i = 0; enum_i < enum_d->value_count(); ++enum_i) {
const auto* value_d = enum_d->value(enum_i);
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, ") {");
Nest();
@ -628,14 +629,14 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) {
void Generator::AppendDebugStringFunctions(const Descriptor& md) {
SetOutput(&header_impl_).Print();
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>.
for (int short_pass = 0; short_pass < 2; ++short_pass) {
const bool short_debug = (short_pass == 1);
// Make the Get functions.
const string sig = StrCat(
const std::string sig = StrCat(
"string ", short_debug ? "ProtoShortDebugString" : "ProtoDebugString",
"(\n const ", GetQualifiedName(md), "& msg)");
SetOutput(&header_).Print(sig, ";");
@ -652,7 +653,7 @@ void Generator::AppendDebugStringFunctions(const Descriptor& md) {
}
// Make the Append function.
const string sig =
const std::string sig =
StrCat("void AppendProtoDebugString(\n",
" ::tensorflow::strings::ProtoTextOutput* o,\n const ",
GetQualifiedName(md), "& msg)");
@ -703,7 +704,7 @@ void Generator::AppendMessageFunctions(const Descriptor& md) {
void Generator::AddNamespaceToCurrentSection(absl::string_view package,
bool open) {
Print();
std::vector<string> parts = {""};
std::vector<std::string> parts = {""};
for (size_t i = 0; i < package.size(); ++i) {
if (package[i] == '.') {
parts.resize(parts.size() + 1);
@ -722,8 +723,9 @@ void Generator::AddNamespaceToCurrentSection(absl::string_view package,
}
}
void Generator::AddHeadersToCurrentSection(const std::vector<string>& headers) {
std::vector<string> sorted = headers;
void Generator::AddHeadersToCurrentSection(
const std::vector<std::string>& headers) {
std::vector<std::string> sorted = headers;
std::sort(sorted.begin(), sorted.end());
for (const auto& h : sorted) {
Print("#include \"", h, "\"");
@ -783,7 +785,7 @@ void Generator::Generate(const FileDescriptor& fd) {
std::set<const Descriptor*> all_d;
GetAllFileDescriptorsFromFile(&fd, &all_fd, &all_d);
std::vector<string> headers;
std::vector<std::string> headers;
// Add header to header file.
SetOutput(&header_);
@ -862,8 +864,8 @@ void Generator::Generate(const FileDescriptor& fd) {
} // namespace
ProtoTextFunctionCode GetProtoTextFunctionCode(const FileDescriptor& fd,
const string& tf_header_prefix) {
ProtoTextFunctionCode GetProtoTextFunctionCode(
const FileDescriptor& fd, const std::string& tf_header_prefix) {
Generator gen(tf_header_prefix);
gen.Generate(fd);
return gen.code();

View File

@ -22,9 +22,9 @@ limitations under the License.
namespace tensorflow {
struct ProtoTextFunctionCode {
string header; // for a file named proto_name + ".pb_text.h"
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 header; // for a file named proto_name + ".pb_text.h"
std::string header_impl; // for a file named proto_name + ".pb_text-impl.h"
std::string cc; // for a file named proto_name + ".pb_text.cc"
};
// Returns the generated source code for a proto file descriptor.
@ -46,7 +46,7 @@ struct ProtoTextFunctionCode {
// in proto.
ProtoTextFunctionCode GetProtoTextFunctionCode(
const tensorflow::protobuf::FileDescriptor& fd,
const string& tf_header_prefix);
const std::string& tf_header_prefix);
} // namespace tensorflow

View File

@ -59,7 +59,7 @@ std::string PrintTextFormat(const tensorflow::protobuf::Message& message) {
// new message using the generated parse function. Return the new message.
template <typename T>
T RoundtripParseProtoOrDie(const T& input, bool short_text) {
const string s =
const std::string s =
short_text ? PrintShortTextFormat(input) : PrintTextFormat(input);
T t;
EXPECT_TRUE(ProtoParseFromString(s, &t)) << "Failed to parse " << s;
@ -120,10 +120,10 @@ TEST(CreateProtoDebugStringLibTest, ValidSimpleTypes) {
// Max numeric values.
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_uint32(std::numeric_limits<uint32>::max());
proto.set_optional_uint64(std::numeric_limits<uint64>::max());
proto.set_optional_uint32(std::numeric_limits<uint32_t>::max());
proto.set_optional_uint64(std::numeric_limits<uint64_t>::max());
// TODO(b/67475677): Re-enable after resolving float precision issue
// proto.set_optional_float(std::numeric_limits<float>::max());
proto.set_optional_double(std::numeric_limits<double>::max());
@ -138,7 +138,7 @@ TEST(CreateProtoDebugStringLibTest, ValidSimpleTypes) {
// Lowest numeric values.
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());
// TODO(b/67475677): Re-enable after resolving float precision issue
// proto.set_optional_float(std::numeric_limits<float>::lowest());
@ -361,14 +361,15 @@ TEST(CreateProtoDebugStringLibTest, RecursiveMessage) {
}
template <typename T>
T ParseProto(const string& value_text_proto) {
T ParseProto(const std::string& value_text_proto) {
T value;
EXPECT_TRUE(protobuf::TextFormat::ParseFromString(value_text_proto, &value))
<< value_text_proto;
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);
}
@ -494,10 +495,10 @@ TEST(CreateProtoDebugStringLibTest, Enums) {
EXPECT_PARSE_FAILURE("optional_nested_enum: 'BAR'");
EXPECT_PARSE_FAILURE("optional_nested_enum: \"BAR\" ");
EXPECT_EQ(string("BAR"),
string(EnumName_TestAllTypes_NestedEnum(TestAllTypes::BAR)));
EXPECT_EQ(std::string("BAR"),
std::string(EnumName_TestAllTypes_NestedEnum(TestAllTypes::BAR)));
// 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))));
}