Skip to content

Commit 3379e51

Browse files
[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 flutter/flutter#147328
1 parent 2677981 commit 3379e51

10 files changed

Lines changed: 42 additions & 18 deletions

File tree

packages/pigeon/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 21.0.0
2+
3+
* **Breaking Change** [cpp] Fixes style of enum names. References to enum values
4+
will need to be updated to `EnumType.kValue` style, instead of the previous
5+
`EnumType.value`.
6+
17
## 20.0.2
28

39
* [java] Adds `equals` and `hashCode` support for data classes.

packages/pigeon/example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class PigeonApiImplementation : public ExampleHostApi {
182182
}
183183
void SendMessage(const MessageData& message,
184184
std::function<void(ErrorOr<bool> reply)> result) {
185-
if (message.code == Code.one) {
185+
if (message.code == Code.kOne) {
186186
result(FlutterError("code", "message", "details"));
187187
return;
188188
}

packages/pigeon/example/app/windows/runner/flutter_window.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PigeonApiImplementation : public ExampleHostApi {
2929
}
3030
void SendMessage(const MessageData& message,
3131
std::function<void(ErrorOr<bool> reply)> result) {
32-
if (message.code == Code.one) {
32+
if (message.code == Code.kOne) {
3333
result(FlutterError("code", "message", "details"));
3434
return;
3535
}

packages/pigeon/example/app/windows/runner/messages.g.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ErrorOr {
5959
std::variant<T, FlutterError> v_;
6060
};
6161

62-
enum class Code { one = 0, two = 1 };
62+
enum class Code { kOne = 0, kTwo = 1 };
6363

6464
// Generated class from Pigeon that represents data sent in messages.
6565
class MessageData {

packages/pigeon/lib/cpp_generator.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
181181
enumerate(anEnum.members, (int index, final EnumMember member) {
182182
addDocumentationComments(
183183
indent, member.documentationComments, _docCommentSpec);
184+
final String valueName = 'k${_pascalCaseFromCamelCase(member.name)}';
184185
indent.writeln(
185-
'${member.name} = $index${index == anEnum.members.length - 1 ? '' : ','}');
186+
'$valueName = $index${index == anEnum.members.length - 1 ? '' : ','}');
186187
});
187188
});
188189
}

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'ast.dart';
1313
/// The current version of pigeon.
1414
///
1515
/// This must match the version in pubspec.yaml.
16-
const String pigeonVersion = '20.0.2';
16+
const String pigeonVersion = '21.0.0';
1717

1818
/// Prefix for all local variables in methods.
1919
///

packages/pigeon/platform_tests/test_plugin/windows/pigeon/core_tests.gen.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class ErrorOr {
6666
};
6767

6868
enum class AnEnum {
69-
one = 0,
70-
two = 1,
71-
three = 2,
72-
fortyTwo = 3,
73-
fourHundredTwentyTwo = 4
69+
kOne = 0,
70+
kTwo = 1,
71+
kThree = 2,
72+
kFortyTwo = 3,
73+
kFourHundredTwentyTwo = 4
7474
};
7575

7676
// A class containing all supported types.

packages/pigeon/platform_tests/test_plugin/windows/test/null_fields_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ TEST(NullFields, BuildWithValues) {
6666
reply.set_error("error");
6767
reply.set_indices(EncodableList({1, 2, 3}));
6868
reply.set_request(request);
69-
reply.set_type(NullFieldsSearchReplyType::success);
69+
reply.set_type(NullFieldsSearchReplyType::kSuccess);
7070

7171
EXPECT_EQ(*reply.result(), "result");
7272
EXPECT_EQ(*reply.error(), "error");
7373
EXPECT_EQ(reply.indices()->size(), 3);
7474
EXPECT_EQ(*reply.request()->query(), "hello");
75-
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::success);
75+
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::kSuccess);
7676
}
7777

7878
TEST(NullFields, BuildRequestWithNulls) {
@@ -121,7 +121,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) {
121121
EncodableValue("error"),
122122
EncodableValue(EncodableList({1, 2, 3})),
123123
CustomEncodableValue(request),
124-
CustomEncodableValue(NullFieldsSearchReplyType::success),
124+
CustomEncodableValue(NullFieldsSearchReplyType::kSuccess),
125125
};
126126
NullFieldsSearchReply reply = ReplyFromList(list);
127127

@@ -130,7 +130,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) {
130130
EXPECT_EQ(reply.indices()->size(), 3);
131131
EXPECT_EQ(*reply.request()->query(), "hello");
132132
EXPECT_EQ(reply.request()->identifier(), 1);
133-
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::success);
133+
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::kSuccess);
134134
}
135135

136136
TEST_F(NullFieldsTest, ReplyFromListWithNulls) {
@@ -178,7 +178,7 @@ TEST_F(NullFieldsTest, ReplyToMapWithValues) {
178178
reply.set_error("error");
179179
reply.set_indices(EncodableList({1, 2, 3}));
180180
reply.set_request(request);
181-
reply.set_type(NullFieldsSearchReplyType::success);
181+
reply.set_type(NullFieldsSearchReplyType::kSuccess);
182182

183183
const EncodableList list = ListFromReply(reply);
184184

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 20.0.2 # This must match the version in lib/generator_tools.dart
5+
version: 21.0.0 # This must match the version in lib/generator_tools.dart
66

77
environment:
88
sdk: ^3.2.0

packages/pigeon/test/cpp_generator_test.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ void main() {
101101
});
102102

103103
test('naming follows style', () {
104+
final Enum anEnum = Enum(name: 'AnEnum', members: <EnumMember>[
105+
EnumMember(name: 'one'),
106+
EnumMember(name: 'fortyTwo'),
107+
]);
104108
final Root root = Root(apis: <Api>[
105109
AstHostApi(name: 'Api', methods: <Method>[
106110
Method(
@@ -137,9 +141,19 @@ void main() {
137141
baseName: 'bool',
138142
isNullable: false,
139143
),
140-
name: 'outputField')
144+
name: 'outputField'),
145+
NamedType(
146+
type: TypeDeclaration(
147+
baseName: anEnum.name,
148+
isNullable: false,
149+
associatedEnum: anEnum,
150+
),
151+
name: 'code',
152+
)
141153
])
142-
], enums: <Enum>[]);
154+
], enums: <Enum>[
155+
anEnum
156+
]);
143157
{
144158
final StringBuffer sink = StringBuffer();
145159
const CppGenerator generator = CppGenerator();
@@ -161,6 +175,9 @@ void main() {
161175
// Instance variables should be adjusted.
162176
expect(code, contains('bool input_field_'));
163177
expect(code, contains('bool output_field_'));
178+
// Enum values should be adjusted.
179+
expect(code, contains('kOne'));
180+
expect(code, contains('kFortyTwo'));
164181
}
165182
{
166183
final StringBuffer sink = StringBuffer();

0 commit comments

Comments
 (0)