Skip to content

Commit

Permalink
nit
Browse files Browse the repository at this point in the history
  • Loading branch information
mannprerak2 committed Nov 18, 2023
1 parent 97ea8ba commit 661d6e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
5 changes: 5 additions & 0 deletions pkgs/ffigen/lib/src/header_parser/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ List<Constant> _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));
Expand All @@ -54,4 +58,5 @@ void initializeGlobals({required Config config}) {
_cursorIndex = CursorIndex();
_bindingsIndex = BindingsIndex();
_objCBuiltInFunctions = ObjCBuiltInFunctions();
hasSourceErrors = false;
}
9 changes: 2 additions & 7 deletions pkgs/ffigen/lib/src/header_parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -85,8 +84,6 @@ List<Binding> parseToBindings(Config c) {

final tuList = <Pointer<clang_types.CXTranslationUnitImpl>>[];

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');
Expand All @@ -112,13 +109,11 @@ List<Binding> 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) {
Expand Down
15 changes: 6 additions & 9 deletions pkgs/ffigen/lib/src/header_parser/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<clang_types.CXTranslationUnitImpl> 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
Expand All @@ -56,7 +54,6 @@ int logTuDiagnostics(
logger.log(logLevel, ' ${cxstring.toStringAndDispose()}');
clang.clang_disposeDiagnostic(diag);
}
return result;
}

extension CXSourceRangeExt on Pointer<clang_types.CXSourceRange> {
Expand Down

0 comments on commit 661d6e2

Please sign in to comment.