[clang] [clang][sema]`no address_space attribute found at the expected location!` assertion fail (PR #216348)

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 14 11:56:18 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Akash Manna (akash-manna-sky)

<details>
<summary>Changes</summary>

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`.

---
Full diff: https://github.com/llvm/llvm-project/pull/216348.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+4) 
- (modified) clang/lib/Sema/SemaType.cpp (+16-8) 
- (modified) clang/test/SemaTemplate/address_space-dependent.cpp (+14) 


``````````diff
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}}
+}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/216348


More information about the cfe-commits mailing list