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

>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 1/2] [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}}
+}
+}

>From f07922d02aaff9904cf1cc6f4af9c7b08210e7a9 Mon Sep 17 00:00:00 2001
From: Akash Manna <[email protected]>
Date: Tue, 18 Aug 2026 22:54:52 +0530
Subject: [PATCH 2/2] [Sema] Refactor fillDependentAddressSpaceTypeLoc to
 streamline attribute handling

---
 clang/lib/Sema/SemaType.cpp | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index e9286aaa9de79..29e4c41d820f3 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -6344,19 +6344,21 @@ namespace {
   };
 } // end anonymous namespace
 
-static void fillDependentAddressSpaceTypeLoc(ASTContext &Context,
-                                             DependentAddressSpaceTypeLoc 
DASTL,
-                                             const Declarator &D,
-                                             const DeclaratorChunk &Chunk) {
+static void
+fillDependentAddressSpaceTypeLoc(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()};
+  // entity and is applied to the outermost type rather than to a chunk, so
+  // every attribute list of the declarator has to be searched.
+  const ParsedAttributesView *AttrLists[] = {
+      &Chunk.getAttrs(), &D.getAttributes(), &D.getDeclSpec().getAttributes(),
+      &D.getDeclarationAttributes()};
   for (const ParsedAttributesView *Attrs : AttrLists) {
     for (const ParsedAttr &AL : *Attrs) {
-      if (AL.getKind() != ParsedAttr::AT_AddressSpace || AL.getNumArgs() != 1 
||
-          !AL.isArgExpr(0))
+      // Invalid or malformed attributes never produce a type.
+      if (AL.getKind() != ParsedAttr::AT_AddressSpace || AL.isInvalid() ||
+          AL.getNumArgs() != 1 || !AL.isArgExpr(0))
         continue;
       DASTL.setAttrNameLoc(AL.getLoc());
       DASTL.setAttrExprOperand(AL.getArgAsExpr(0));
@@ -6365,7 +6367,8 @@ static void fillDependentAddressSpaceTypeLoc(ASTContext 
&Context,
     }
   }
 
-  DASTL.initializeLocal(Context, DASTL.getTypePtr()->getAttributeLoc());
+  llvm_unreachable(
+      "no address_space attribute found at the expected location!");
 }
 
 /// Create and instantiate a TypeSourceInfo with type source information.
@@ -6431,7 +6434,7 @@ GetTypeSourceInfoForDeclarator(TypeProcessingState &State,
 
       case TypeLoc::DependentAddressSpace: {
         auto TL = CurrTL.castAs<DependentAddressSpaceTypeLoc>();
-        fillDependentAddressSpaceTypeLoc(S.Context, TL, D, D.getTypeObject(i));
+        fillDependentAddressSpaceTypeLoc(TL, D, D.getTypeObject(i));
         CurrTL = TL.getPointeeTypeLoc().getUnqualifiedLoc();
         break;
       }

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

Reply via email to