https://github.com/chestnykh updated 
https://github.com/llvm/llvm-project/pull/97846

>From ecf8360b1ca201eeb8887214477eb8ffeb5f088f Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestn...@gmail.com>
Date: Fri, 5 Jul 2024 20:40:32 +0300
Subject: [PATCH] [clang] Use internal linkage for c23 constexpr vars.

Set `static` storage class specifier for such decls
to have `internal` linkage in produced IR and then
in the object file.
Fix #97830
---
 clang/lib/Parse/ParseDecl.cpp                  | 10 +++++++---
 .../CodeGen/constexpr-c23-internal-linkage.c   | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGen/constexpr-c23-internal-linkage.c

diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index a07f7ad2233fe6..8222e0c96e3f1b 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4428,10 +4428,14 @@ void Parser::ParseDeclarationSpecifiers(
 
     // constexpr, consteval, constinit specifiers
     case tok::kw_constexpr:
-      if (getLangOpts().C23)
+      if (getLangOpts().C23) {
         Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName();
-      isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
-                                      PrevSpec, DiagID);
+        isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
+                                        PrevSpec, DiagID);
+
+        isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
+                                           PrevSpec, DiagID, Policy);
+      }
       break;
     case tok::kw_consteval:
       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Consteval, Loc,
diff --git a/clang/test/CodeGen/constexpr-c23-internal-linkage.c 
b/clang/test/CodeGen/constexpr-c23-internal-linkage.c
new file mode 100644
index 00000000000000..1236062272a2d5
--- /dev/null
+++ b/clang/test/CodeGen/constexpr-c23-internal-linkage.c
@@ -0,0 +1,18 @@
+/*
+ * RUN: %clang_cc1 -std=c23 -emit-llvm -o - %s | FileCheck %s
+ */
+
+constexpr int var_int = 1;
+constexpr char var_char = 'a';
+constexpr float var_float = 2.5;
+
+const int *p_i = &var_int;
+const char *p_c = &var_char;
+const float *p_f = &var_float;
+
+/*
+CHECK: @var_int = internal constant i32 1{{.*}}
+CHECK: @var_char = internal constant i8 97{{.*}}
+CHECK: @var_float = internal constant float 2.5{{.*}}
+*/
+

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to