From 3c82042633f5fc089f815583a09c8d90d5b3d430 Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Fri, 12 Sep 2025 17:41:43 +0100 Subject: [PATCH] [cxx-interop] Import complex macros consistently in C++ language mode This is similar to ced4cb06. 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 --- lib/ClangImporter/ImportMacro.cpp | 12 ++++++++---- test/ClangImporter/macros.swift | 4 +--- .../Interop/Cxx/objc-correctness/darwin-macros.swift | 7 +++++++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 test/Interop/Cxx/objc-correctness/darwin-macros.swift diff --git a/lib/ClangImporter/ImportMacro.cpp b/lib/ClangImporter/ImportMacro.cpp index a932ba05cc55e..ada666b6b8612 100644 --- a/lib/ClangImporter/ImportMacro.cpp +++ b/lib/ClangImporter/ImportMacro.cpp @@ -333,12 +333,16 @@ getIntegerConstantForMacroToken(ClangImporter::Implementation &impl, } // Macro identifier. - // TODO: for some reason when in C++ mode, "hasMacroDefinition" is often - // false: rdar://110071334 - } else if (token.is(clang::tok::identifier) && - token.getIdentifierInfo()->hasMacroDefinition()) { + } else if (token.is(clang::tok::identifier)) { auto rawID = token.getIdentifierInfo(); + + // When importing in (Objective-)C++ language mode, sometimes a macro might + // have an outdated identifier info, which would cause Clang preprocessor to + // assume that it does not have a definition. + if (rawID->isOutOfDate()) + (void)impl.getClangPreprocessor().getLeafModuleMacros(rawID); + auto definition = impl.getClangPreprocessor().getMacroDefinition(rawID); if (!definition) return std::nullopt; diff --git a/test/ClangImporter/macros.swift b/test/ClangImporter/macros.swift index 6a9f07be9f54c..a25354d17cb1b 100644 --- a/test/ClangImporter/macros.swift +++ b/test/ClangImporter/macros.swift @@ -1,7 +1,5 @@ // RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -typecheck -verify %s - -// Most of these don't pass: rdar://110071334 -// %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-cxx-interop -enable-objc-interop -typecheck -verify %s +// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -cxx-interoperability-mode=default -enable-objc-interop -typecheck -verify %s @_exported import macros diff --git a/test/Interop/Cxx/objc-correctness/darwin-macros.swift b/test/Interop/Cxx/objc-correctness/darwin-macros.swift new file mode 100644 index 0000000000000..77f52a632f32d --- /dev/null +++ b/test/Interop/Cxx/objc-correctness/darwin-macros.swift @@ -0,0 +1,7 @@ +// RUN: %target-swift-frontend -cxx-interoperability-mode=default -typecheck -verify -I %S/Inputs %s + +// REQUIRES: OS=macosx + +import Darwin + +let _ = COPYFILE_ALL