Fix macro file parser (#524)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2861c65..5144ff0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 7.2.7
+
+- Fix some macros not being generated in some cases due to relative header paths.
+
# 7.2.6
- Fix path normalization behaviour for absolute paths and globs starting with `**`.
diff --git a/lib/src/header_parser/sub_parsers/macro_parser.dart b/lib/src/header_parser/sub_parsers/macro_parser.dart
index 7478ea8..535e46d 100644
--- a/lib/src/header_parser/sub_parsers/macro_parser.dart
+++ b/lib/src/header_parser/sub_parsers/macro_parser.dart
@@ -68,9 +68,6 @@
var cmdLen = 0;
final compilerOpts = config.compilerOpts;
- config.headers.entryPoints.followedBy([file.path]).forEach((entryFile) {
- compilerOpts.add("-imacros$entryFile");
- });
clangCmdArgs = createDynamicStringArray(compilerOpts);
cmdLen = config.compilerOpts.length;
@@ -87,6 +84,7 @@
if (tu == nullptr) {
_logger.severe('Unable to parse Macros.');
} else {
+ logTuDiagnostics(tu, _logger, file.path, logLevel: Level.FINEST);
final rootCursor = clang.clang_getTranslationUnitCursor(tu);
final resultCode = clang.clang_visitChildren(
@@ -205,7 +203,8 @@
// Write file contents.
final sb = StringBuffer();
for (final h in config.headers.entryPoints) {
- sb.writeln('#include "$h"');
+ final fullHeaderPath = File(h).absolute.path;
+ sb.writeln('#include "$fullHeaderPath"');
}
_macroVarNames = {};
diff --git a/lib/src/header_parser/utils.dart b/lib/src/header_parser/utils.dart
index d53cb8b..355bf3a 100644
--- a/lib/src/header_parser/utils.dart
+++ b/lib/src/header_parser/utils.dart
@@ -30,16 +30,13 @@
/// Logs the warnings/errors returned by clang for a translation unit.
void logTuDiagnostics(
- Pointer<clang_types.CXTranslationUnitImpl> tu,
- Logger logger,
- String header,
-) {
+ Pointer<clang_types.CXTranslationUnitImpl> tu, Logger logger, String header,
+ {Level logLevel = Level.SEVERE}) {
final total = clang.clang_getNumDiagnostics(tu);
if (total == 0) {
return;
}
-
- logger.severe('Header $header: Total errors/warnings: $total.');
+ logger.log(logLevel, 'Header $header: Total errors/warnings: $total.');
for (var i = 0; i < total; i++) {
final diag = clang.clang_getDiagnostic(tu, i);
final cxstring = clang.clang_formatDiagnostic(
@@ -50,7 +47,7 @@
clang_types
.CXDiagnosticDisplayOptions.CXDiagnostic_DisplayCategoryName,
);
- logger.severe(' ' + cxstring.toStringAndDispose());
+ logger.log(logLevel, ' ' + cxstring.toStringAndDispose());
clang.clang_disposeDiagnostic(diag);
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index dbb5194..015c592 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.
name: ffigen
-version: 7.2.6
+version: 7.2.7
description: Generator for FFI bindings, using LibClang to parse C header files.
repository: https://github.com/dart-lang/ffigen
diff --git a/test/header_parser_tests/macros.h b/test/header_parser_tests/macros.h
index 90bbd45..5cae837 100644
--- a/test/header_parser_tests/macros.h
+++ b/test/header_parser_tests/macros.h
@@ -30,3 +30,16 @@
#define TEST14 INFINITY
#define TEST15 -INFINITY
#define TEST16 NAN
+
+
+// Inside include guard.
+#ifndef __TEST_INCLUDE_GUARD_1__
+#define __TEST_INCLUDE_GUARD_1__
+
+#define TEST17 TEST_FUNCTION_MACRO(0)
+#define TEST18 TEST_FUNCTION_MACRO(1)
+#define TEST19 TEST_FUNCTION_MACRO(2)
+
+#define TEST_FUNCTION_MACRO(x) ((unsigned long)((x) << (2)))
+
+#endif /* __TEST_INCLUDE_GUARD_1__ */
diff --git a/test/header_parser_tests/macros_test.dart b/test/header_parser_tests/macros_test.dart
index d615b26..689a289 100644
--- a/test/header_parser_tests/macros_test.dart
+++ b/test/header_parser_tests/macros_test.dart
@@ -98,6 +98,18 @@
expect(actual.getBindingAsString('TEST16'),
expected.getBindingAsString('TEST16'));
});
+ test('TEST17', () {
+ expect(actual.getBindingAsString('TEST17'),
+ expected.getBindingAsString('TEST17'));
+ });
+ test('TEST18', () {
+ expect(actual.getBindingAsString('TEST18'),
+ expected.getBindingAsString('TEST18'));
+ });
+ test('TEST19', () {
+ expect(actual.getBindingAsString('TEST19'),
+ expected.getBindingAsString('TEST19'));
+ });
});
}
@@ -125,6 +137,9 @@
rawType: 'double',
rawValue: strings.doubleNegativeInfinity),
Constant(name: 'TEST16', rawType: 'double', rawValue: strings.doubleNaN),
+ Constant(name: 'TEST17', rawType: 'int', rawValue: "0"),
+ Constant(name: 'TEST18', rawType: 'int', rawValue: "4"),
+ Constant(name: 'TEST19', rawType: 'int', rawValue: "8"),
],
);
}