diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ab29d2dbb566..36005430ae4c 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1243,9 +1243,15 @@ void CodeGenModule::EmitModuleLinkOptions() { SmallVector Stack; // Seed the stack with imported modules. - for (Module *M : ImportedModules) + for (Module *M : ImportedModules) { + // Do not add any link flags when an implementation TU of a module imports + // a header of that same module. + if (M->getTopLevelModuleName() == getLangOpts().CurrentModule && + !getLangOpts().isCompilingModule()) + continue; if (Visited.insert(M).second) Stack.push_back(M); + } // Find all of the modules to import, making a little effort to prune // non-leaf modules. diff --git a/clang/test/Modules/Inputs/module-impl-with-link/foo.h b/clang/test/Modules/Inputs/module-impl-with-link/foo.h new file mode 100644 index 000000000000..90fe1bcc5851 --- /dev/null +++ b/clang/test/Modules/Inputs/module-impl-with-link/foo.h @@ -0,0 +1 @@ +//empty diff --git a/clang/test/Modules/Inputs/module-impl-with-link/module.modulemap b/clang/test/Modules/Inputs/module-impl-with-link/module.modulemap new file mode 100644 index 000000000000..b85f8b6fe804 --- /dev/null +++ b/clang/test/Modules/Inputs/module-impl-with-link/module.modulemap @@ -0,0 +1,4 @@ +module Clib { + header "foo.h" + link "Clib" +} diff --git a/clang/test/Modules/module-impl-with-link.c b/clang/test/Modules/module-impl-with-link.c new file mode 100644 index 000000000000..5e5ca83aafd6 --- /dev/null +++ b/clang/test/Modules/module-impl-with-link.c @@ -0,0 +1,7 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -fmodule-name=Clib %s -I %S/Inputs/module-impl-with-link -emit-llvm -o - +#include "foo.h" +// CHECK: !{{[0-9]+}} = !{i32 6, !"Linker Options", ![[LINK_OPTIONS:[0-9]+]]} +// Make sure we don't generate linker option for module Clib since this TU is +// an implementation of Clib. +// CHECK: ![[LINK_OPTIONS]] = !{}