[clang] [clang][ExprConstant] Fix error on static constexpr symbol in dllimport function (PR #171628)
Alexandre Ganea via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 18 07:17:02 PST 2025
https://github.com/aganea updated https://github.com/llvm/llvm-project/pull/171628
>From 29817263e2e11c468089d742fd39179e78d4ca1a Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <aganea at havenstudios.com>
Date: Tue, 9 Dec 2025 19:36:08 -0500
Subject: [PATCH 1/3] [SemaCXX] Fix static constexpr in dllimport function
---
clang/lib/AST/ExprConstant.cpp | 7 +++++--
clang/test/SemaCXX/dllimport.cpp | 22 ++++++++++++++++++++++
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d81496ffd74e0..e5ca6912ed566 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2407,8 +2407,11 @@ static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
return false;
// A dllimport variable never acts like a constant, unless we're
- // evaluating a value for use only in name mangling.
- if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>())
+ // evaluating a value for use only in name mangling, and unless we're a
+ // static local. For the latter case, we'd still need to evaluate the
+ // constant expression in case we're inside a (inlined) function.
+ if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
+ !Var->isStaticLocal())
// FIXME: Diagnostic!
return false;
diff --git a/clang/test/SemaCXX/dllimport.cpp b/clang/test/SemaCXX/dllimport.cpp
index b7a1a62b8725b..dafb12fab6888 100644
--- a/clang/test/SemaCXX/dllimport.cpp
+++ b/clang/test/SemaCXX/dllimport.cpp
@@ -1526,6 +1526,28 @@ template <typename T> struct __declspec(dllimport) PartiallySpecializedClassTemp
template <typename T> struct ExpliciallySpecializedClassTemplate {};
template <> struct __declspec(dllimport) ExpliciallySpecializedClassTemplate<int> { void f() {} };
+// Function-local static constexpr in dllimport function (or class).
+struct DLLImportFuncWithConstexprStatic {
+ __declspec(dllimport) static const int *func() {
+ // expected-warning at -1{{'dllimport' attribute ignored on inline function}}
+ static constexpr int value = 42;
+ static constexpr const int *p = &value;
+ static_assert(*p == 42, "");
+ return p;
+ }
+ __declspec(dllimport) __forceinline static const int *funcForceInline() {
+ // expected-warning at -1{{'dllimport' attribute ignored on inline function}}
+ static constexpr int value = 42;
+ static constexpr const int *p = &value;
+ static_assert(*p == 42, "");
+ return p;
+ }
+};
+const int* (*pFunc)() = &DLLImportFuncWithConstexprStatic::func;
+const int* (*pFuncForceInline)() = &DLLImportFuncWithConstexprStatic::funcForceInline;
+bool UsedDLLImportFuncWithConstexprStatic() {
+ return pFunc() == DLLImportFuncWithConstexprStatic::func() && pFuncForceInline() == DLLImportFuncWithConstexprStatic::funcForceInline();
+}
//===----------------------------------------------------------------------===//
// Classes with template base classes
>From 17a28e40e670961fefc303603602687667b3a53e Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <aganea at havenstudios.com>
Date: Wed, 10 Dec 2025 09:30:05 -0500
Subject: [PATCH 2/3] Fix tests
---
clang/test/SemaCXX/dllimport.cpp | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/clang/test/SemaCXX/dllimport.cpp b/clang/test/SemaCXX/dllimport.cpp
index dafb12fab6888..eaf217c8e2d3b 100644
--- a/clang/test/SemaCXX/dllimport.cpp
+++ b/clang/test/SemaCXX/dllimport.cpp
@@ -1528,26 +1528,38 @@ template <> struct __declspec(dllimport) ExpliciallySpecializedClassTemplate<int
// Function-local static constexpr in dllimport function (or class).
struct DLLImportFuncWithConstexprStatic {
+#if defined(GNU)
+// expected-warning at +2{{'dllimport' attribute ignored on inline function}}
+#endif
__declspec(dllimport) static const int *func() {
- // expected-warning at -1{{'dllimport' attribute ignored on inline function}}
static constexpr int value = 42;
static constexpr const int *p = &value;
static_assert(*p == 42, "");
return p;
}
- __declspec(dllimport) __forceinline static const int *funcForceInline() {
- // expected-warning at -1{{'dllimport' attribute ignored on inline function}}
+};
+const int* (*pFunc)() = &DLLImportFuncWithConstexprStatic::func;
+bool UsedDLLImportFuncWithConstexprStatic() {
+ return pFunc() == DLLImportFuncWithConstexprStatic::func();
+}
+
+#if !defined(PS)
+struct DLLImportInlineFuncWithConstexprStatic {
+#if defined(GNU)
+ // expected-warning at +2{{'dllimport' attribute ignored on inline function}}
+#endif
+ __declspec(dllimport) __forceinline static const int* funcForceInline() {
static constexpr int value = 42;
- static constexpr const int *p = &value;
+ static constexpr const int* p = &value;
static_assert(*p == 42, "");
return p;
}
};
-const int* (*pFunc)() = &DLLImportFuncWithConstexprStatic::func;
-const int* (*pFuncForceInline)() = &DLLImportFuncWithConstexprStatic::funcForceInline;
-bool UsedDLLImportFuncWithConstexprStatic() {
- return pFunc() == DLLImportFuncWithConstexprStatic::func() && pFuncForceInline() == DLLImportFuncWithConstexprStatic::funcForceInline();
+const int* (*pFuncForceInline)() = &DLLImportInlineFuncWithConstexprStatic::funcForceInline;
+bool UsedDLLImportInlineFuncWithConstexprStatic() {
+ return pFuncForceInline() == DLLImportInlineFuncWithConstexprStatic::funcForceInline();
}
+#endif // !PS
//===----------------------------------------------------------------------===//
// Classes with template base classes
>From 20fbbaff1166f0e58fd8d6d4938848543a2adb96 Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <alex_toresh at yahoo.fr>
Date: Thu, 18 Dec 2025 10:16:34 -0500
Subject: [PATCH 3/3] Change wording and remove FIXME
---
clang/lib/AST/ExprConstant.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5ca6912ed566..1ba5a347a25a5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2407,12 +2407,11 @@ static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
return false;
// A dllimport variable never acts like a constant, unless we're
- // evaluating a value for use only in name mangling, and unless we're a
+ // evaluating a value for use only in name mangling, and unless it's a
// static local. For the latter case, we'd still need to evaluate the
// constant expression in case we're inside a (inlined) function.
if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
!Var->isStaticLocal())
- // FIXME: Diagnostic!
return false;
// In CUDA/HIP device compilation, only device side variables have
More information about the cfe-commits
mailing list