[llvm-branch-commits] [clang] release/20.x: [C++20] [Modules] handling selectAny attribute for vardecl (PR #128114)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Feb 20 18:55:11 PST 2025


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/128114

Backport 24c06a19be7bcf28b37e5eabbe65df95a2c0265a

Requested by: @ChuanqiXu9

>From 9aac12a38047a505bfa969b89f6cda17bf1cdfdf Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
Date: Fri, 21 Feb 2025 10:34:14 +0800
Subject: [PATCH] [C++20] [Modules] handling selectAny attribute for vardecl

Close https://github.com/llvm/llvm-project/issues/127963

The root cause of the problem seems to be that we didn't realize it
simply.

(cherry picked from commit 24c06a19be7bcf28b37e5eabbe65df95a2c0265a)
---
 clang/lib/Sema/SemaDecl.cpp      |  3 ++-
 clang/test/Modules/pr127943.cppm | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Modules/pr127943.cppm

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1ecb9aff5f319..01f09aba8c2ad 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4803,7 +4803,8 @@ bool Sema::checkVarDeclRedefinition(VarDecl *Old, VarDecl *New) {
       (New->getFormalLinkage() == Linkage::Internal || New->isInline() ||
        isa<VarTemplateSpecializationDecl>(New) ||
        New->getDescribedVarTemplate() || New->getNumTemplateParameterLists() ||
-       New->getDeclContext()->isDependentContext())) {
+       New->getDeclContext()->isDependentContext() ||
+       New->hasAttr<SelectAnyAttr>())) {
     // The previous definition is hidden, and multiple definitions are
     // permitted (in separate TUs). Demote this to a declaration.
     New->demoteThisDefinitionToDeclaration();
diff --git a/clang/test/Modules/pr127943.cppm b/clang/test/Modules/pr127943.cppm
new file mode 100644
index 0000000000000..7cc3be6903e6a
--- /dev/null
+++ b/clang/test/Modules/pr127943.cppm
@@ -0,0 +1,31 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/repro.cppm -fdeclspec -emit-module-interface -o %t/repro.pcm
+// RUN: %clang_cc1 -std=c++20 %t/source.cpp -fdeclspec -fsyntax-only -verify -fprebuilt-module-path=%t
+
+//--- repro_decl.hpp
+#pragma once
+
+extern "C"
+{
+    __declspec(selectany) int foo = 0;
+}
+
+//--- repro.cppm
+module;
+#include "repro_decl.hpp"
+
+export module repro;
+
+export inline int func()
+{
+    return foo;
+}
+
+//--- source.cpp
+// expected-no-diagnostics
+import repro;
+
+#include "repro_decl.hpp"



More information about the llvm-branch-commits mailing list