From 8f4f3df3c0ffc22c09d9201980a88ebb8aecc8f8 Mon Sep 17 00:00:00 2001 From: Daniel Grumberg Date: Tue, 20 Aug 2024 15:36:46 +0100 Subject: [PATCH] Reenable anon structs (#104922) Add back missing includes and revert revert "[clang][ExtractAPI] Stop dropping fields of nested anonymous record types when they aren't attached to variable declaration (#104600)" --- clang/include/clang/ExtractAPI/API.h | 25 +++++---- .../clang/ExtractAPI/ExtractAPIVisitor.h | 37 ++++++++++++- clang/lib/ExtractAPI/API.cpp | 53 ++++++++++++++++++- .../Serialization/SymbolGraphSerializer.cpp | 8 --- .../ExtractAPI/anonymous_record_no_typedef.c | 12 +++++ 5 files changed, 110 insertions(+), 25 deletions(-) diff --git a/clang/include/clang/ExtractAPI/API.h b/clang/include/clang/ExtractAPI/API.h index bf291074fd0614..188e35b72117b5 100644 --- a/clang/include/clang/ExtractAPI/API.h +++ b/clang/include/clang/ExtractAPI/API.h @@ -19,21 +19,13 @@ #define LLVM_CLANG_EXTRACTAPI_API_H #include "clang/AST/Availability.h" -#include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" -#include "clang/AST/DeclObjC.h" #include "clang/AST/RawCommentList.h" #include "clang/Basic/SourceLocation.h" -#include "clang/Basic/Specifiers.h" #include "clang/ExtractAPI/DeclarationFragments.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/MapVector.h" -#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Casting.h" -#include "llvm/Support/Compiler.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/Triple.h" #include #include @@ -328,6 +320,8 @@ class RecordContext { /// chain. void stealRecordChain(RecordContext &Other); + void removeFromRecordChain(APIRecord *Record); + APIRecord::RecordKind getKind() const { return Kind; } struct record_iterator { @@ -1426,10 +1420,15 @@ class APISet { typename std::enable_if_t, RecordTy> * createRecord(StringRef USR, StringRef Name, CtorArgsContTy &&...CtorArgs); - ArrayRef getTopLevelRecords() const { - return TopLevelRecords; + auto getTopLevelRecords() const { + return llvm::iterator_range( + TopLevelRecords); } + void removeRecord(StringRef USR); + + void removeRecord(APIRecord *Record); + APISet(const llvm::Triple &Target, Language Lang, const std::string &ProductName) : Target(Target), Lang(Lang), ProductName(ProductName) {} @@ -1456,7 +1455,7 @@ class APISet { // lives in the BumpPtrAllocator. using APIRecordStoredPtr = std::unique_ptr; llvm::DenseMap USRBasedLookupTable; - std::vector TopLevelRecords; + llvm::SmallPtrSet TopLevelRecords; public: const std::string ProductName; @@ -1482,7 +1481,7 @@ APISet::createRecord(StringRef USR, StringRef Name, dyn_cast_if_present(Record->Parent.Record)) ParentContext->addToRecordChain(Record); else - TopLevelRecords.push_back(Record); + TopLevelRecords.insert(Record); } else { Record = dyn_cast(Result.first->second.get()); } diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h index 1b27027621666a..67659f5a25037c 100644 --- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h +++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h @@ -40,6 +40,8 @@ namespace impl { template class ExtractAPIVisitorBase : public RecursiveASTVisitor { + using Base = RecursiveASTVisitor; + protected: ExtractAPIVisitorBase(ASTContext &Context, APISet &API) : Context(Context), API(API) {} @@ -81,8 +83,10 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor { bool VisitNamespaceDecl(const NamespaceDecl *Decl); + bool TraverseRecordDecl(RecordDecl *Decl); bool VisitRecordDecl(const RecordDecl *Decl); + bool TraverseCXXRecordDecl(CXXRecordDecl *Decl); bool VisitCXXRecordDecl(const CXXRecordDecl *Decl); bool VisitCXXMethodDecl(const CXXMethodDecl *Decl); @@ -240,7 +244,7 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor { bool isEmbeddedInVarDeclarator(const TagDecl &D) { return D.getName().empty() && getTypedefName(&D).empty() && - D.isEmbeddedInDeclarator(); + D.isEmbeddedInDeclarator() && !D.isFreeStanding(); } void maybeMergeWithAnonymousTag(const DeclaratorDecl &D, @@ -252,8 +256,10 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor { clang::index::generateUSRForDecl(Tag, TagUSR); if (auto *Record = llvm::dyn_cast_if_present( API.findRecordForUSR(TagUSR))) { - if (Record->IsEmbeddedInVarDeclarator) + if (Record->IsEmbeddedInVarDeclarator) { NewRecordContext->stealRecordChain(*Record); + API.removeRecord(Record); + } } } }; @@ -548,6 +554,19 @@ bool ExtractAPIVisitorBase::VisitNamespaceDecl( return true; } +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; +} + template bool ExtractAPIVisitorBase::VisitRecordDecl(const RecordDecl *Decl) { if (!getDerivedExtractAPIVisitor().shouldDeclBeIncluded(Decl)) @@ -588,6 +607,20 @@ bool ExtractAPIVisitorBase::VisitRecordDecl(const RecordDecl *Decl) { return true; } +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); + } + + return Ret; +} + template bool ExtractAPIVisitorBase::VisitCXXRecordDecl( const CXXRecordDecl *Decl) { diff --git a/clang/lib/ExtractAPI/API.cpp b/clang/lib/ExtractAPI/API.cpp index ab1108f663deac..9dbc023885c37f 100644 --- a/clang/lib/ExtractAPI/API.cpp +++ b/clang/lib/ExtractAPI/API.cpp @@ -13,8 +13,6 @@ //===----------------------------------------------------------------------===// #include "clang/ExtractAPI/API.h" -#include "clang/AST/RawCommentList.h" -#include "clang/Index/USRGeneration.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/ErrorHandling.h" #include @@ -60,6 +58,10 @@ bool RecordContext::IsWellFormed() const { void RecordContext::stealRecordChain(RecordContext &Other) { assert(IsWellFormed()); + // Other's record chain is empty, nothing to do + if (Other.First == nullptr && Other.Last == nullptr) + return; + // If we don't have an empty chain append Other's chain into ours. if (First) Last->NextInContext = Other.First; @@ -68,6 +70,10 @@ void RecordContext::stealRecordChain(RecordContext &Other) { Last = Other.Last; + for (auto *StolenRecord = Other.First; StolenRecord != nullptr; + StolenRecord = StolenRecord->getNextInContext()) + StolenRecord->Parent = SymbolReference(cast(this)); + // Delete Other's chain to ensure we don't accidentally traverse it. Other.First = nullptr; Other.Last = nullptr; @@ -85,6 +91,22 @@ void RecordContext::addToRecordChain(APIRecord *Record) const { Last = Record; } +void RecordContext::removeFromRecordChain(APIRecord *Record) { + APIRecord *Prev = nullptr; + for (APIRecord *Curr = First; Curr != Record; Curr = Curr->NextInContext) + Prev = Curr; + + if (Prev) + Prev->NextInContext = Record->NextInContext; + else + First = Record->NextInContext; + + if (Last == Record) + Last = Prev; + + Record->NextInContext = nullptr; +} + APIRecord *APISet::findRecordForUSR(StringRef USR) const { if (USR.empty()) return nullptr; @@ -114,6 +136,33 @@ SymbolReference APISet::createSymbolReference(StringRef Name, StringRef USR, return SymbolReference(copyString(Name), copyString(USR), copyString(Source)); } +void APISet::removeRecord(StringRef USR) { + auto Result = USRBasedLookupTable.find(USR); + if (Result != USRBasedLookupTable.end()) { + auto *Record = Result->getSecond().get(); + auto &ParentReference = Record->Parent; + auto *ParentRecord = const_cast(ParentReference.Record); + if (!ParentRecord) + ParentRecord = findRecordForUSR(ParentReference.USR); + + if (auto *ParentCtx = llvm::cast_if_present(ParentRecord)) { + ParentCtx->removeFromRecordChain(Record); + if (auto *RecordAsCtx = llvm::dyn_cast(Record)) + ParentCtx->stealRecordChain(*RecordAsCtx); + } else { + TopLevelRecords.erase(Record); + if (auto *RecordAsCtx = llvm::dyn_cast(Record)) { + for (const auto *Child = RecordAsCtx->First; Child != nullptr; + Child = Child->getNextInContext()) + TopLevelRecords.insert(Child); + } + } + USRBasedLookupTable.erase(Result); + } +} + +void APISet::removeRecord(APIRecord *Record) { removeRecord(Record->USR); } + APIRecord::~APIRecord() {} TagRecord::~TagRecord() {} RecordRecord::~RecordRecord() {} diff --git a/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp b/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp index 1f8029cbd39ad2..1bce9c59b19791 100644 --- a/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp +++ b/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp @@ -673,14 +673,6 @@ bool SymbolGraphSerializer::shouldSkip(const APIRecord *Record) const { if (Record->Availability.isUnconditionallyUnavailable()) return true; - // Filter out symbols without a name as we can generate correct symbol graphs - // for them. In practice these are anonymous record types that aren't attached - // to a declaration. - if (auto *Tag = dyn_cast(Record)) { - if (Tag->IsEmbeddedInVarDeclarator) - return true; - } - // Filter out symbols prefixed with an underscored as they are understood to // be symbols clients should not use. if (Record->Name.starts_with("_")) diff --git a/clang/test/ExtractAPI/anonymous_record_no_typedef.c b/clang/test/ExtractAPI/anonymous_record_no_typedef.c index 789316ca8930b8..064c223ad56e73 100644 --- a/clang/test/ExtractAPI/anonymous_record_no_typedef.c +++ b/clang/test/ExtractAPI/anonymous_record_no_typedef.c @@ -163,4 +163,16 @@ enum { // GLOBALOTHERCASE-NEXT: "GlobalOtherCase" // GLOBALOTHERCASE-NEXT: ] +// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix VEC +union Vector { + struct { + float X; + float Y; + }; + float Data[2]; +}; +// VEC-DAG: "!testRelLabel": "memberOf $ c:@U@Vector@FI@Data $ c:@U@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" + // expected-no-diagnostics