Skip to content

Commit

Permalink
Emit /DEFAULTLIB directive for #pragma comment(lib, ...) in a C m…
Browse files Browse the repository at this point in the history
…odule.

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.
  • Loading branch information
martinboehme committed May 20, 2020
1 parent c230622 commit 6bf0f26
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/IRGen/GenClangDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ IRGenModule::getAddrOfClangGlobalDecl(clang::GlobalDecl global,
}

void IRGenModule::finalizeClangCodeGen() {
// Ensure that code is emitted for any `PragmaCommentDecl`s. (These are
// always guaranteed to be directly below the TranslationUnitDecl.)
// In Clang, this happens automatically during the Sema phase, but here we
// need to take care of it manually because our Clang CodeGenerator is not
// attached to Clang Sema as an ASTConsumer.
for (const auto *D : ClangASTContext->getTranslationUnitDecl()->decls()) {
if (const auto *PCD = dyn_cast<clang::PragmaCommentDecl>(D)) {
emitClangDecl(PCD);
}
}

ClangCodeGen->HandleTranslationUnit(
*const_cast<clang::ASTContext *>(ClangASTContext));
}
1 change: 1 addition & 0 deletions test/IRGen/Inputs/autolink-coff-c-pragma-transitive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma comment(lib, "transitive-module")
3 changes: 3 additions & 0 deletions test/IRGen/Inputs/autolink-coff-c-pragma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "autolink-coff-c-pragma-transitive.h"

#pragma comment(lib, "module")
9 changes: 9 additions & 0 deletions test/IRGen/Inputs/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module AutolinkCoffCPragma {
header "autolink-coff-c-pragma.h"
export *
}

module AutolinkCoffCPragmaTransitive {
header "autolink-coff-c-pragma-transitive.h"
export *
}
20 changes: 20 additions & 0 deletions test/IRGen/autolink-coff-c-pragma.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Tests that a `#pragma comment(lib, ...)` in a C header imported as a module
// causes a corresponding `/DEFAULTLIB` directive to be emitted.
//
// We test that this is true also for C headers included transitively from
// another C header.

// 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
// 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

// REQUIRES: CODEGENERATOR=X86

import AutolinkCoffCPragma

// CHECK-MSVC-IR: !llvm.linker.options = !{!{{[0-9]+}}, !{{[0-9]+}}}
// CHECK-MSVC-IR-DAG: !{{[0-9]+}} = !{!"/DEFAULTLIB:module.lib"}
// CHECK-MSVC-IR-DAG: !{{[0-9]+}} = !{!"/DEFAULTLIB:transitive-module.lib"}

// CHECK-MSVC-ASM: .section .drectve
// CHECK-MSVC-ASM-DAG: .ascii " /DEFAULTLIB:module.lib"
// CHECK-MSVC-ASM-DAG: .ascii " /DEFAULTLIB:transitive-module.lib"

0 comments on commit 6bf0f26

Please sign in to comment.