Skip to content

Conversation

@QuietMisdreavus
Copy link
Contributor

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.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2024

@llvm/pr-subscribers-clang

Author: QuietMisdreavus (QuietMisdreavus)

Changes

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.


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

2 Files Affected:

  • (modified) clang/include/clang/ExtractAPI/ExtractAPIVisitor.h (+5)
  • (modified) clang/test/ExtractAPI/anonymous_record_no_typedef.c (+40)
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@Vector@Sa@FI@X $ c:@U@Vector"
 // VEC-DAG: "!testRelLabel": "memberOf $ c:@U@Vector@Sa@FI@Y $ c:@U@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@MyStruct@FI@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

@QuietMisdreavus QuietMisdreavus merged commit a368402 into llvm:main Jan 29, 2025
10 checks passed
@QuietMisdreavus QuietMisdreavus deleted the unnamed-records branch January 29, 2025 20:03
QuietMisdreavus added a commit to swiftlang/llvm-project that referenced this pull request Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants