[clang] [clang][ExtractAPI] Fix for nested types are not enabled. (PR #159698)
chaitanyav wrote: @daniel-grumberg please review. https://github.com/llvm/llvm-project/pull/159698 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] Fix for nested types are not enabled. (PR #159698)
https://github.com/chaitanyav updated
https://github.com/llvm/llvm-project/pull/159698
>From c24df2cedb575c76c89ec5ba2604967e3d3e91c5 Mon Sep 17 00:00:00 2001
From: NagaChaitanya Vellanki
Date: Thu, 18 Sep 2025 20:36:53 -0700
Subject: [PATCH] [clang][ExtractAPI] Fix for nested types are not enabled.
Recursively extract nested types. Keep anonymous structs/unions embedded in
var declarators
Fixes #61477
---
.../clang/ExtractAPI/ExtractAPIVisitor.h | 68 +--
.../ExtractAPI/anonymous_record_no_typedef.c | 24 +-
clang/test/ExtractAPI/nested_types.cpp| 290 ++
clang/test/ExtractAPI/nested_types_deep.cpp | 532 ++
4 files changed, 850 insertions(+), 64 deletions(-)
create mode 100644 clang/test/ExtractAPI/nested_types.cpp
create mode 100644 clang/test/ExtractAPI/nested_types_deep.cpp
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 9ea664f57f828..5f0b8dfcede62 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -250,27 +250,6 @@ class ExtractAPIVisitorBase : public
RecursiveASTVisitor {
return D.getName().empty() && getTypedefName(&D).empty() &&
D.isEmbeddedInDeclarator() && !D.isFreeStanding();
}
-
- void maybeMergeWithAnonymousTag(const DeclaratorDecl &D,
- RecordContext *NewRecordContext) {
-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(
-API.findRecordForUSR(TagUSR))) {
- if (Record->IsEmbeddedInVarDeclarator) {
-NewRecordContext->stealRecordChain(*Record);
-API.removeRecord(Record);
- }
-}
- }
};
template
@@ -322,14 +301,10 @@ bool ExtractAPIVisitorBase::VisitVarDecl(const
VarDecl *Decl) {
SubHeading, Access, isInSystemHeader(Decl));
} else {
// Add the global variable record to the API set.
-auto *NewRecord = API.createRecord(
+API.createRecord(
USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Linkage, Comment, Declaration,
SubHeading, isInSystemHeader(Decl));
-
-// If this global variable has a non typedef'd anonymous tag type let's
-// pretend the type's child records are under us in the hierarchy.
-maybeMergeWithAnonymousTag(*Decl, NewRecord);
}
return true;
@@ -565,15 +540,7 @@ bool ExtractAPIVisitorBase::VisitNamespaceDecl(
template
bool ExtractAPIVisitorBase::TraverseRecordDecl(RecordDecl *Decl) {
- bool Ret = Base::TraverseRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
- return Ret;
+ return Base::TraverseRecordDecl(Decl);
}
template
@@ -620,13 +587,7 @@ template
bool ExtractAPIVisitorBase::TraverseCXXRecordDecl(
CXXRecordDecl *Decl) {
bool Ret = Base::TraverseCXXRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
+ // Keep anonymous structs in the hierarchy to preserve path components
return Ret;
}
@@ -643,6 +604,11 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
SmallString<128> USR;
index::generateUSRForDecl(Decl, USR);
+
+ if (API.findRecordForUSR(USR)) {
+return true;
+ }
+
PresumedLoc Loc =
Context.getSourceManager().getPresumedLoc(Decl->getLocation());
DocComment Comment;
@@ -679,6 +645,13 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
Record->KindForDisplay = getKindForDisplay(Decl);
Record->Bases = getBases(Decl);
+ // Visit nested records
+ for (const auto *NestedDecl : Decl->decls()) {
+if (auto *NestedRecord = dyn_cast(NestedDecl)) {
+ VisitCXXRecordDecl(NestedRecord);
+}
+ }
+
return true;
}
@@ -1320,31 +1293,26 @@ bool
ExtractAPIVisitorBase::VisitFieldDecl(const FieldDecl *Decl) {
DeclarationFragments SubHeading =
DeclarationFragmentsBuilder::getSubHeading(Decl);
- RecordContext *NewRecord = nullptr;
if (isa(Decl->getDeclContext())) {
AccessControl Access = DeclarationFragmentsBuilder::getAccessControl(Decl);
-NewRecord = API.createRecord(
+API.createRecord(
USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
SubHeading, Access, isInSystemHeader(Decl));
} else if (auto *RD = dyn_cast(Decl->getDeclContext())) {
[clang] [clang][ExtractAPI] Fix for nested types are not enabled. (PR #159698)
https://github.com/chaitanyav updated
https://github.com/llvm/llvm-project/pull/159698
>From 6b638a98afd02759b4de7031585a1f0d0ffc8740 Mon Sep 17 00:00:00 2001
From: NagaChaitanya Vellanki
Date: Thu, 18 Sep 2025 20:36:53 -0700
Subject: [PATCH] [clang][ExtractAPI] Fix for nested types are not enabled.
Recursively extract nested types. Keep anonymous structs/unions embedded in
var declarators
Fixes #61477
---
.../clang/ExtractAPI/ExtractAPIVisitor.h | 68 +--
.../ExtractAPI/anonymous_record_no_typedef.c | 24 +-
clang/test/ExtractAPI/nested_types.cpp| 290 ++
clang/test/ExtractAPI/nested_types_deep.cpp | 532 ++
4 files changed, 850 insertions(+), 64 deletions(-)
create mode 100644 clang/test/ExtractAPI/nested_types.cpp
create mode 100644 clang/test/ExtractAPI/nested_types_deep.cpp
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 9ea664f57f828..5f0b8dfcede62 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -250,27 +250,6 @@ class ExtractAPIVisitorBase : public
RecursiveASTVisitor {
return D.getName().empty() && getTypedefName(&D).empty() &&
D.isEmbeddedInDeclarator() && !D.isFreeStanding();
}
-
- void maybeMergeWithAnonymousTag(const DeclaratorDecl &D,
- RecordContext *NewRecordContext) {
-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(
-API.findRecordForUSR(TagUSR))) {
- if (Record->IsEmbeddedInVarDeclarator) {
-NewRecordContext->stealRecordChain(*Record);
-API.removeRecord(Record);
- }
-}
- }
};
template
@@ -322,14 +301,10 @@ bool ExtractAPIVisitorBase::VisitVarDecl(const
VarDecl *Decl) {
SubHeading, Access, isInSystemHeader(Decl));
} else {
// Add the global variable record to the API set.
-auto *NewRecord = API.createRecord(
+API.createRecord(
USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Linkage, Comment, Declaration,
SubHeading, isInSystemHeader(Decl));
-
-// If this global variable has a non typedef'd anonymous tag type let's
-// pretend the type's child records are under us in the hierarchy.
-maybeMergeWithAnonymousTag(*Decl, NewRecord);
}
return true;
@@ -565,15 +540,7 @@ bool ExtractAPIVisitorBase::VisitNamespaceDecl(
template
bool ExtractAPIVisitorBase::TraverseRecordDecl(RecordDecl *Decl) {
- bool Ret = Base::TraverseRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
- return Ret;
+ return Base::TraverseRecordDecl(Decl);
}
template
@@ -620,13 +587,7 @@ template
bool ExtractAPIVisitorBase::TraverseCXXRecordDecl(
CXXRecordDecl *Decl) {
bool Ret = Base::TraverseCXXRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
+ // Keep anonymous structs in the hierarchy to preserve path components
return Ret;
}
@@ -643,6 +604,11 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
SmallString<128> USR;
index::generateUSRForDecl(Decl, USR);
+
+ if (API.findRecordForUSR(USR)) {
+return true;
+ }
+
PresumedLoc Loc =
Context.getSourceManager().getPresumedLoc(Decl->getLocation());
DocComment Comment;
@@ -679,6 +645,13 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
Record->KindForDisplay = getKindForDisplay(Decl);
Record->Bases = getBases(Decl);
+ // Visit nested records
+ for (const auto *NestedDecl : Decl->decls()) {
+if (auto *NestedRecord = dyn_cast(NestedDecl)) {
+ VisitCXXRecordDecl(NestedRecord);
+}
+ }
+
return true;
}
@@ -1320,31 +1293,26 @@ bool
ExtractAPIVisitorBase::VisitFieldDecl(const FieldDecl *Decl) {
DeclarationFragments SubHeading =
DeclarationFragmentsBuilder::getSubHeading(Decl);
- RecordContext *NewRecord = nullptr;
if (isa(Decl->getDeclContext())) {
AccessControl Access = DeclarationFragmentsBuilder::getAccessControl(Decl);
-NewRecord = API.createRecord(
+API.createRecord(
USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
SubHeading, Access, isInSystemHeader(Decl));
} else if (auto *RD = dyn_cast(Decl->getDeclContext())) {
[clang] [clang][ExtractAPI] Fix for nested types are not enabled. (PR #159698)
https://github.com/chaitanyav updated
https://github.com/llvm/llvm-project/pull/159698
>From 101054caeeb2889fc4ced2a207d9b029fbbc98e8 Mon Sep 17 00:00:00 2001
From: NagaChaitanya Vellanki
Date: Thu, 18 Sep 2025 20:36:53 -0700
Subject: [PATCH] [clang][ExtractAPI] Fix for nested types are not enabled.
Recursively extract nested types. Keep anonymous structs/unions embedded in
var declarators
Fixes #61477
---
.../clang/ExtractAPI/ExtractAPIVisitor.h | 68 +--
.../ExtractAPI/anonymous_record_no_typedef.c | 24 +-
clang/test/ExtractAPI/nested_types.cpp| 290 ++
clang/test/ExtractAPI/nested_types_deep.cpp | 532 ++
4 files changed, 850 insertions(+), 64 deletions(-)
create mode 100644 clang/test/ExtractAPI/nested_types.cpp
create mode 100644 clang/test/ExtractAPI/nested_types_deep.cpp
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 9ea664f57f828..5f0b8dfcede62 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -250,27 +250,6 @@ class ExtractAPIVisitorBase : public
RecursiveASTVisitor {
return D.getName().empty() && getTypedefName(&D).empty() &&
D.isEmbeddedInDeclarator() && !D.isFreeStanding();
}
-
- void maybeMergeWithAnonymousTag(const DeclaratorDecl &D,
- RecordContext *NewRecordContext) {
-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(
-API.findRecordForUSR(TagUSR))) {
- if (Record->IsEmbeddedInVarDeclarator) {
-NewRecordContext->stealRecordChain(*Record);
-API.removeRecord(Record);
- }
-}
- }
};
template
@@ -322,14 +301,10 @@ bool ExtractAPIVisitorBase::VisitVarDecl(const
VarDecl *Decl) {
SubHeading, Access, isInSystemHeader(Decl));
} else {
// Add the global variable record to the API set.
-auto *NewRecord = API.createRecord(
+API.createRecord(
USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Linkage, Comment, Declaration,
SubHeading, isInSystemHeader(Decl));
-
-// If this global variable has a non typedef'd anonymous tag type let's
-// pretend the type's child records are under us in the hierarchy.
-maybeMergeWithAnonymousTag(*Decl, NewRecord);
}
return true;
@@ -565,15 +540,7 @@ bool ExtractAPIVisitorBase::VisitNamespaceDecl(
template
bool ExtractAPIVisitorBase::TraverseRecordDecl(RecordDecl *Decl) {
- bool Ret = Base::TraverseRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
- return Ret;
+ return Base::TraverseRecordDecl(Decl);
}
template
@@ -620,13 +587,7 @@ template
bool ExtractAPIVisitorBase::TraverseCXXRecordDecl(
CXXRecordDecl *Decl) {
bool Ret = Base::TraverseCXXRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
+ // Keep anonymous structs in the hierarchy to preserve path components
return Ret;
}
@@ -643,6 +604,11 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
SmallString<128> USR;
index::generateUSRForDecl(Decl, USR);
+
+ if (API.findRecordForUSR(USR)) {
+return true;
+ }
+
PresumedLoc Loc =
Context.getSourceManager().getPresumedLoc(Decl->getLocation());
DocComment Comment;
@@ -679,6 +645,13 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
Record->KindForDisplay = getKindForDisplay(Decl);
Record->Bases = getBases(Decl);
+ // Visit nested records
+ for (const auto *NestedDecl : Decl->decls()) {
+if (auto *NestedRecord = dyn_cast(NestedDecl)) {
+ VisitCXXRecordDecl(NestedRecord);
+}
+ }
+
return true;
}
@@ -1320,31 +1293,26 @@ bool
ExtractAPIVisitorBase::VisitFieldDecl(const FieldDecl *Decl) {
DeclarationFragments SubHeading =
DeclarationFragmentsBuilder::getSubHeading(Decl);
- RecordContext *NewRecord = nullptr;
if (isa(Decl->getDeclContext())) {
AccessControl Access = DeclarationFragmentsBuilder::getAccessControl(Decl);
-NewRecord = API.createRecord(
+API.createRecord(
USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
SubHeading, Access, isInSystemHeader(Decl));
} else if (auto *RD = dyn_cast(Decl->getDeclContext())) {
[clang] [clang][ExtractAPI] Fix for nested types are not enabled. (PR #159698)
QuietMisdreavus wrote: cc @daniel-grumberg, i don't 100% remember the full context or the desired/current state of the world, but i do remember that we went back and forth about the state of "anonymous types in var declarators" a couple times. https://github.com/llvm/llvm-project/pull/159698 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][ExtractAPI] Fix for nested types are not enabled. (PR #159698)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: NagaChaitanya Vellanki (chaitanyav)
Changes
Keep anonymous structs/unions embedded in var declarators
Fixes #61477
---
Full diff: https://github.com/llvm/llvm-project/pull/159698.diff
1 Files Affected:
- (modified) clang/include/clang/ExtractAPI/ExtractAPIVisitor.h (+17-23)
``diff
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 9ea664f57f828..5b7a8266d2092 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -261,15 +261,9 @@ class ExtractAPIVisitorBase : public
RecursiveASTVisitor {
Tag = AT->getElementType()->getAsTagDecl();
}
}
-SmallString<128> TagUSR;
-clang::index::generateUSRForDecl(Tag, TagUSR);
-if (auto *Record = llvm::dyn_cast_if_present(
-API.findRecordForUSR(TagUSR))) {
- if (Record->IsEmbeddedInVarDeclarator) {
-NewRecordContext->stealRecordChain(*Record);
-API.removeRecord(Record);
- }
-}
+// For anonymous structs/unions embedded in var declarators,
+// we keep them in the hierarchy (don't remove them)
+// This preserves the path components while maintaining relationships
}
};
@@ -566,13 +560,7 @@ bool ExtractAPIVisitorBase::VisitNamespaceDecl(
template
bool ExtractAPIVisitorBase::TraverseRecordDecl(RecordDecl *Decl) {
bool Ret = Base::TraverseRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
+ // Keep anonymous structs in the hierarchy to preserve path components
return Ret;
}
@@ -620,13 +608,7 @@ template
bool ExtractAPIVisitorBase::TraverseCXXRecordDecl(
CXXRecordDecl *Decl) {
bool Ret = Base::TraverseCXXRecordDecl(Decl);
-
- if (!isEmbeddedInVarDeclarator(*Decl) && Decl->isAnonymousStructOrUnion()) {
-SmallString<128> USR;
-index::generateUSRForDecl(Decl, USR);
-API.removeRecord(USR);
- }
-
+ // Keep anonymous structs in the hierarchy to preserve path components
return Ret;
}
@@ -643,6 +625,11 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
SmallString<128> USR;
index::generateUSRForDecl(Decl, USR);
+
+ if (auto *USRRecord = API.findRecordForUSR(USR)) {
+return true;
+ }
+
PresumedLoc Loc =
Context.getSourceManager().getPresumedLoc(Decl->getLocation());
DocComment Comment;
@@ -679,6 +666,13 @@ bool ExtractAPIVisitorBase::VisitCXXRecordDecl(
Record->KindForDisplay = getKindForDisplay(Decl);
Record->Bases = getBases(Decl);
+ // Visit nested records
+ for (const auto *NestedDecl : Decl->decls()) {
+if (auto *NestedRecord = dyn_cast(NestedDecl)) {
+ VisitCXXRecordDecl(NestedRecord);
+}
+ }
+
return true;
}
``
https://github.com/llvm/llvm-project/pull/159698
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
