[clang] [ExtractAPI] merge anon declarators even if they're array types (PR #120801)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 20 14:26:03 PST 2024
https://github.com/QuietMisdreavus created https://github.com/llvm/llvm-project/pull/120801
Resolves rdar://140210765
ExtractAPI currently checks declarator decls for embedded tag decls so that it can fold in anonymous tag decls as necessary. However, this check appears to be tricked when the field decl specifies an array of this embedded anonymous tag decl. This PR adds an extra check to `ExtractAPIVisitorBase::maybeMergeWithAnonymousTag()` to poke inside array types for a potential anonymous tag decl to merge, so that it doesn't erroneously create symbols with empty names.
>From 61f08e3ed54e94734153150cdab02b9cb16df939 Mon Sep 17 00:00:00 2001
From: Vera Mitchell <vgm at apple.com>
Date: Fri, 20 Dec 2024 15:20:35 -0700
Subject: [PATCH] merge anon declarators even if they're array types
rdar://140210765
---
.../clang/ExtractAPI/ExtractAPIVisitor.h | 5 +++
.../ExtractAPI/anonymous_record_no_typedef.c | 40 +++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index b09b8b44d9abaa..aa86e418067114 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -252,6 +252,11 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor<Derived> {
if (!NewRecordContext)
return;
auto *Tag = D.getType()->getAsTagDecl();
+ if (!Tag) {
+ if (const auto *AT = D.getASTContext().getAsArrayType(D.getType())) {
+ Tag = AT->getElementType()->getAsTagDecl();
+ }
+ }
SmallString<128> TagUSR;
clang::index::generateUSRForDecl(Tag, TagUSR);
if (auto *Record = llvm::dyn_cast_if_present<TagRecord>(
diff --git a/clang/test/ExtractAPI/anonymous_record_no_typedef.c b/clang/test/ExtractAPI/anonymous_record_no_typedef.c
index c0c76ef1f06b57..d278cbb8ec348b 100644
--- a/clang/test/ExtractAPI/anonymous_record_no_typedef.c
+++ b/clang/test/ExtractAPI/anonymous_record_no_typedef.c
@@ -191,4 +191,44 @@ union Vector {
// VEC-DAG: "!testRelLabel": "memberOf $ c:@U at Vector@Sa at FI@X $ c:@U at Vector"
// VEC-DAG: "!testRelLabel": "memberOf $ c:@U at Vector@Sa at FI@Y $ c:@U at Vector"
+// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix MYSTRUCT
+// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix MYSTRUCT
+// RUN: FileCheck %s --input-file %t/output-c.symbols.json --check-prefix COUNTS
+// RUN: FileCheck %s --input-file %t/output-cxx.symbols.json --check-prefix COUNTS
+struct MyStruct {
+ struct {
+ int count;
+ } counts[1];
+};
+// MYSTRUCT-NOT: "spelling": ""
+// MYSTRUCT-NOT: "title": ""
+
+// COUNTS-LABEL: "!testLabel": "c:@S at MyStruct@FI at counts"
+// COUNTS: "declarationFragments": [
+// COUNTS-NEXT: {
+// COUNTS-NEXT: "kind": "keyword",
+// COUNTS-NEXT: "spelling": "struct"
+// COUNTS-NEXT: },
+// COUNTS-NEXT: {
+// COUNTS-NEXT: "kind": "text",
+// COUNTS-NEXT: "spelling": " { ... } "
+// COUNTS-NEXT: },
+// COUNTS-NEXT: {
+// COUNTS-NEXT: "kind": "identifier",
+// COUNTS-NEXT: "spelling": "counts"
+// COUNTS-NEXT: },
+// COUNTS-NEXT: {
+// COUNTS-NEXT: "kind": "text",
+// COUNTS-NEXT: "spelling": "["
+// COUNTS-NEXT: },
+// COUNTS-NEXT: {
+// COUNTS-NEXT: "kind": "number",
+// COUNTS-NEXT: "spelling": "1"
+// COUNTS-NEXT: },
+// COUNTS-NEXT: {
+// COUNTS-NEXT: "kind": "text",
+// COUNTS-NEXT: "spelling": "];"
+// COUNTS-NEXT: }
+// COUNTS-NEXT: ],
+
// expected-no-diagnostics
More information about the cfe-commits
mailing list