[clang] [Sema] Fix assertion crash on section conflict with a non-identifier decl (PR #200873)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 2 02:03:23 PDT 2026
https://github.com/lijinpei-amd updated https://github.com/llvm/llvm-project/pull/200873
>From 38f79eb3acd6e16628459517ae285d177fed46f3 Mon Sep 17 00:00:00 2001
From: Li Jinpei <jinpli at amd.com>
Date: Tue, 2 Jun 2026 00:55:37 +0800
Subject: [PATCH 1/3] [Sema] Fix assertion crash on section conflict with a
non-identifier decl
NamedDecl::getName() asserts the name is a simple identifier, so
UnifySection crashed when the conflicting section decl had a special
name such as a lambda's call operator.
Drop the argument: `note_declared_at` has no format placeholder,
so it was dead code. The error already prints the section decl.
Fixes https://github.com/llvm/llvm-project/issues/192264
---
clang/lib/Sema/SemaAttr.cpp | 6 ++----
clang/test/SemaCXX/attr-section.cpp | 15 +++++++++++++++
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index ffd546138008a..67573c9f1c72a 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -853,8 +853,7 @@ bool Sema::UnifySection(StringRef SectionName, int SectionFlags,
return false;
Diag(Decl->getLocation(), diag::err_section_conflict) << Decl << Section;
if (Section.Decl)
- Diag(Section.Decl->getLocation(), diag::note_declared_at)
- << Section.Decl->getName();
+ Diag(Section.Decl->getLocation(), diag::note_declared_at);
if (PragmaLocation.isValid())
Diag(PragmaLocation, diag::note_pragma_entered_here);
if (Section.PragmaSectionLocation.isValid())
@@ -874,8 +873,7 @@ bool Sema::UnifySection(StringRef SectionName,
Diag(PragmaSectionLocation, diag::err_section_conflict)
<< "this" << Section;
if (Section.Decl)
- Diag(Section.Decl->getLocation(), diag::note_declared_at)
- << Section.Decl->getName();
+ Diag(Section.Decl->getLocation(), diag::note_declared_at);
if (Section.PragmaSectionLocation.isValid())
Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
return true;
diff --git a/clang/test/SemaCXX/attr-section.cpp b/clang/test/SemaCXX/attr-section.cpp
index 1c07e3dd8bba2..35d38ec6e2be0 100644
--- a/clang/test/SemaCXX/attr-section.cpp
+++ b/clang/test/SemaCXX/attr-section.cpp
@@ -69,3 +69,18 @@ __attribute__((section("non_trivial_ctor"))) const t1 v1; // expected-note {{dec
extern const t1 v2;
__attribute__((section("non_trivial_ctor"))) const t1 v2{3}; // expected-error {{'v2' causes a section type conflict with 'v1'}}
} // namespace non_trivial_ctor
+
+// Check that a section conflict with a decl whose name is not a simple
+// identifier (here, a lambda's call operator) is diagnosed without crashing.
+namespace lambda_call_operator {
+auto lambda = [](int val) __attribute__((section("lambda_op"))) { return val; }; // expected-note {{declared here}}
+__attribute__((section("lambda_op"))) int i{}; // expected-error {{'i' causes a section type conflict with 'operator()'}}
+} // namespace lambda_call_operator
+
+// Check that a section conflict with a decl whose name is not a simple
+// identifier (here, a lambda's call operator) is diagnosed without crashing.
+namespace lambda_call_operator_pragma {
+auto lambda = [](int val) __attribute__((section("lambda_op_pragma"))) { return val; }; // expected-note {{declared here}}
+#pragma clang section bss="lambda_op_pragma" // expected-error {{this causes a section type conflict with 'operator()'}}
+#pragma clang section bss=""
+} // namespace lambda_call_operator_pragma
>From c6a548b2a66b3d37ab8a2667451176f4718411a4 Mon Sep 17 00:00:00 2001
From: lijinpei-amd <jinpei.li at amd.com>
Date: Tue, 2 Jun 2026 16:58:04 +0800
Subject: [PATCH 2/3] Update clang/test/SemaCXX/attr-section.cpp
Co-authored-by: Corentin Jabot <corentinjabot at gmail.com>
---
clang/test/SemaCXX/attr-section.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/attr-section.cpp b/clang/test/SemaCXX/attr-section.cpp
index 35d38ec6e2be0..7252ef7694b27 100644
--- a/clang/test/SemaCXX/attr-section.cpp
+++ b/clang/test/SemaCXX/attr-section.cpp
@@ -72,7 +72,7 @@ __attribute__((section("non_trivial_ctor"))) const t1 v2{3}; // expected-error {
// Check that a section conflict with a decl whose name is not a simple
// identifier (here, a lambda's call operator) is diagnosed without crashing.
-namespace lambda_call_operator {
+namespace GH192264 {
auto lambda = [](int val) __attribute__((section("lambda_op"))) { return val; }; // expected-note {{declared here}}
__attribute__((section("lambda_op"))) int i{}; // expected-error {{'i' causes a section type conflict with 'operator()'}}
} // namespace lambda_call_operator
>From efd9e1a237e5fc5375ff649661721da1738ed863 Mon Sep 17 00:00:00 2001
From: Li Jinpei <jinpli at amd.com>
Date: Tue, 2 Jun 2026 17:03:02 +0800
Subject: [PATCH 3/3] [Sema] Add release note for section-conflict crash fix
Address review: add a ReleaseNotes.rst entry referencing GH192264, and
fix a stale closing-namespace comment left by the GH192264 rename.
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/test/SemaCXX/attr-section.cpp | 2 +-
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d1c58435ab399..c279e00758134 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -666,6 +666,9 @@ Bug Fixes to Attribute Support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed a behavioral discrepancy between deleted functions and private members when checking the ``enable_if`` attribute. (#GH175895)
- Fixed ``init_priority`` attribute by delaying type checks until after the type is deduced.
+- Fixed a crash when a ``section`` attribute or ``#pragma clang section`` caused a
+ section type conflict with a declaration whose name is not a simple identifier,
+ such as a lambda's call operator. (#GH192264)
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/attr-section.cpp b/clang/test/SemaCXX/attr-section.cpp
index 7252ef7694b27..95d4fc53ebe6c 100644
--- a/clang/test/SemaCXX/attr-section.cpp
+++ b/clang/test/SemaCXX/attr-section.cpp
@@ -75,7 +75,7 @@ __attribute__((section("non_trivial_ctor"))) const t1 v2{3}; // expected-error {
namespace GH192264 {
auto lambda = [](int val) __attribute__((section("lambda_op"))) { return val; }; // expected-note {{declared here}}
__attribute__((section("lambda_op"))) int i{}; // expected-error {{'i' causes a section type conflict with 'operator()'}}
-} // namespace lambda_call_operator
+} // namespace GH192264
// Check that a section conflict with a decl whose name is not a simple
// identifier (here, a lambda's call operator) is diagnosed without crashing.
More information about the cfe-commits
mailing list