• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dart-lang / ffigen / 4364636722

pending completion
4364636722

Pull #527

github

GitHub
Merge 984472b2c into 1ac20ffd5
Pull Request #527: Bump dependencies and fix lints

21 of 21 new or added lines in 10 files covered. (100.0%)

3188 of 3452 relevant lines covered (92.35%)

24.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.1
/lib/src/header_parser/translation_unit_parser.dart
1
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4

5
import 'dart:ffi';
6

7
import 'package:ffigen/src/code_generator.dart';
8
import 'package:ffigen/src/header_parser/sub_parsers/macro_parser.dart';
9
import 'package:ffigen/src/header_parser/sub_parsers/objcinterfacedecl_parser.dart';
10
import 'package:ffigen/src/header_parser/sub_parsers/var_parser.dart';
11
import 'package:logging/logging.dart';
12

13
import 'clang_bindings/clang_bindings.dart' as clang_types;
14
import 'data.dart';
15
import 'includer.dart';
16
import 'sub_parsers/functiondecl_parser.dart';
17
import 'type_extractor/extractor.dart';
18
import 'utils.dart';
19

20
final _logger = Logger('ffigen.header_parser.translation_unit_parser');
93✔
21

22
late Set<Binding> _bindings;
31✔
23

24
Pointer<
25
        NativeFunction<
26
            Int32 Function(
27
                clang_types.CXCursor, clang_types.CXCursor, Pointer<Void>)>>?
28
    _rootCursorVisitorPtr;
29

30
Pointer<
31
        NativeFunction<
32
            Int32 Function(
33
                clang_types.CXCursor, clang_types.CXCursor, Pointer<Void>)>>?
34
    _cursorDefinitionVisitorPtr;
35

36
/// Parses the translation unit and returns the generated bindings.
37
Set<Binding> parseTranslationUnit(clang_types.CXCursor translationUnitCursor) {
31✔
38
  _bindings = {};
39
  final resultCode = clang.clang_visitChildren(
62✔
40
    translationUnitCursor,
41
    _rootCursorVisitorPtr ??=
42
        Pointer.fromFunction(_rootCursorVisitor, exceptional_visitor_return),
43
    nullptr,
31✔
44
  );
45

46
  visitChildrenResultChecker(resultCode);
31✔
47

48
  return _bindings;
31✔
49
}
50

51
/// Child visitor invoked on translationUnitCursor [CXCursorKind.CXCursor_TranslationUnit].
52
int _rootCursorVisitor(clang_types.CXCursor cursor, clang_types.CXCursor parent,
31✔
53
    Pointer<Void> clientData) {
54
  try {
55
    if (shouldIncludeRootCursor(cursor.sourceFileName())) {
62✔
56
      _logger.finest('rootCursorVisitor: ${cursor.completeStringRepr()}');
124✔
57
      switch (clang.clang_getCursorKind(cursor)) {
62✔
58
        case clang_types.CXCursorKind.CXCursor_FunctionDecl:
31✔
59
          addToBindings(parseFunctionDeclaration(cursor));
44✔
60
          break;
61
        case clang_types.CXCursorKind.CXCursor_StructDecl:
29✔
62
        case clang_types.CXCursorKind.CXCursor_UnionDecl:
23✔
63
        case clang_types.CXCursorKind.CXCursor_EnumDecl:
22✔
64
        case clang_types.CXCursorKind.CXCursor_ObjCInterfaceDecl:
21✔
65
          addToBindings(_getCodeGenTypeFromCursor(cursor));
46✔
66
          break;
67
        case clang_types.CXCursorKind.CXCursor_ObjCCategoryDecl:
21✔
68
          addToBindings(parseObjCCategoryDeclaration(cursor));
×
69
          break;
70
        case clang_types.CXCursorKind.CXCursor_MacroDefinition:
21✔
71
          saveMacroDefinition(cursor);
12✔
72
          break;
73
        case clang_types.CXCursorKind.CXCursor_VarDecl:
21✔
74
          addToBindings(parseVarDeclaration(cursor));
6✔
75
          break;
76
        default:
77
          _logger.finer('rootCursorVisitor: CursorKind not implemented');
40✔
78
      }
79
    } else {
80
      _logger.finest(
62✔
81
          'rootCursorVisitor:(not included) ${cursor.completeStringRepr()}');
62✔
82
    }
83
  } catch (e, s) {
84
    _logger.severe(e);
×
85
    _logger.severe(s);
×
86
    rethrow;
87
  }
88
  return clang_types.CXChildVisitResult.CXChildVisit_Continue;
89
}
90

91
/// Adds to binding if unseen and not null.
92
void addToBindings(Binding? b) {
30✔
93
  if (b != null) {
94
    // This is a set, and hence will not have duplicates.
95
    _bindings.add(b);
58✔
96
  }
97
}
98

99
BindingType? _getCodeGenTypeFromCursor(clang_types.CXCursor cursor) {
25✔
100
  final t = getCodeGenType(cursor.type(), ignoreFilter: false);
50✔
101
  return t is BindingType ? t : null;
25✔
102
}
103

104
/// Visits all cursors and builds a map of usr and [CXCursor].
105
void buildUsrCursorDefinitionMap(clang_types.CXCursor translationUnitCursor) {
31✔
106
  _bindings = {};
107
  final resultCode = clang.clang_visitChildren(
62✔
108
    translationUnitCursor,
109
    _cursorDefinitionVisitorPtr ??= Pointer.fromFunction(
110
        _definitionCursorVisitor, exceptional_visitor_return),
111
    nullptr,
31✔
112
  );
113

114
  visitChildrenResultChecker(resultCode);
31✔
115
}
116

117
/// Child visitor invoked on translationUnitCursor [CXCursorKind.CXCursor_TranslationUnit].
118
int _definitionCursorVisitor(clang_types.CXCursor cursor,
31✔
119
    clang_types.CXCursor parent, Pointer<Void> clientData) {
120
  try {
121
    cursorIndex.saveDefinition(cursor);
62✔
122
  } catch (e, s) {
123
    _logger.severe(e);
×
124
    _logger.severe(s);
×
125
    rethrow;
126
  }
127
  return clang_types.CXChildVisitResult.CXChildVisit_Continue;
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc