@@ -7,6 +7,7 @@ import 'package:path/path.dart' as p;
7
7
import '../code_generator.dart' ;
8
8
import '../context.dart' ;
9
9
import '../strings.dart' as strings;
10
+ import '../visitor/visitor.dart' ;
10
11
import 'unique_namer.dart' ;
11
12
import 'utils.dart' ;
12
13
@@ -39,10 +40,6 @@ class Writer {
39
40
40
41
final List <String > nativeEntryPoints;
41
42
42
- /// Tracks the enums for which enumType.getCType is called. Reset everytime
43
- /// [generate] is called.
44
- final usedEnumCTypes = < EnumClass > {};
45
-
46
43
String ? _ffiLibraryPrefix;
47
44
String get ffiLibraryPrefix {
48
45
if (_ffiLibraryPrefix != null ) {
@@ -228,9 +225,6 @@ class Writer {
228
225
// Reset unique namers to initial state.
229
226
_resetUniqueNamers ();
230
227
231
- // Reset [usedEnumCTypes].
232
- usedEnumCTypes.clear ();
233
-
234
228
// Write file header (if any).
235
229
if (header != null ) {
236
230
result.writeln (header);
@@ -325,17 +319,23 @@ class Writer {
325
319
result.write (s);
326
320
327
321
// Warn about Enum usage in API surface.
328
- if (! silenceEnumWarning && usedEnumCTypes.isNotEmpty) {
329
- final names = usedEnumCTypes.map ((e) => e.originalName).toList ()..sort ();
330
- context.logger.severe (
331
- 'The integer type used for enums is '
332
- 'implementation-defined. FFIgen tries to mimic the integer sizes '
333
- 'chosen by the most common compilers for the various OS and '
334
- 'architecture combinations. To prevent any crashes, remove the '
335
- 'enums from your API surface. To rely on the (unsafe!) mimicking, '
336
- 'you can silence this warning by adding silence-enum-warning: true '
337
- 'to the FFIgen config. Affected enums:\n\t ${names .join ('\n\t ' )}' ,
322
+ if (! silenceEnumWarning) {
323
+ final notEnums = _allBindings.where (
324
+ (b) => b is ! Type || (b as Type ).typealiasType is ! EnumClass ,
338
325
);
326
+ final usedEnums = visit (context, _FindEnumsVisitation (), notEnums).enums;
327
+ if (usedEnums.isNotEmpty) {
328
+ final names = usedEnums.map ((e) => e.originalName).toList ()..sort ();
329
+ context.logger.severe (
330
+ 'The integer type used for enums is '
331
+ 'implementation-defined. FFIgen tries to mimic the integer sizes '
332
+ 'chosen by the most common compilers for the various OS and '
333
+ 'architecture combinations. To prevent any crashes, remove the '
334
+ 'enums from your API surface. To rely on the (unsafe!) mimicking, '
335
+ 'you can silence this warning by adding silence-enum-warning: true '
336
+ 'to the FFIgen config. Affected enums:\n\t ${names .join ('\n\t ' )}' ,
337
+ );
338
+ }
339
339
}
340
340
341
341
_canGenerateSymbolOutput = true ;
@@ -583,3 +583,13 @@ class _SymbolAddressUnit {
583
583
584
584
_SymbolAddressUnit (this .type, this .name, this .ptrName, this .native );
585
585
}
586
+
587
+ class _FindEnumsVisitation extends Visitation {
588
+ final enums = < EnumClass > {};
589
+
590
+ @override
591
+ void visitEnumClass (EnumClass node) {
592
+ node.visitChildren (visitor);
593
+ enums.add (node);
594
+ }
595
+ }
0 commit comments