llunak updated this revision to Diff 238086. llunak added a comment. I've updated the test as requested.
However I've noticed that a PCH-based test for this relies on https://reviews.llvm.org/D69585 . The fix works (of course), but it requires a PCH/module with the instantiation. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D69779/new/ https://reviews.llvm.org/D69779 Files: clang/lib/Serialization/ASTWriterDecl.cpp clang/test/PCH/codegen-extern-template.cpp clang/test/PCH/codegen-extern-template.h Index: clang/test/PCH/codegen-extern-template.h =================================================================== --- /dev/null +++ clang/test/PCH/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 <typename T> +inline T foo() { return 10; } + +extern template int foo<int>(); + +inline int bar() { return foo<int>(); } + +#endif Index: clang/test/PCH/codegen-extern-template.cpp =================================================================== --- /dev/null +++ clang/test/PCH/codegen-extern-template.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple=x86_64-linux-gnu -x c++-header -building-pch-with-obj -pch-instantiate-templates -fmodules-codegen -emit-pch %S/codegen-extern-template.h -o %t.pch +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -include-pch %t.pch | FileCheck %s +// expected-no-diagnostics + +#include "codegen-extern-template.h" + +template int foo<int>(); + +// CHECK: define weak_odr i32 @_Z3fooIiET_v Index: clang/lib/Serialization/ASTWriterDecl.cpp =================================================================== --- clang/lib/Serialization/ASTWriterDecl.cpp +++ clang/lib/Serialization/ASTWriterDecl.cpp @@ -2412,11 +2412,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<AlwaysInlineAttr>()) { if (!Linkage) Linkage = Writer->Context->GetGVALinkageForFunction(FD); - ModulesCodegen = *Linkage != GVA_Internal; + ModulesCodegen = + *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally; } } }
Index: clang/test/PCH/codegen-extern-template.h =================================================================== --- /dev/null +++ clang/test/PCH/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 <typename T> +inline T foo() { return 10; } + +extern template int foo<int>(); + +inline int bar() { return foo<int>(); } + +#endif Index: clang/test/PCH/codegen-extern-template.cpp =================================================================== --- /dev/null +++ clang/test/PCH/codegen-extern-template.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple=x86_64-linux-gnu -x c++-header -building-pch-with-obj -pch-instantiate-templates -fmodules-codegen -emit-pch %S/codegen-extern-template.h -o %t.pch +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -include-pch %t.pch | FileCheck %s +// expected-no-diagnostics + +#include "codegen-extern-template.h" + +template int foo<int>(); + +// CHECK: define weak_odr i32 @_Z3fooIiET_v Index: clang/lib/Serialization/ASTWriterDecl.cpp =================================================================== --- clang/lib/Serialization/ASTWriterDecl.cpp +++ clang/lib/Serialization/ASTWriterDecl.cpp @@ -2412,11 +2412,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<AlwaysInlineAttr>()) { if (!Linkage) Linkage = Writer->Context->GetGVALinkageForFunction(FD); - ModulesCodegen = *Linkage != GVA_Internal; + ModulesCodegen = + *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally; } } }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits