From 661d6e2414ee59889316fde7dc68fb2eaf0e55f1 Mon Sep 17 00:00:00 2001 From: Prerak Mann Date: Sat, 18 Nov 2023 19:00:34 +0530 Subject: [PATCH] nit --- pkgs/ffigen/lib/src/header_parser/data.dart | 5 +++++ pkgs/ffigen/lib/src/header_parser/parser.dart | 9 ++------- pkgs/ffigen/lib/src/header_parser/utils.dart | 15 ++++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pkgs/ffigen/lib/src/header_parser/data.dart b/pkgs/ffigen/lib/src/header_parser/data.dart index 5bd4e903a..1ee8f27d4 100644 --- a/pkgs/ffigen/lib/src/header_parser/data.dart +++ b/pkgs/ffigen/lib/src/header_parser/data.dart @@ -45,6 +45,10 @@ List _unnamedEnumConstants = []; ObjCBuiltInFunctions get objCBuiltInFunctions => _objCBuiltInFunctions; late ObjCBuiltInFunctions _objCBuiltInFunctions; +/// Tracks if any source error/warning has occured which can potentially cause +/// invalid generated bindings. +bool hasSourceErrors = false; + void initializeGlobals({required Config config}) { _config = config; _clang = Clang(DynamicLibrary.open(config.libclangDylib)); @@ -54,4 +58,5 @@ void initializeGlobals({required Config config}) { _cursorIndex = CursorIndex(); _bindingsIndex = BindingsIndex(); _objCBuiltInFunctions = ObjCBuiltInFunctions(); + hasSourceErrors = false; } diff --git a/pkgs/ffigen/lib/src/header_parser/parser.dart b/pkgs/ffigen/lib/src/header_parser/parser.dart index 893cc6115..c7ff18995 100644 --- a/pkgs/ffigen/lib/src/header_parser/parser.dart +++ b/pkgs/ffigen/lib/src/header_parser/parser.dart @@ -4,7 +4,6 @@ import 'dart:ffi'; import 'dart:io'; -import 'dart:math' show max; import 'package:ffi/ffi.dart'; import 'package:ffigen/src/code_generator.dart'; @@ -85,8 +84,6 @@ List parseToBindings(Config c) { final tuList = >[]; - var highestDiagnosticLevel = - clang_types.CXDiagnosticSeverity.CXDiagnostic_Ignored; // Parse all translation units from entry points. for (final headerLocation in config.headers.entryPoints) { _logger.fine('Creating TranslationUnit for header: $headerLocation'); @@ -112,13 +109,11 @@ List parseToBindings(Config c) { continue; } - final diagnosticsLevel = logTuDiagnostics(tu, _logger, headerLocation); - highestDiagnosticLevel = max(highestDiagnosticLevel, diagnosticsLevel); + logTuDiagnostics(tu, _logger, headerLocation); tuList.add(tu); } - if (highestDiagnosticLevel >= - clang_types.CXDiagnosticSeverity.CXDiagnostic_Warning) { + if (hasSourceErrors) { _logger.severe( "The compiler found some warnings/errors in source files. This might generate invalid bindings due to a wrong compiler guess."); if (config.ignoreSourceErrors) { diff --git a/pkgs/ffigen/lib/src/header_parser/utils.dart b/pkgs/ffigen/lib/src/header_parser/utils.dart index 3945b9665..423d7cdba 100644 --- a/pkgs/ffigen/lib/src/header_parser/utils.dart +++ b/pkgs/ffigen/lib/src/header_parser/utils.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'dart:ffi'; -import 'dart:math' show max; import 'package:ffi/ffi.dart'; import 'package:ffigen/src/code_generator.dart'; @@ -30,21 +29,20 @@ void visitChildrenResultChecker(int resultCode) { } /// Logs the warnings/errors returned by clang for a translation unit. -/// -/// Returns the highest [clang_types.CXDiagnosticSeverity] seen, defaults to -/// [clang_types.CXDiagnosticSeverity.CXDiagnostic_Ignored] if none. -int logTuDiagnostics( +void logTuDiagnostics( Pointer tu, Logger logger, String header, {Level logLevel = Level.SEVERE}) { - var result = clang_types.CXDiagnosticSeverity.CXDiagnostic_Ignored; final total = clang.clang_getNumDiagnostics(tu); if (total == 0) { - return result; + return; } logger.log(logLevel, 'Header $header: Total errors/warnings: $total.'); for (var i = 0; i < total; i++) { final diag = clang.clang_getDiagnostic(tu, i); - result = max(result, clang.clang_getDiagnosticSeverity(diag)); + if (clang.clang_getDiagnosticSeverity(diag) >= + clang_types.CXDiagnosticSeverity.CXDiagnostic_Warning) { + hasSourceErrors = true; + } final cxstring = clang.clang_formatDiagnostic( diag, clang_types @@ -56,7 +54,6 @@ int logTuDiagnostics( logger.log(logLevel, ' ${cxstring.toStringAndDispose()}'); clang.clang_disposeDiagnostic(diag); } - return result; } extension CXSourceRangeExt on Pointer {