Skip to content

Commit 6bf0f26

Browse files
committed
Emit /DEFAULTLIB directive for #pragma comment(lib, ...) in a C module.
This is important, for example, for linking correctly to the C++ standard library on Windows, which uses `#pragma comment(lib, ...)` in its headers to specify the correct library to link against.
1 parent c230622 commit 6bf0f26

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

lib/IRGen/GenClangDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ IRGenModule::getAddrOfClangGlobalDecl(clang::GlobalDecl global,
113113
}
114114

115115
void IRGenModule::finalizeClangCodeGen() {
116+
// Ensure that code is emitted for any `PragmaCommentDecl`s. (These are
117+
// always guaranteed to be directly below the TranslationUnitDecl.)
118+
// In Clang, this happens automatically during the Sema phase, but here we
119+
// need to take care of it manually because our Clang CodeGenerator is not
120+
// attached to Clang Sema as an ASTConsumer.
121+
for (const auto *D : ClangASTContext->getTranslationUnitDecl()->decls()) {
122+
if (const auto *PCD = dyn_cast<clang::PragmaCommentDecl>(D)) {
123+
emitClangDecl(PCD);
124+
}
125+
}
126+
116127
ClangCodeGen->HandleTranslationUnit(
117128
*const_cast<clang::ASTContext *>(ClangASTContext));
118129
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma comment(lib, "transitive-module")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "autolink-coff-c-pragma-transitive.h"
2+
3+
#pragma comment(lib, "module")

test/IRGen/Inputs/module.modulemap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module AutolinkCoffCPragma {
2+
header "autolink-coff-c-pragma.h"
3+
export *
4+
}
5+
6+
module AutolinkCoffCPragmaTransitive {
7+
header "autolink-coff-c-pragma-transitive.h"
8+
export *
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Tests that a `#pragma comment(lib, ...)` in a C header imported as a module
2+
// causes a corresponding `/DEFAULTLIB` directive to be emitted.
3+
//
4+
// We test that this is true also for C headers included transitively from
5+
// another C header.
6+
7+
// RUN: %swift -module-name Swift -target x86_64-unknown-windows-msvc -I %S/Inputs -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=CHECK-MSVC-IR
8+
// RUN: %swift -module-name Swift -target x86_64-unknown-windows-msvc -I %S/Inputs -S %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=CHECK-MSVC-ASM
9+
10+
// REQUIRES: CODEGENERATOR=X86
11+
12+
import AutolinkCoffCPragma
13+
14+
// CHECK-MSVC-IR: !llvm.linker.options = !{!{{[0-9]+}}, !{{[0-9]+}}}
15+
// CHECK-MSVC-IR-DAG: !{{[0-9]+}} = !{!"/DEFAULTLIB:module.lib"}
16+
// CHECK-MSVC-IR-DAG: !{{[0-9]+}} = !{!"/DEFAULTLIB:transitive-module.lib"}
17+
18+
// CHECK-MSVC-ASM: .section .drectve
19+
// CHECK-MSVC-ASM-DAG: .ascii " /DEFAULTLIB:module.lib"
20+
// CHECK-MSVC-ASM-DAG: .ascii " /DEFAULTLIB:transitive-module.lib"

0 commit comments

Comments
 (0)