Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ffigen] Improve enum warning #1717

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkgs/ffigen/lib/src/code_generator/enum_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class EnumClass extends BindingType {

@override
String getCType(Writer w) {
w.usedEnumCType = true;
w.usedEnumCTypes.add(this);
return nativeType.getCType(w);
}

Expand Down
15 changes: 8 additions & 7 deletions pkgs/ffigen/lib/src/code_generator/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class Writer {

final List<String> nativeEntryPoints;

/// Tracks where enumType.getCType is called. Reset everytime [generate] is
/// called.
bool usedEnumCType = false;
/// Tracks the enums for which enumType.getCType is called. Reset everytime
/// [generate] is called.
final usedEnumCTypes = <EnumClass>{};

String? _ffiLibraryPrefix;
String get ffiLibraryPrefix {
Expand Down Expand Up @@ -243,8 +243,8 @@ class Writer {
// Reset unique namers to initial state.
_resetUniqueNamersNamers();

// Reset [usedEnumCType].
usedEnumCType = false;
// Reset [usedEnumCTypes].
usedEnumCTypes.clear();

// Write file header (if any).
if (header != null) {
Expand Down Expand Up @@ -333,14 +333,15 @@ class Writer {
result.write(s);

// Warn about Enum usage in API surface.
if (!silenceEnumWarning && usedEnumCType) {
if (!silenceEnumWarning && usedEnumCTypes.isNotEmpty) {
final names = usedEnumCTypes.map((e) => e.originalName).toList()..sort();
_logger.severe('The integer type used for enums is '
'implementation-defined. FFIgen tries to mimic the integer sizes '
'chosen by the most common compilers for the various OS and '
'architecture combinations. To prevent any crashes, remove the '
'enums from your API surface. To rely on the (unsafe!) mimicking, '
'you can silence this warning by adding silence-enum-warning: true '
'to the FFIgen config.');
'to the FFIgen config. Affected enums:\n\t${names.join('\n\t')}');
}

_canGenerateSymbolOutput = true;
Expand Down
Loading