Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions clang/test/CodeGen/constexpr-c23-internal-linkage.c
Original file line number Diff line number Diff line change
@@ -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{{.*}}
*/

Loading