[C++20] [Modules] Avoid use-but-not-defined error

See the attached test for example.
This commit is contained in:
Chuanqi Xu 2025-03-05 19:02:37 +08:00
parent a98707e285
commit ea15e8b16e
2 changed files with 39 additions and 0 deletions

View File

@ -330,6 +330,12 @@ namespace clang {
}
bool clang::CanElideDeclDef(const Decl *D) {
bool isExternalWithNoLinkageType = false;
if (auto *VD = dyn_cast<ValueDecl>(D))
if (VD->hasExternalFormalLinkage() &&
!isExternalFormalLinkage(VD->getType()->getLinkage()))
isExternalWithNoLinkageType = true;
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
if (FD->isInlined() || FD->isConstexpr())
return false;
@ -339,6 +345,9 @@ bool clang::CanElideDeclDef(const Decl *D) {
if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
return false;
if (isExternalWithNoLinkageType && !FD->isExternC())
return false;
}
if (auto *VD = dyn_cast<VarDecl>(D)) {
@ -352,6 +361,9 @@ bool clang::CanElideDeclDef(const Decl *D) {
if (VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
return false;
if (isExternalWithNoLinkageType && !VD->isExternC())
return false;
}
return true;

View File

@ -0,0 +1,27 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: cd %t
//
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -emit-reduced-module-interface -o %t/a.pcm
// RUN: %clang_cc1 -std=c++20 %t/use.cc -fmodule-file=a=%t/a.pcm -fsyntax-only -verify
//--- a.cppm
export module a;
namespace {
struct Local {};
}
export class A {
public:
void *external_but_not_type_external(Local *) {
return nullptr;
}
};
//--- use.cc
// expected-no-diagnostics
import a;
void *use() {
A a;
return a.external_but_not_type_external(nullptr);
}