@@ -319,6 +319,21 @@ static void writeImports(raw_ostream &out,
319
319
320
320
// Track printed names to handle overlay modules.
321
321
llvm::SmallPtrSet<Identifier, 8 > seenImports;
322
+ llvm::SmallString<256 > allPaths;
323
+ llvm::SmallSetVector<StringRef, 8 > headerImports;
324
+ auto insertHeaderPath = [&](clang::Module::Header header,
325
+ const clang::Module *module ) {
326
+ auto startIdx = allPaths.size ();
327
+ if (module ->IsFramework ) {
328
+ // For framworks, the header import should start from the framework name.
329
+ allPaths.append (module ->getTopLevelModuleName ());
330
+ llvm::sys::path::append (allPaths, header.NameAsWritten );
331
+ } else {
332
+ // Otherwise, import the header directly.
333
+ allPaths.append (header.NameAsWritten );
334
+ }
335
+ headerImports.insert (allPaths.str ().substr (startIdx));
336
+ };
322
337
bool includeUnderlying = false ;
323
338
for (auto import : sortedImports) {
324
339
if (auto *swiftModule = import .dyn_cast <ModuleDecl *>()) {
@@ -327,17 +342,43 @@ static void writeImports(raw_ostream &out,
327
342
includeUnderlying = true ;
328
343
continue ;
329
344
}
330
- if (seenImports.insert (Name).second )
345
+ if (seenImports.insert (Name).second ) {
331
346
out << " @import " << Name.str () << " ;\n " ;
347
+ if (auto *clangM = swiftModule->findUnderlyingClangModule ()) {
348
+ if (auto umbrella = clangM->getUmbrellaHeader ()) {
349
+ // If an umbrella header is available, use that.
350
+ insertHeaderPath (umbrella, clangM);
351
+ } else {
352
+ // Collect all headers included in the module.
353
+ for (auto headers: clangM->Headers ) {
354
+ for (auto header: headers) {
355
+ insertHeaderPath (header, clangM);
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
332
361
} else {
333
362
const auto *clangModule = import .get <const clang::Module *>();
334
363
assert (clangModule->isSubModule () &&
335
364
" top-level modules should use a normal swift::ModuleDecl" );
336
365
out << " @import " ;
337
366
ModuleDecl::ReverseFullNameIterator (clangModule).printForward (out);
338
367
out << " ;\n " ;
368
+ // Collect all headers included in the submodule
369
+ for (auto headers: clangModule->Headers ) {
370
+ for (auto header: headers) {
371
+ insertHeaderPath (header, clangModule);
372
+ }
373
+ }
339
374
}
340
375
}
376
+ out << " #else\n " ;
377
+
378
+ // We cannot use module import, so use header includes instead.
379
+ for (auto header: headerImports) {
380
+ out << " #import <" << header << " >\n " ;
381
+ }
341
382
342
383
out << " #endif\n\n " ;
343
384
0 commit comments