[clang] [ExtractAPI] Include availability attributes for anonymous typedefs (PR #209187)

Prajwal Nadig via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 13 07:08:05 PDT 2026


https://github.com/snprajwal created https://github.com/llvm/llvm-project/pull/209187

When an anonymous type is defined and has availability attributes, the attributes are attached to the typedef rather than the type being defined. This patch updates the anonymous type handling logic to merge the availability from the typedef in order to correctly include it in the symbol graph.

rdar://125497949

>From 3722586a02ea7b834968e4c7b52fd49571d4fba7 Mon Sep 17 00:00:00 2001
From: Prajwal Nadig <pnadig at apple.com>
Date: Mon, 13 Jul 2026 15:04:28 +0100
Subject: [PATCH] [ExtractAPI] Include availability attributes for anonymous
 typedefs

When an anonymous type is defined and has availability attributes, the
attributes are attached to the typedef rather than the type being
defined. This patch updates the anonymous type handling logic to merge
the availability from the typedef in order to correctly include it in
the symbol graph.

rdar://125497949
---
 .../clang/ExtractAPI/ExtractAPIVisitor.h      | 16 ++++++++-----
 .../availability_typedef_anonymous_record.c   | 24 +++++++++++++++++++
 2 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 clang/test/ExtractAPI/availability_typedef_anonymous_record.c

diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 38f57eeeddc3a..f716231645723 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -602,16 +602,20 @@ bool ExtractAPIVisitorBase<Derived>::VisitRecordDecl(const RecordDecl *Decl) {
   DeclarationFragments SubHeading =
       DeclarationFragmentsBuilder::getSubHeading(Decl);
 
+  AvailabilityInfo Availability = AvailabilityInfo::createFromDecl(Decl);
+  if (const auto *TypedefDecl = Decl->getTypedefNameForAnonDecl())
+    Availability.mergeWith(AvailabilityInfo::createFromDecl(TypedefDecl));
+
   if (Decl->isUnion())
     API.createRecord<UnionRecord>(
-        USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
-        AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
-        SubHeading, isInSystemHeader(Decl), isEmbeddedInVarDeclarator(*Decl));
+        USR, Name, createHierarchyInformationForDecl(*Decl), Loc, Availability,
+        Comment, Declaration, SubHeading, isInSystemHeader(Decl),
+        isEmbeddedInVarDeclarator(*Decl));
   else
     API.createRecord<StructRecord>(
-        USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
-        AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
-        SubHeading, isInSystemHeader(Decl), isEmbeddedInVarDeclarator(*Decl));
+        USR, Name, createHierarchyInformationForDecl(*Decl), Loc, Availability,
+        Comment, Declaration, SubHeading, isInSystemHeader(Decl),
+        isEmbeddedInVarDeclarator(*Decl));
 
   return true;
 }
diff --git a/clang/test/ExtractAPI/availability_typedef_anonymous_record.c b/clang/test/ExtractAPI/availability_typedef_anonymous_record.c
new file mode 100644
index 0000000000000..b1eba9d9eb534
--- /dev/null
+++ b/clang/test/ExtractAPI/availability_typedef_anonymous_record.c
@@ -0,0 +1,24 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing -triple arm64-apple-macosx \
+// RUN:   -x c-header %s -o %t/output.symbols.json -verify
+
+// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix STRUCT
+
+typedef struct {
+  int x;
+} MyStruct __attribute__((availability(macos, introduced=12.0)));
+
+// STRUCT-LABEL: "!testLabel": "c:@SA at MyStruct"
+// STRUCT:      "availability": [
+// STRUCT-NEXT:   {
+// STRUCT-NEXT:     "domain": "macos",
+// STRUCT-NEXT:     "introduced": {
+// STRUCT-NEXT:       "major": 12,
+// STRUCT-NEXT:       "minor": 0,
+// STRUCT-NEXT:       "patch": 0
+// STRUCT-NEXT:     }
+// STRUCT-NEXT:   }
+// STRUCT-NEXT: ]
+// STRUCT:      "title": "MyStruct"
+
+// expected-no-diagnostics



More information about the cfe-commits mailing list