From 3379e51a042af5efe5c5e1e69e0f3ce5d979351f Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Fri, 12 Jul 2024 14:23:19 -0400 Subject: [PATCH] [pigeon] Fix C++ enum naming (#7094) The C++ generator wasn't adjusting enum value names, so they were just using the Dart camelCase style. This makes them style-guide compliant by adding the `k` prefix and making the first actual letter upper-case. Fixes https://github.com/flutter/flutter/issues/147328 --- packages/pigeon/CHANGELOG.md | 6 ++++++ packages/pigeon/example/README.md | 2 +- .../app/windows/runner/flutter_window.cpp | 2 +- .../example/app/windows/runner/messages.g.h | 2 +- packages/pigeon/lib/cpp_generator.dart | 3 ++- packages/pigeon/lib/generator_tools.dart | 2 +- .../windows/pigeon/core_tests.gen.h | 10 ++++----- .../windows/test/null_fields_test.cpp | 10 ++++----- packages/pigeon/pubspec.yaml | 2 +- packages/pigeon/test/cpp_generator_test.dart | 21 +++++++++++++++++-- 10 files changed, 42 insertions(+), 18 deletions(-) diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index e13c7e3c4d..ec34f493db 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,9 @@ +## 21.0.0 + +* **Breaking Change** [cpp] Fixes style of enum names. References to enum values + will need to be updated to `EnumType.kValue` style, instead of the previous + `EnumType.value`. + ## 20.0.2 * [java] Adds `equals` and `hashCode` support for data classes. diff --git a/packages/pigeon/example/README.md b/packages/pigeon/example/README.md index 46e8c643b1..4aa23db934 100644 --- a/packages/pigeon/example/README.md +++ b/packages/pigeon/example/README.md @@ -182,7 +182,7 @@ class PigeonApiImplementation : public ExampleHostApi { } void SendMessage(const MessageData& message, std::function reply)> result) { - if (message.code == Code.one) { + if (message.code == Code.kOne) { result(FlutterError("code", "message", "details")); return; } diff --git a/packages/pigeon/example/app/windows/runner/flutter_window.cpp b/packages/pigeon/example/app/windows/runner/flutter_window.cpp index d4f0fec674..b7f7ab0359 100644 --- a/packages/pigeon/example/app/windows/runner/flutter_window.cpp +++ b/packages/pigeon/example/app/windows/runner/flutter_window.cpp @@ -29,7 +29,7 @@ class PigeonApiImplementation : public ExampleHostApi { } void SendMessage(const MessageData& message, std::function reply)> result) { - if (message.code == Code.one) { + if (message.code == Code.kOne) { result(FlutterError("code", "message", "details")); return; } diff --git a/packages/pigeon/example/app/windows/runner/messages.g.h b/packages/pigeon/example/app/windows/runner/messages.g.h index 84368cbb1d..3218b702d9 100644 --- a/packages/pigeon/example/app/windows/runner/messages.g.h +++ b/packages/pigeon/example/app/windows/runner/messages.g.h @@ -59,7 +59,7 @@ class ErrorOr { std::variant v_; }; -enum class Code { one = 0, two = 1 }; +enum class Code { kOne = 0, kTwo = 1 }; // Generated class from Pigeon that represents data sent in messages. class MessageData { diff --git a/packages/pigeon/lib/cpp_generator.dart b/packages/pigeon/lib/cpp_generator.dart index 47dd2b3276..b8d1fbb5d2 100644 --- a/packages/pigeon/lib/cpp_generator.dart +++ b/packages/pigeon/lib/cpp_generator.dart @@ -181,8 +181,9 @@ class CppHeaderGenerator extends StructuredGenerator { enumerate(anEnum.members, (int index, final EnumMember member) { addDocumentationComments( indent, member.documentationComments, _docCommentSpec); + final String valueName = 'k${_pascalCaseFromCamelCase(member.name)}'; indent.writeln( - '${member.name} = $index${index == anEnum.members.length - 1 ? '' : ','}'); + '$valueName = $index${index == anEnum.members.length - 1 ? '' : ','}'); }); }); } diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart index c78761ecef..6b83633d68 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -13,7 +13,7 @@ import 'ast.dart'; /// The current version of pigeon. /// /// This must match the version in pubspec.yaml. -const String pigeonVersion = '20.0.2'; +const String pigeonVersion = '21.0.0'; /// Prefix for all local variables in methods. /// diff --git a/packages/pigeon/platform_tests/test_plugin/windows/pigeon/core_tests.gen.h b/packages/pigeon/platform_tests/test_plugin/windows/pigeon/core_tests.gen.h index ceed1b0872..1d0b642895 100644 --- a/packages/pigeon/platform_tests/test_plugin/windows/pigeon/core_tests.gen.h +++ b/packages/pigeon/platform_tests/test_plugin/windows/pigeon/core_tests.gen.h @@ -66,11 +66,11 @@ class ErrorOr { }; enum class AnEnum { - one = 0, - two = 1, - three = 2, - fortyTwo = 3, - fourHundredTwentyTwo = 4 + kOne = 0, + kTwo = 1, + kThree = 2, + kFortyTwo = 3, + kFourHundredTwentyTwo = 4 }; // A class containing all supported types. diff --git a/packages/pigeon/platform_tests/test_plugin/windows/test/null_fields_test.cpp b/packages/pigeon/platform_tests/test_plugin/windows/test/null_fields_test.cpp index e6a366248f..da56b4a757 100644 --- a/packages/pigeon/platform_tests/test_plugin/windows/test/null_fields_test.cpp +++ b/packages/pigeon/platform_tests/test_plugin/windows/test/null_fields_test.cpp @@ -66,13 +66,13 @@ TEST(NullFields, BuildWithValues) { reply.set_error("error"); reply.set_indices(EncodableList({1, 2, 3})); reply.set_request(request); - reply.set_type(NullFieldsSearchReplyType::success); + reply.set_type(NullFieldsSearchReplyType::kSuccess); EXPECT_EQ(*reply.result(), "result"); EXPECT_EQ(*reply.error(), "error"); EXPECT_EQ(reply.indices()->size(), 3); EXPECT_EQ(*reply.request()->query(), "hello"); - EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::success); + EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::kSuccess); } TEST(NullFields, BuildRequestWithNulls) { @@ -121,7 +121,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) { EncodableValue("error"), EncodableValue(EncodableList({1, 2, 3})), CustomEncodableValue(request), - CustomEncodableValue(NullFieldsSearchReplyType::success), + CustomEncodableValue(NullFieldsSearchReplyType::kSuccess), }; NullFieldsSearchReply reply = ReplyFromList(list); @@ -130,7 +130,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) { EXPECT_EQ(reply.indices()->size(), 3); EXPECT_EQ(*reply.request()->query(), "hello"); EXPECT_EQ(reply.request()->identifier(), 1); - EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::success); + EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::kSuccess); } TEST_F(NullFieldsTest, ReplyFromListWithNulls) { @@ -178,7 +178,7 @@ TEST_F(NullFieldsTest, ReplyToMapWithValues) { reply.set_error("error"); reply.set_indices(EncodableList({1, 2, 3})); reply.set_request(request); - reply.set_type(NullFieldsSearchReplyType::success); + reply.set_type(NullFieldsSearchReplyType::kSuccess); const EncodableList list = ListFromReply(reply); diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index 411d3bdb58..e833e74934 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -2,7 +2,7 @@ name: pigeon description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. repository: https://github.com/flutter/packages/tree/main/packages/pigeon issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22 -version: 20.0.2 # This must match the version in lib/generator_tools.dart +version: 21.0.0 # This must match the version in lib/generator_tools.dart environment: sdk: ^3.2.0 diff --git a/packages/pigeon/test/cpp_generator_test.dart b/packages/pigeon/test/cpp_generator_test.dart index 80ffc07022..edc074a478 100644 --- a/packages/pigeon/test/cpp_generator_test.dart +++ b/packages/pigeon/test/cpp_generator_test.dart @@ -101,6 +101,10 @@ void main() { }); test('naming follows style', () { + final Enum anEnum = Enum(name: 'AnEnum', members: [ + EnumMember(name: 'one'), + EnumMember(name: 'fortyTwo'), + ]); final Root root = Root(apis: [ AstHostApi(name: 'Api', methods: [ Method( @@ -137,9 +141,19 @@ void main() { baseName: 'bool', isNullable: false, ), - name: 'outputField') + name: 'outputField'), + NamedType( + type: TypeDeclaration( + baseName: anEnum.name, + isNullable: false, + associatedEnum: anEnum, + ), + name: 'code', + ) ]) - ], enums: []); + ], enums: [ + anEnum + ]); { final StringBuffer sink = StringBuffer(); const CppGenerator generator = CppGenerator(); @@ -161,6 +175,9 @@ void main() { // Instance variables should be adjusted. expect(code, contains('bool input_field_')); expect(code, contains('bool output_field_')); + // Enum values should be adjusted. + expect(code, contains('kOne')); + expect(code, contains('kFortyTwo')); } { final StringBuffer sink = StringBuffer();