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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 10:53:02 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Dmitriy Chestnykh (chestnykh)

<details>
<summary>Changes</summary>

Set `static` storage class specifier for such decls to have `internal` linkage in produced IR and then in the object file.
Fix #<!-- -->97830

---
Full diff: https://github.com/llvm/llvm-project/pull/97846.diff


2 Files Affected:

- (modified) clang/lib/Parse/ParseDecl.cpp (+8-3) 
- (added) clang/test/CodeGen/constexpr-c23-internal-linkage.c (+17) 


``````````diff
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..a1df206d520d0
--- /dev/null
+++ b/clang/test/CodeGen/constexpr-c23-internal-linkage.c
@@ -0,0 +1,17 @@
+/* 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{{.*}}
+*/
+

``````````

</details>


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


More information about the cfe-commits mailing list