https://github.com/akash-manna-sky created 
https://github.com/llvm/llvm-project/pull/216348

Fixes #196982

When filling in source locations for a dependent `address_space` type, we only 
looked at the declarator chunk we happened to be visiting. But an attribute 
written after the declarator-id appertains to the declared entity, so it never 
lands on a chunk — it gets applied to the outermost type instead. The search 
came up empty and we hit an `llvm_unreachable`. Nothing to do with the 
malformed code in the bug report, by the way: plain `template <int AS> void f() 
{ void *p [[clang::address_space(AS)]]; }` crashes too.

So we now check the declarator's own attributes as well, and if there's still 
no match, fall back to the location stored on the type rather than asserting. I 
kept the attribute search first because the type is uniqued without its 
location in the profile, so two declarators with the same operand share a node 
and the type would give us the wrong one. Leading-position attributes aren't 
searched — `address_space` always slides to the decl-spec, so they can't 
produce this type. Tests in 
`clang/test/SemaTemplate/address_space-dependent.cpp`.

>From 38a456e345f0015e6ab8e07e34b586037c0e526f Mon Sep 17 00:00:00 2001
From: Akash Manna <[email protected]>
Date: Fri, 14 Aug 2026 21:44:45 +0530
Subject: [PATCH] [clang][Sema] Fix crash on address_space attribute written
 after the declarator-id

---
 clang/docs/ReleaseNotes.md                    |  4 ++++
 clang/lib/Sema/SemaType.cpp                   | 24 ++++++++++++-------
 .../SemaTemplate/address_space-dependent.cpp  | 14 +++++++++++
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index d9b9c92950c98..e08a059be1a47 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -410,6 +410,10 @@ features cannot lower the translation-unit ABI level;
   `sized_by_or_null` describe the size in bytes rather than a count of 
elements,
   they are now correctly accepted on such pointers.
 
+- Fixed a crash when an `address_space` attribute with a dependent argument was
+  written after the declarator-id, where it appertains to the declared entity
+  rather than to a declarator chunk. (#GH196982)
+
 #### Bug Fixes to C++ Support
 
 - Fixed an issue where `__typeof__` incorrectly rejected cv-qualified function 
types.
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index c19022ac1aee8..e9286aaa9de79 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6344,11 +6344,20 @@ namespace {
   };
 } // end anonymous namespace
 
-static void
-fillDependentAddressSpaceTypeLoc(DependentAddressSpaceTypeLoc DASTL,
-                                 const ParsedAttributesView &Attrs) {
-  for (const ParsedAttr &AL : Attrs) {
-    if (AL.getKind() == ParsedAttr::AT_AddressSpace) {
+static void fillDependentAddressSpaceTypeLoc(ASTContext &Context,
+                                             DependentAddressSpaceTypeLoc 
DASTL,
+                                             const Declarator &D,
+                                             const DeclaratorChunk &Chunk) {
+  // An attribute written after the declarator-id appertains to the declared
+  // entity, so it is applied to the outermost type instead of to the chunk
+  // that is being visited.
+  const ParsedAttributesView *AttrLists[] = {&Chunk.getAttrs(),
+                                             &D.getAttributes()};
+  for (const ParsedAttributesView *Attrs : AttrLists) {
+    for (const ParsedAttr &AL : *Attrs) {
+      if (AL.getKind() != ParsedAttr::AT_AddressSpace || AL.getNumArgs() != 1 
||
+          !AL.isArgExpr(0))
+        continue;
       DASTL.setAttrNameLoc(AL.getLoc());
       DASTL.setAttrExprOperand(AL.getArgAsExpr(0));
       DASTL.setAttrOperandParensRange(SourceRange());
@@ -6356,8 +6365,7 @@ 
fillDependentAddressSpaceTypeLoc(DependentAddressSpaceTypeLoc DASTL,
     }
   }
 
-  llvm_unreachable(
-      "no address_space attribute found at the expected location!");
+  DASTL.initializeLocal(Context, DASTL.getTypePtr()->getAttributeLoc());
 }
 
 /// Create and instantiate a TypeSourceInfo with type source information.
@@ -6423,7 +6431,7 @@ GetTypeSourceInfoForDeclarator(TypeProcessingState &State,
 
       case TypeLoc::DependentAddressSpace: {
         auto TL = CurrTL.castAs<DependentAddressSpaceTypeLoc>();
-        fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
+        fillDependentAddressSpaceTypeLoc(S.Context, TL, D, D.getTypeObject(i));
         CurrTL = TL.getPointeeTypeLoc().getUnqualifiedLoc();
         break;
       }
diff --git a/clang/test/SemaTemplate/address_space-dependent.cpp 
b/clang/test/SemaTemplate/address_space-dependent.cpp
index d6f25923b69b5..9c90c9831b7e3 100644
--- a/clang/test/SemaTemplate/address_space-dependent.cpp
+++ b/clang/test/SemaTemplate/address_space-dependent.cpp
@@ -130,3 +130,17 @@ struct EntryTy {
 ASPtrTy<1> x;
 EntryTy<2> y;
 }
+
+namespace gh196982 {
+template <int AS>
+void trailing() {
+  void *p [[clang::address_space(AS)]]; // expected-warning {{applying 
attribute 'clang::address_space' to a declaration is deprecated; apply it to 
the type instead}}
+  void *q __attribute__((address_space(AS)));
+  int r[2] __attribute__((address_space(AS)));
+}
+
+void invalidOperand() {
+  void *p [[clang::address_space(undeclared())]]; // expected-error {{use of 
undeclared identifier 'undeclared'}} \
+                                                 // expected-warning 
{{applying attribute 'clang::address_space' to a declaration is deprecated; 
apply it to the type instead}}
+}
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to