[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:36 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: QuietMisdreavus (QuietMisdreavus)
<details>
<summary>Changes</summary>
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
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/120801
More information about the cfe-commits
mailing list