[clang] [clang] Use internal linkage for c23 constexpr vars. (PR #97846)

Dmitriy Chestnykh via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 11:04:06 PDT 2024


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

>From 20adb1c05dc59cc0a4b3969824f75c2d9c612755 Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at 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                  | 11 ++++++++---
 .../CodeGen/constexpr-c23-internal-linkage.c   | 18 ++++++++++++++++++
 2 files changed, 26 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 a07f7ad2233fe..a4e66bb78d6df 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4428,10 +4428,15 @@ 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);
+        isStorageClass = true;
+      }
       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 0000000000000..1236062272a2d
--- /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{{.*}}
+*/
+



More information about the cfe-commits mailing list