Skip to content

Commit f65d7a7

Browse files
authored
[pigeon] adds Internal options (flutter#8709)
Separates the user facing options from the internal use of options to avoid exposing settings we don't need the users to access. fixes flutter/flutter#161634 Also adds option to prevent merging of pigeon definition file options. fixes flutter/flutter#163159 Also removes injection of overflow members for testing from public facing api surface.
1 parent ff7724c commit f65d7a7

29 files changed

+4044
-3063
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 25.0.0
2+
3+
* **Breaking Change** Removes `oneLanguage` field from `PigeonOptions`.
4+
* Separates internal options classes from user facing options.
5+
* Adds `mergeDefinitionFileOptions` parameter to `runWithOptions` method.
6+
* Relocates `injectOverflowTypes` to non-public facing method.
7+
18
## 24.2.2
29

310
* Updates compileSdk 34 to flutter.compileSdkVersion.

packages/pigeon/lib/src/cpp/cpp_generator.dart

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:path/path.dart' as path;
6+
57
import '../ast.dart';
68
import '../functional.dart';
79
import '../generator.dart';
810
import '../generator_tools.dart';
9-
import '../pigeon_lib.dart' show Error;
11+
import '../pigeon_lib.dart';
1012

1113
/// General comment opening token.
1214
const String _commentPrefix = '//';
@@ -51,7 +53,7 @@ class CppOptions {
5153
this.headerOutPath,
5254
});
5355

54-
/// The path to the header that will get placed in the source filed (example:
56+
/// The path to the header that will get placed in the source file (example:
5557
/// "foo.h").
5658
final String? headerIncludePath;
5759

@@ -93,15 +95,61 @@ class CppOptions {
9395
}
9496
}
9597

98+
/// Options that control how C++ code will be generated.
99+
///
100+
/// For internal use only.
101+
class InternalCppOptions {
102+
/// Creates a [InternalCppOptions] object.
103+
const InternalCppOptions({
104+
required this.headerIncludePath,
105+
required this.cppHeaderOut,
106+
required this.cppSourceOut,
107+
this.namespace,
108+
this.copyrightHeader,
109+
this.headerOutPath,
110+
});
111+
112+
/// Creates InternalCppOptions from CppOptions.
113+
InternalCppOptions.fromCppOptions(
114+
CppOptions options, {
115+
required this.cppHeaderOut,
116+
required this.cppSourceOut,
117+
Iterable<String>? copyrightHeader,
118+
}) : headerIncludePath =
119+
options.headerIncludePath ?? path.basename(cppHeaderOut),
120+
namespace = options.namespace,
121+
copyrightHeader = options.copyrightHeader ?? copyrightHeader,
122+
headerOutPath = options.headerOutPath;
123+
124+
/// The path to the header that will get placed in the source file (example:
125+
/// "foo.h").
126+
final String headerIncludePath;
127+
128+
/// Path to the ".h" C++ file that will be generated.
129+
final String cppHeaderOut;
130+
131+
/// Path to the ".cpp" C++ file that will be generated.
132+
final String cppSourceOut;
133+
134+
/// The namespace where the generated class will live.
135+
final String? namespace;
136+
137+
/// A copyright header that will get prepended to generated code.
138+
final Iterable<String>? copyrightHeader;
139+
140+
/// The path to the output header file location.
141+
final String? headerOutPath;
142+
}
143+
96144
/// Class that manages all Cpp code generation.
97-
class CppGenerator extends Generator<OutputFileOptions<CppOptions>> {
145+
class CppGenerator extends Generator<OutputFileOptions<InternalCppOptions>> {
98146
/// Constructor.
99147
const CppGenerator();
100148

101149
/// Generates C++ file of type specified in [generatorOptions]
102150
@override
103151
void generate(
104-
OutputFileOptions<CppOptions> generatorOptions,
152+
OutputFileOptions<InternalCppOptions> generatorOptions,
105153
Root root,
106154
StringSink sink, {
107155
required String dartPackageName,
@@ -127,13 +175,13 @@ class CppGenerator extends Generator<OutputFileOptions<CppOptions>> {
127175
}
128176

129177
/// Writes C++ header (.h) file to sink.
130-
class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
178+
class CppHeaderGenerator extends StructuredGenerator<InternalCppOptions> {
131179
/// Constructor.
132180
const CppHeaderGenerator();
133181

134182
@override
135183
void writeFilePrologue(
136-
CppOptions generatorOptions,
184+
InternalCppOptions generatorOptions,
137185
Root root,
138186
Indent indent, {
139187
required String dartPackageName,
@@ -148,7 +196,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
148196

149197
@override
150198
void writeFileImports(
151-
CppOptions generatorOptions,
199+
InternalCppOptions generatorOptions,
152200
Root root,
153201
Indent indent, {
154202
required String dartPackageName,
@@ -185,7 +233,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
185233

186234
@override
187235
void writeEnum(
188-
CppOptions generatorOptions,
236+
InternalCppOptions generatorOptions,
189237
Root root,
190238
Indent indent,
191239
Enum anEnum, {
@@ -208,7 +256,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
208256

209257
@override
210258
void writeGeneralUtilities(
211-
CppOptions generatorOptions,
259+
InternalCppOptions generatorOptions,
212260
Root root,
213261
Indent indent, {
214262
required String dartPackageName,
@@ -226,7 +274,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
226274

227275
@override
228276
void writeDataClasses(
229-
CppOptions generatorOptions,
277+
InternalCppOptions generatorOptions,
230278
Root root,
231279
Indent indent, {
232280
required String dartPackageName,
@@ -252,7 +300,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
252300

253301
@override
254302
void writeDataClass(
255-
CppOptions generatorOptions,
303+
InternalCppOptions generatorOptions,
256304
Root root,
257305
Indent indent,
258306
Class classDefinition, {
@@ -399,7 +447,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
399447

400448
@override
401449
void writeGeneralCodec(
402-
CppOptions generatorOptions,
450+
InternalCppOptions generatorOptions,
403451
Root root,
404452
Indent indent, {
405453
required String dartPackageName,
@@ -443,7 +491,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
443491

444492
@override
445493
void writeFlutterApi(
446-
CppOptions generatorOptions,
494+
InternalCppOptions generatorOptions,
447495
Root root,
448496
Indent indent,
449497
AstFlutterApi api, {
@@ -498,7 +546,7 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
498546

499547
@override
500548
void writeHostApi(
501-
CppOptions generatorOptions,
549+
InternalCppOptions generatorOptions,
502550
Root root,
503551
Indent indent,
504552
AstHostApi api, {
@@ -660,7 +708,7 @@ $friendLines
660708

661709
@override
662710
void writeCloseNamespace(
663-
CppOptions generatorOptions,
711+
InternalCppOptions generatorOptions,
664712
Root root,
665713
Indent indent, {
666714
required String dartPackageName,
@@ -674,13 +722,13 @@ $friendLines
674722
}
675723

676724
/// Writes C++ source (.cpp) file to sink.
677-
class CppSourceGenerator extends StructuredGenerator<CppOptions> {
725+
class CppSourceGenerator extends StructuredGenerator<InternalCppOptions> {
678726
/// Constructor.
679727
const CppSourceGenerator();
680728

681729
@override
682730
void writeFilePrologue(
683-
CppOptions generatorOptions,
731+
InternalCppOptions generatorOptions,
684732
Root root,
685733
Indent indent, {
686734
required String dartPackageName,
@@ -697,7 +745,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
697745

698746
@override
699747
void writeFileImports(
700-
CppOptions generatorOptions,
748+
InternalCppOptions generatorOptions,
701749
Root root,
702750
Indent indent, {
703751
required String dartPackageName,
@@ -721,7 +769,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
721769

722770
@override
723771
void writeOpenNamespace(
724-
CppOptions generatorOptions,
772+
InternalCppOptions generatorOptions,
725773
Root root,
726774
Indent indent, {
727775
required String dartPackageName,
@@ -733,7 +781,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
733781

734782
@override
735783
void writeGeneralUtilities(
736-
CppOptions generatorOptions,
784+
InternalCppOptions generatorOptions,
737785
Root root,
738786
Indent indent, {
739787
required String dartPackageName,
@@ -763,7 +811,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
763811

764812
@override
765813
void writeDataClass(
766-
CppOptions generatorOptions,
814+
InternalCppOptions generatorOptions,
767815
Root root,
768816
Indent indent,
769817
Class classDefinition, {
@@ -817,7 +865,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
817865

818866
@override
819867
void writeClassEncode(
820-
CppOptions generatorOptions,
868+
InternalCppOptions generatorOptions,
821869
Root root,
822870
Indent indent,
823871
Class classDefinition, {
@@ -848,7 +896,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
848896

849897
@override
850898
void writeClassDecode(
851-
CppOptions generatorOptions,
899+
InternalCppOptions generatorOptions,
852900
Root root,
853901
Indent indent,
854902
Class classDefinition, {
@@ -919,7 +967,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
919967
}
920968

921969
void _writeCodecOverflowUtilities(
922-
CppOptions generatorOptions,
970+
InternalCppOptions generatorOptions,
923971
Root root,
924972
Indent indent,
925973
List<EnumeratedType> types, {
@@ -986,7 +1034,7 @@ EncodableValue $_overflowClassName::FromEncodableList(
9861034

9871035
@override
9881036
void writeGeneralCodec(
989-
CppOptions generatorOptions,
1037+
InternalCppOptions generatorOptions,
9901038
Root root,
9911039
Indent indent, {
9921040
required String dartPackageName,
@@ -1081,7 +1129,7 @@ EncodableValue $_overflowClassName::FromEncodableList(
10811129

10821130
@override
10831131
void writeFlutterApi(
1084-
CppOptions generatorOptions,
1132+
InternalCppOptions generatorOptions,
10851133
Root root,
10861134
Indent indent,
10871135
AstFlutterApi api, {
@@ -1215,7 +1263,7 @@ EncodableValue $_overflowClassName::FromEncodableList(
12151263

12161264
@override
12171265
void writeHostApi(
1218-
CppOptions generatorOptions,
1266+
InternalCppOptions generatorOptions,
12191267
Root root,
12201268
Indent indent,
12211269
AstHostApi api, {
@@ -1439,7 +1487,7 @@ return EncodableValue(EncodableList{
14391487
});
14401488
}
14411489

1442-
void _writeCppSourceClassField(CppOptions generatorOptions, Root root,
1490+
void _writeCppSourceClassField(InternalCppOptions generatorOptions, Root root,
14431491
Indent indent, Class classDefinition, NamedType field) {
14441492
final HostDatatype hostDatatype =
14451493
getFieldHostDatatype(field, _shortBaseCppTypeForBuiltinDartType);
@@ -1570,7 +1618,7 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
15701618

15711619
@override
15721620
void writeCloseNamespace(
1573-
CppOptions generatorOptions,
1621+
InternalCppOptions generatorOptions,
15741622
Root root,
15751623
Indent indent, {
15761624
required String dartPackageName,
@@ -2085,7 +2133,7 @@ void _writeAccessBlock(
20852133
}
20862134

20872135
/// Validates an AST to make sure the cpp generator supports everything.
2088-
List<Error> validateCpp(CppOptions options, Root root) {
2136+
List<Error> validateCpp(InternalCppOptions options, Root root) {
20892137
final List<Error> result = <Error>[];
20902138
for (final Api api in root.apis) {
20912139
for (final Method method in api.methods) {

0 commit comments

Comments
 (0)