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

dart-lang / ffigen / 4784152981

24 Apr 2023 08:39AM UTC coverage: 92.363% (-7.0%) from 99.366%
4784152981

push

github

GitHub
Merge pull request #560 from dart-lang/stable-to-7-x

1780 of 1780 new or added lines in 41 files covered. (100.0%)

3229 of 3496 relevant lines covered (92.36%)

25.17 hits per line

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

94.92
/lib/src/header_parser/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
import 'dart:io';
7

8
import 'package:ffi/ffi.dart';
9
import 'package:ffigen/src/code_generator.dart';
10
import 'package:ffigen/src/config_provider.dart';
11
import 'package:ffigen/src/config_provider/config_types.dart';
12
import 'package:ffigen/src/header_parser/sub_parsers/macro_parser.dart';
13
import 'package:ffigen/src/header_parser/translation_unit_parser.dart';
14
import 'package:ffigen/src/strings.dart' as strings;
15
import 'package:logging/logging.dart';
16

17
import 'clang_bindings/clang_bindings.dart' as clang_types;
18
import 'data.dart';
19
import 'utils.dart';
20

21
/// Main entrypoint for header_parser.
22
Library parse(Config c) {
47✔
23
  initParser(c);
47✔
24

25
  final bindings = parseToBindings();
47✔
26

27
  final library = Library(
47✔
28
    bindings: bindings,
29
    name: config.wrapperName,
94✔
30
    description: config.wrapperDocComment,
94✔
31
    header: config.preamble,
94✔
32
    sort: config.sort,
94✔
33
    packingOverride: config.structPackingOverride,
94✔
34
    libraryImports: c.libraryImports.values.toSet(),
141✔
35
  );
36

37
  return library;
38
}
39

40
// ===================================================================================
41
//           BELOW FUNCTIONS ARE MEANT FOR INTERNAL USE AND TESTING
42
// ===================================================================================
43

44
final _logger = Logger('ffigen.header_parser.parser');
141✔
45

46
/// Initializes parser, clears any previous values.
47
void initParser(Config c) {
47✔
48
  // Initialize global variables.
49
  initializeGlobals(
47✔
50
    config: c,
51
  );
52
}
53

54
/// Parses source files and adds generated bindings to [bindings].
55
List<Binding> parseToBindings() {
47✔
56
  final index = clang.clang_createIndex(0, 0);
94✔
57

58
  Pointer<Pointer<Utf8>> clangCmdArgs = nullptr;
47✔
59
  final compilerOpts = <String>[
47✔
60
    // Add compiler opt for comment parsing for clang based on config.
61
    if (config.commentType.length != CommentLength.none &&
188✔
62
        config.commentType.style == CommentStyle.any)
184✔
63
      strings.fparseAllComments,
2✔
64

65
    // If the config targets Objective C, add a compiler opt for it.
66
    if (config.language == Language.objc) ...[
158✔
67
      ...strings.clangLangObjC,
68
      ..._findObjectiveCSysroot(),
17✔
69
    ],
70

71
    // Add the user options last so they can override any other options.
72
    ...config.compilerOpts
94✔
73
  ];
74

75
  _logger.fine('CompilerOpts used: $compilerOpts');
141✔
76
  clangCmdArgs = createDynamicStringArray(compilerOpts);
47✔
77
  final cmdLen = compilerOpts.length;
47✔
78

79
  // Contains all bindings. A set ensures we never have duplicates.
80
  final bindings = <Binding>{};
81

82
  // Log all headers for user.
83
  _logger.info('Input Headers: ${config.headers.entryPoints}');
282✔
84

85
  final tuList = <Pointer<clang_types.CXTranslationUnitImpl>>[];
47✔
86

87
  // Parse all translation units from entry points.
88
  for (final headerLocation in config.headers.entryPoints) {
173✔
89
    _logger.fine('Creating TranslationUnit for header: $headerLocation');
96✔
90

91
    final tu = clang.clang_parseTranslationUnit(
64✔
92
      index,
93
      headerLocation.toNativeUtf8().cast(),
64✔
94
      clangCmdArgs.cast(),
32✔
95
      cmdLen,
96
      nullptr,
32✔
97
      0,
98
      clang_types.CXTranslationUnit_Flags.CXTranslationUnit_SkipFunctionBodies |
32✔
99
          clang_types.CXTranslationUnit_Flags
100
              .CXTranslationUnit_DetailedPreprocessingRecord,
101
    );
102

103
    if (tu == nullptr) {
64✔
104
      _logger.severe(
×
105
          "Skipped header/file: $headerLocation, couldn't parse source.");
×
106
      // Skip parsing this header.
107
      continue;
108
    }
109

110
    logTuDiagnostics(tu, _logger, headerLocation);
64✔
111
    tuList.add(tu);
32✔
112
  }
113

114
  final tuCursors =
115
      tuList.map((tu) => clang.clang_getTranslationUnitCursor(tu));
143✔
116

117
  // Build usr to CXCusror map from translation units.
118
  for (final rootCursor in tuCursors) {
79✔
119
    buildUsrCursorDefinitionMap(rootCursor);
32✔
120
  }
121

122
  // Parse definitions from translation units.
123
  for (final rootCursor in tuCursors) {
79✔
124
    bindings.addAll(parseTranslationUnit(rootCursor));
64✔
125
  }
126

127
  // Dispose translation units.
128
  for (final tu in tuList) {
79✔
129
    clang.clang_disposeTranslationUnit(tu);
64✔
130
  }
131

132
  // Add all saved unnamed enums.
133
  bindings.addAll(unnamedEnumConstants);
94✔
134

135
  // Parse all saved macros.
136
  bindings.addAll(parseSavedMacros()!);
94✔
137

138
  clangCmdArgs.dispose(cmdLen);
47✔
139
  clang.clang_disposeIndex(index);
94✔
140
  return bindings.toList();
47✔
141
}
142

143
List<String> _findObjectiveCSysroot() {
17✔
144
  final result = Process.runSync('xcrun', ['--show-sdk-path']);
34✔
145
  if (result.exitCode == 0) {
34✔
146
    for (final line in (result.stdout as String).split('\n')) {
51✔
147
      if (line.isNotEmpty) {
17✔
148
        return ['-isysroot', line];
17✔
149
      }
150
    }
151
  }
152
  return [];
×
153
}
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

© 2026 Coveralls, Inc