[clang] [C++20] [Modules] Instantiate pending instantiations when GMF ends (PR #126842)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 11 20:48:50 PST 2025
https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/126842
>From a25871dd97fded43484f7b690f6ff58664be0b99 Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Wed, 12 Feb 2025 11:35:37 +0800
Subject: [PATCH] [C++20] [Modules] Instantiate pending instantiations when GMF
ends
Close https://github.com/llvm/llvm-project/issues/125999
The cause of the problem is, when we instantiate the pending
instantiation, the owning module of the TU gets into 'foo' instead
of the GMF.
The concern of the patch is, I am not sure the point of 'pending'
instantiations. I mean, if there is reason we must pending the
intantiations to the end of the TU.
---
clang/lib/Sema/Sema.cpp | 8 ++++++--
clang/test/Modules/pr125999.cppm | 33 ++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
create mode 100644 clang/test/Modules/pr125999.cppm
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 15c18f9a4525b..8a47c016348e4 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1104,9 +1104,13 @@ void Sema::ActOnStartOfTranslationUnit() {
}
void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) {
- // No explicit actions are required at the end of the global module fragment.
- if (Kind == TUFragmentKind::Global)
+ if (Kind == TUFragmentKind::Global) {
+ // Perform Pending Instantiations at the end of global module fragment so
+ // that the module ownership of TU-level decls won't get messed.
+ llvm::TimeTraceScope TimeScope("PerformPendingInstantiations");
+ PerformPendingInstantiations();
return;
+ }
// Transfer late parsed template instantiations over to the pending template
// instantiation list. During normal compilation, the late template parser
diff --git a/clang/test/Modules/pr125999.cppm b/clang/test/Modules/pr125999.cppm
new file mode 100644
index 0000000000000..a859ce7592604
--- /dev/null
+++ b/clang/test/Modules/pr125999.cppm
@@ -0,0 +1,33 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/foo.cppm -verify -fsyntax-only
+
+//--- bar.h
+template <typename T>
+struct Singleton {
+ static T* instance_;
+ static T* get() {
+ static bool init = false;
+ if (!init) {
+ init = true;
+ instance_ = ::new T();
+ }
+ return instance_;
+ }
+};
+
+template <typename T>
+T* Singleton<T>::instance_ = nullptr;
+
+struct s{};
+inline void* foo() {
+ return Singleton<s>::get();
+}
+
+//--- foo.cppm
+// expected-no-diagnostics
+module;
+#include "bar.h"
+export module foo;
More information about the cfe-commits
mailing list