Skip to content

Commit 5539cec

Browse files
committed
WIP
1 parent cb3293b commit 5539cec

File tree

11 files changed

+102
-3
lines changed

11 files changed

+102
-3
lines changed

include/swift/Subsystems.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ namespace swift {
126126
/// Resolve imports for a source file generated to adapt a given
127127
/// Clang module.
128128
void performImportResolutionForClangMacroBuffer(
129-
SourceFile &SF, ModuleDecl *clangModule
129+
SourceFile &SF, ModuleDecl *clangModule, const clang::Module *M
130130
);
131131

132132
/// Once type-checking is complete, this instruments code with calls to an

lib/Sema/ImportResolution.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void swift::performImportResolution(SourceFile &SF) {
320320
}
321321

322322
void swift::performImportResolutionForClangMacroBuffer(
323-
SourceFile &SF, ModuleDecl *clangModule
323+
SourceFile &SF, ModuleDecl *clangModule, const clang::Module *M
324324
) {
325325
// If we've already performed import resolution, bail.
326326
if (SF.ASTStage == SourceFile::ImportsResolved)
@@ -329,6 +329,21 @@ void swift::performImportResolutionForClangMacroBuffer(
329329
ImportResolver resolver(SF);
330330
resolver.addImplicitImport(clangModule);
331331

332+
ASTContext &ctx = clangModule->getASTContext();
333+
auto *clangImporter = ctx.getClangModuleLoader();
334+
llvm::dbgs() << "adding self from actual clang module:\n";
335+
M->dump();
336+
auto wrapper = clangImporter->getWrapperForModule(M);
337+
wrapper->dump();
338+
resolver.addImplicitImport(wrapper);
339+
for (auto &I : M->Imports) {
340+
llvm::dbgs() << "adding import from actual clang module:\n";
341+
I->dump();
342+
auto wrapper = clangImporter->getWrapperForModule(I);
343+
wrapper->dump();
344+
resolver.addImplicitImport(wrapper);
345+
}
346+
332347
// FIXME: This is a hack that we shouldn't need, but be sure that we can
333348
// see the Swift standard library.
334349
if (auto stdlib = SF.getASTContext().getStdlibModule())

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,9 @@ createMacroSourceFile(std::unique_ptr<llvm::MemoryBuffer> buffer,
10621062
else if (auto clangModuleUnit =
10631063
dyn_cast<ClangModuleUnit>(dc->getModuleScopeContext())) {
10641064
auto clangModule = clangModuleUnit->getParentModule();
1065-
performImportResolutionForClangMacroBuffer(*macroSourceFile, clangModule);
1065+
const clang::Decl *clangDecl = target.get<Decl*>()->getClangDecl();
1066+
const clang::Module *M = clangDecl->getOwningModule();
1067+
performImportResolutionForClangMacroBuffer(*macroSourceFile, clangModule, M);
10661068
}
10671069
return macroSourceFile;
10681070
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
typedef int a_t;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
typedef int b_t;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#pragma once
2+
typedef int c_t;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ModuleA {
2+
header "module-a.h"
3+
export *
4+
}
5+
module ModuleB {
6+
header "module-b.h"
7+
}
8+
module ModuleOuter {
9+
module ModuleC {
10+
header "module-c.h"
11+
export *
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include "TransitiveModules/module-a.h"
4+
#include "TransitiveModules/module-b.h"
5+
#include "TransitiveModules/module-c.h"
6+
7+
#define __counted_by(x) __attribute__((__counted_by__(x)))
8+
#define __noescape __attribute__((noescape))
9+
10+
void basic_include(const a_t *__counted_by(len) p __noescape, a_t len);
11+
12+
void non_exported_include(const b_t *__counted_by(len) p __noescape, b_t len);
13+
14+
void submodule_include(const c_t *__counted_by(len) p __noescape, c_t len);

test/Interop/C/swiftify-import/Inputs/module.modulemap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ module CommentsClang {
2626
header "comments.h"
2727
export *
2828
}
29+
module ClangIncludesModule {
30+
header "clang-includes.h"
31+
export *
32+
}
33+
module ClangIncludesNoExportModule {
34+
header "clang-includes.h"
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// REQUIRES: swift_feature_SafeInteropWrappers
2+
3+
// RN: %target-swift-ide-test -print-module -module-to-print=ClangIncludesNoExportModule -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s
4+
5+
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
6+
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesNoExport.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s
7+
8+
import ClangIncludesNoExportModule
9+
10+
public func callBasicInclude(_ p: Span<CInt>) {
11+
basic_include(p)
12+
}
13+
14+
public func callNonExported(_ p: Span<CInt>) {
15+
non_exported_include(p)
16+
}
17+
18+
public func callSubmoduleInclude(_ p: Span<CInt>) {
19+
submodule_include(p)
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// REQUIRES: swift_feature_SafeInteropWrappers
2+
3+
// RN: %target-swift-ide-test -print-module -module-to-print=ClangIncludesModule -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s
4+
5+
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
6+
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludes.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s
7+
8+
import ClangIncludesModule
9+
10+
public func callBasicInclude(_ p: Span<CInt>) {
11+
basic_include(p)
12+
}
13+
14+
public func callNonExported(_ p: Span<CInt>) {
15+
non_exported_include(p)
16+
}
17+
18+
public func callSubmoduleInclude(_ p: Span<CInt>) {
19+
submodule_include(p)
20+
}

0 commit comments

Comments
 (0)