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

Dmitriy Chestnykh via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 8 07:07:25 PDT 2024


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

>From 7a3f28574377bb6139971039ca6da5d299c2228d 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 1/2] [clang] Use internal linkage for c23 constexpr vars.

See C23 std 6.2.2p3.
Fix #97830
---
 clang/lib/AST/Decl.cpp                         | 11 +++++++++--
 .../CodeGen/constexpr-c23-internal-linkage.c   | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGen/constexpr-c23-internal-linkage.c

diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 16ed6d88d1cb1..490c4a2fc525c 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -612,19 +612,26 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
   assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
          "Not a name having namespace scope");
   ASTContext &Context = D->getASTContext();
+  const auto *Var = dyn_cast<VarDecl>(D);
 
   // C++ [basic.link]p3:
   //   A name having namespace scope (3.3.6) has internal linkage if it
   //   is the name of
 
-  if (getStorageClass(D->getCanonicalDecl()) == SC_Static) {
+  if ((getStorageClass(D->getCanonicalDecl()) == SC_Static) ||
+      (Context.getLangOpts().C23 && Var && Var->isConstexpr())) {
     // - a variable, variable template, function, or function template
     //   that is explicitly declared static; or
     // (This bullet corresponds to C99 6.2.2p3.)
+
+    // C23 6.2.2p3
+    // If the declaration of a file scope identifier for
+    // an object contains any of the storage-class specifiers static or
+    // constexpr then the identifier has internal linkage.
     return LinkageInfo::internal();
   }
 
-  if (const auto *Var = dyn_cast<VarDecl>(D)) {
+  if (Var) {
     // - a non-template variable of non-volatile const-qualified type, unless
     //   - it is explicitly declared extern, or
     //   - it is declared in the purview of a module interface unit
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{{.*}}
+*/
+

>From 317b3e2f2ff6be20b35da008a6ef243b2c84ec18 Mon Sep 17 00:00:00 2001
From: Dmitry Chestnykh <dm.chestnykh at gmail.com>
Date: Mon, 8 Jul 2024 17:05:02 +0300
Subject: [PATCH 2/2] [clang][C23] Add release notes to bugfix.

Add note that bug with incorrect linkage type
of C23 constexpr vars is fixed.
---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 36cf615a4287c..cb8bd5e6f3a04 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -762,6 +762,9 @@ Bug Fixes in This Version
 
 - Fixed `static_cast` to array of unknown bound. Fixes (#GH62863).
 
+- Fixed a bug with incorrect linkage type of C23 `constexpr` objects. Now the linkage of such
+  objects is internal in accordance with the standard (#GH97846).
+
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 



More information about the cfe-commits mailing list