-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-2402] Clang importer should import nullary function-like macros #45009
Comments
I've looked into this a bit. I think I'll be able to implement this by the end of January at the latest (feel free to steal this task from me if I don't have a pull request out by then). Some notes: First of all, I'm using the following files as a test bed: // MyCMacro.h
#define MY_C_MACRO() 1 // MySwift.swift
let x = MY_C_MACRO() I compile these files using the following command: swift -frontend -c -primary-file MySwift.swift -import-objc-header MyCMacro.h -module-name MySwift -o MySwift.o But of course this compilation fails, which is why this Swift report exists: MySwift.swift:1:9: error: use of unresolved identifier 'MY_C_MACRO'
let x = MY_C_MACRO()
^~~~~~~~~~ I've learned that the ClangImporter works in two fundamental "phases." The first occurs when it is initialized during
diff --git a/lib/ClangImporter/ImportName.cpp b/lib/ClangImporter/ImportName.cpp
index aceb53b..d47ce1f 100644
--- a/lib/ClangImporter/ImportName.cpp
+++ b/lib/ClangImporter/ImportName.cpp
@@ -1597,11 +1597,11 @@ static bool shouldIgnoreMacro(StringRef name, const clang::MacroInfo *macro) {
if (macro->tokens_empty())
return true;
- // Currently we only convert non-function-like macros.
- if (macro->isFunctionLike())
+ // Currently we only convert function-like macros that take zero arguments.
+ if (macro->isFunctionLike() && macro->getNumArgs() != 0)
return true; With this change, the test files above still fail to compile, but this time with a different error: MySwift.swift:1:23: error: cannot call value of non-function type 'Int32'
let x = MY_C_MACRO()
~~~~~~~~~~^~ In other words, Determining the type of a C macro is done during ClangImporter's second "phase." That phase occurs during the Swift compiler's semantic analysis of Swift source code. As the Swift compiler attempts to resolve the identifiers and types used in Swift source code, it consults the ClangImporter's lookup tables. Only once the Swift compiler sees that Unfortunately, based on what I've seen, it looks like the ClangImporter is hardcoded to parse swift::ClangImporter::Implementation::lookupValue at ClangImporter.cpp:2573
swift::ClangImporter::Implementation::importMacro(..., name=(Pointer = "MY_C_MACRO"), ...) at ImportMacro.cpp:610
importMacro(..., name=(Pointer = "MY_C_MACRO"), ...) at ImportMacro.cpp:384
importLiteral(..., name=(Pointer = "MY_C_MACRO"), ...) at ImportMacro.cpp:234
importNumericLiteral(..., name=(Pointer = "MY_C_MACRO"), ...) at ImportMacro.cpp:147
createMacroConstant(..., name=(Pointer = "MY_C_MACRO"), ...) at ImportMacro.cpp:86
swift::ClangImporter::Implementation::createConstant(..., name=(Pointer = "MY_C_MACRO"), ...) at ImportDecl.cpp:7018 So it seems like I'll need to modify the |
I submitted a pull request for this: #6530 Some feedback would be great – I'm not sure if the implementation is satisfactory, and importing macros from other modules doesn't work for some reason. Any ideas? |
I don't have a ton of time to work on this anymore. Still, it was a fun way to learn more about ClangImporter, so thank you! |
Additional Detail from JIRA
md5: 920059f64cf3b7c14c4a3ebcde06d5eb
relates to:
Issue Description:
If a function-like macro takes no arguments, and has a body we would otherwise be able to import, we should be able to make a dummy function the same way we make dummy constants for non-function-like macros.
The text was updated successfully, but these errors were encountered: