diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -2437,11 +2437,12 @@ } if (Writer->Context->getLangOpts().ModulesCodegen) { // Under -fmodules-codegen, codegen is performed for all non-internal, - // non-always_inline functions. + // non-always_inline functions, unless they are available elsewhere. if (!FD->hasAttr()) { if (!Linkage) Linkage = Writer->Context->GetGVALinkageForFunction(FD); - ModulesCodegen = *Linkage != GVA_Internal; + ModulesCodegen = + *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally; } } } diff --git a/clang/test/Modules/codegen-extern-template.h b/clang/test/Modules/codegen-extern-template.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/codegen-extern-template.h @@ -0,0 +1,12 @@ +// header for codegen-extern-template.cpp +#ifndef CODEGEN_EXTERN_TEMPLATE_H +#define CODEGEN_EXTERN_TEMPLATE_H + +template +inline T foo() { return 10; } + +extern template int foo(); + +inline int bar() { return foo(); } + +#endif diff --git a/clang/test/Modules/codegen-extern-template.cpp b/clang/test/Modules/codegen-extern-template.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/codegen-extern-template.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple=x86_64-linux-gnu -fmodules -fmodules-codegen -emit-module -fmodule-name=foo %S/codegen-extern-template.modulemap -x c++ -o %t.pcm +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fmodule-file=%t.pcm %s -emit-llvm -o - | FileCheck %s +// expected-no-diagnostics + +#include "codegen-extern-template.h" + +template int foo(); + +// CHECK: define weak_odr i32 @_Z3fooIiET_v diff --git a/clang/test/Modules/codegen-extern-template.modulemap b/clang/test/Modules/codegen-extern-template.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/codegen-extern-template.modulemap @@ -0,0 +1 @@ +module foo { header "codegen-extern-template.h" }