From d58b154e86883a12f5f0eef9c6438ff68ccd61b4 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 6 Mar 2025 11:00:54 -0800 Subject: [PATCH] Small cleanup of GenerateThunksCC The code had a weird workaround for issue with putting extern "C" {} into a cc block, and a comment that was there about this accidentally had its //~ comments mismangled (putting comments into the emitted gencode that was supposed to be stripped). This has no a functional change, just a local cleanup. PiperOrigin-RevId: 734210691 --- src/google/protobuf/compiler/rust/message.cc | 59 ++++++++------------ 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 9e946bdb46..cec57debd7 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -1307,50 +1307,39 @@ void GenerateThunksCc(Context& ctx, const Descriptor& msg) { return; } + // Approaches to put the extern "C" in any R"cc()cc" badly confuse either + // clang-format or VSCode highlighting. Emit this as a vanilla raw string to + // avoid any issues. + ctx.Emit(R"(extern "C" { + )"); + ctx.Emit( - {{"abi", "\"C\""}, // Workaround for syntax highlight bug in VSCode. - {"Msg", RsSafeName(msg.name())}, - {"QualifiedMsg", cpp::QualifiedClassName(&msg)}, + {{"QualifiedMsg", cpp::QualifiedClassName(&msg)}, {"new_thunk", ThunkName(ctx, msg, "new")}, - {"default_instance_thunk", ThunkName(ctx, msg, "default_instance")}, - {"nested_msg_thunks", - [&] { - for (int i = 0; i < msg.nested_type_count(); ++i) { - GenerateThunksCc(ctx, *msg.nested_type(i)); - } - }}, - {"accessor_thunks", - [&] { - for (int i = 0; i < msg.field_count(); ++i) { - GenerateAccessorThunkCc(ctx, *msg.field(i)); - } - }}, - {"oneof_thunks", - [&] { - for (int i = 0; i < msg.real_oneof_decl_count(); ++i) { - GenerateOneofThunkCc(ctx, *msg.real_oneof_decl(i)); - } - }}}, + {"default_instance_thunk", ThunkName(ctx, msg, "default_instance")}}, R"cc( - //~ $abi$ is a workaround for a syntax highlight bug in VSCode. - // However, ~ that confuses clang-format (it refuses to keep the - // newline after ~ `$abi${`). Disabling clang-format for the block. - // clang-format off - extern $abi$ { void* $new_thunk$() { return new $QualifiedMsg$(); } const google::protobuf::MessageLite* $default_instance_thunk$() { return &$QualifiedMsg$::default_instance(); } - - $accessor_thunks$ - - $oneof_thunks$ - } // extern $abi$ - // clang-format on - - $nested_msg_thunks$ )cc"); + + for (int i = 0; i < msg.field_count(); ++i) { + GenerateAccessorThunkCc(ctx, *msg.field(i)); + } + + for (int i = 0; i < msg.real_oneof_decl_count(); ++i) { + GenerateOneofThunkCc(ctx, *msg.real_oneof_decl(i)); + } + + ctx.Emit(R"(} //extern "C" + )"); + + // Recursively generate the thunks for any nested messages. + for (int i = 0; i < msg.nested_type_count(); ++i) { + GenerateThunksCc(ctx, *msg.nested_type(i)); + } } } // namespace rust