Skip to content

Commit 3c82042

Browse files
committed
[cxx-interop] Import complex macros consistently in C++ language mode
This is similar to ced4cb0. Clang stores macro information for each identifier inside of `IdentifierInfo`, which can sometimes get outdated. Clang solves this by updating the identifier info if necessary in `getLeafModuleMacros`. Let's do the same in Swift. This makes sure that macros that reference other macros are correctly imported with C++ interop enabled. rdar://145584369 rdar://110071334
1 parent 3ee222f commit 3c82042

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

lib/ClangImporter/ImportMacro.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,16 @@ getIntegerConstantForMacroToken(ClangImporter::Implementation &impl,
333333
}
334334

335335
// Macro identifier.
336-
// TODO: for some reason when in C++ mode, "hasMacroDefinition" is often
337-
// false: rdar://110071334
338-
} else if (token.is(clang::tok::identifier) &&
339-
token.getIdentifierInfo()->hasMacroDefinition()) {
336+
} else if (token.is(clang::tok::identifier)) {
340337

341338
auto rawID = token.getIdentifierInfo();
339+
340+
// When importing in (Objective-)C++ language mode, sometimes a macro might
341+
// have an outdated identifier info, which would cause Clang preprocessor to
342+
// assume that it does not have a definition.
343+
if (rawID->isOutOfDate())
344+
(void)impl.getClangPreprocessor().getLeafModuleMacros(rawID);
345+
342346
auto definition = impl.getClangPreprocessor().getMacroDefinition(rawID);
343347
if (!definition)
344348
return std::nullopt;

test/ClangImporter/macros.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -typecheck -verify %s
2-
3-
// Most of these don't pass: rdar://110071334
4-
// %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-cxx-interop -enable-objc-interop -typecheck -verify %s
2+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -cxx-interoperability-mode=default -enable-objc-interop -typecheck -verify %s
53

64
@_exported import macros
75

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-swift-frontend -cxx-interoperability-mode=default -typecheck -verify -I %S/Inputs %s
2+
3+
// REQUIRES: OS=macosx
4+
5+
import Darwin
6+
7+
let _ = COPYFILE_ALL

0 commit comments

Comments
 (0)