Skip to content

Emit /DEFAULTLIB directive for #pragma comment(lib, ...) in a C module #31913

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

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"