[clang] [clang][ExtractAPI] Flatten all enum cases from anonymous enums at top level (PR #93559)

2024-05-28 Thread Daniel Grumberg via cfe-commits

https://github.com/daniel-grumberg created 
https://github.com/llvm/llvm-project/pull/93559

rdar://128863241

>From d453e5b21d369d8c2b1d06c640887ec81ace Mon Sep 17 00:00:00 2001
From: Daniel Grumberg 
Date: Tue, 28 May 2024 15:43:45 +0100
Subject: [PATCH] [clang][ExtractAPI] Flatten all enum cases from anonymous
 enums at top level

rdar://128863241
---
 .../clang/ExtractAPI/ExtractAPIVisitor.h  |  57 +
 .../ExtractAPI/anonymous_record_no_typedef.c  |  42 ++-
 clang/test/ExtractAPI/enum.c  | 112 --
 clang/tools/libclang/CXExtractAPI.cpp |   3 +
 4 files changed, 50 insertions(+), 164 deletions(-)

diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h 
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 8ccebe457ed53..9df5138a223da 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -21,6 +21,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/ParentMapContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/Module.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Specifiers.h"
@@ -127,7 +128,7 @@ class ExtractAPIVisitorBase : public 
RecursiveASTVisitor {
 protected:
   /// Collect API information for the enum constants and associate with the
   /// parent enum.
-  void recordEnumConstants(EnumRecord *EnumRecord,
+  void recordEnumConstants(SymbolReference Container,
const EnumDecl::enumerator_range Constants);
 
   /// Collect API information for the Objective-C methods and associate with 
the
@@ -248,12 +249,8 @@ 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);
-auto *NewRecord = cast(NewRecordContext);
-if (NewRecord->Comment.empty())
-  NewRecord->Comment = Record->Comment;
-  }
 }
   }
 };
@@ -394,17 +391,6 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   if (!getDerivedExtractAPIVisitor().shouldDeclBeIncluded(Decl))
 return true;
 
-  SmallString<128> QualifiedNameBuffer;
-  // Collect symbol information.
-  StringRef Name = Decl->getName();
-  if (Name.empty())
-Name = getTypedefName(Decl);
-  if (Name.empty()) {
-llvm::raw_svector_ostream OS(QualifiedNameBuffer);
-Decl->printQualifiedName(OS);
-Name = QualifiedNameBuffer;
-  }
-
   SmallString<128> USR;
   index::generateUSRForDecl(Decl, USR);
   PresumedLoc Loc =
@@ -420,13 +406,27 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   DeclarationFragmentsBuilder::getFragmentsForEnum(Decl);
   DeclarationFragments SubHeading =
   DeclarationFragmentsBuilder::getSubHeading(Decl);
-  auto *ER = API.createRecord(
+
+  // Collect symbol information.
+  SymbolReference ParentContainer;
+
+  if (Decl->hasNameForLinkage()) {
+StringRef Name = Decl->getName();
+if (Name.empty())
+  Name = getTypedefName(Decl);
+
+auto *ER = API.createRecord(
   USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
   AvailabilityInfo::createFromDecl(Decl), Comment, Declaration, SubHeading,
-  isInSystemHeader(Decl), isEmbeddedInVarDeclarator(*Decl));
+  isInSystemHeader(Decl), false);
+ParentContainer = SymbolReference(ER);
+  } else {
+// If this an anonymous enum then the parent scope of the constants is the 
top level namespace.
+ParentContainer = {};
+  }
 
   // Now collect information about the enumerators in this enum.
-  getDerivedExtractAPIVisitor().recordEnumConstants(ER, Decl->enumerators());
+  getDerivedExtractAPIVisitor().recordEnumConstants(ParentContainer, 
Decl->enumerators());
 
   return true;
 }
@@ -1197,7 +1197,7 @@ bool 
ExtractAPIVisitorBase::VisitObjCCategoryDecl(
 /// parent enum.
 template 
 void ExtractAPIVisitorBase::recordEnumConstants(
-EnumRecord *EnumRecord, const EnumDecl::enumerator_range Constants) {
+SymbolReference Container, const EnumDecl::enumerator_range Constants) {
   for (const auto *Constant : Constants) {
 // Collect symbol information.
 StringRef Name = Constant->getName();
@@ -1218,7 +1218,7 @@ void ExtractAPIVisitorBase::recordEnumConstants(
 DeclarationFragmentsBuilder::getSubHeading(Constant);
 
 API.createRecord(
-USR, Name, createHierarchyInformationForDecl(*Constant), Loc,
+USR, Name, Container, Loc,
 AvailabilityInfo::createFromDecl(Constant), Comment, Declaration,
 SubHeading, isInSystemHeader(Constant));
   }
@@ -1469,7 +1469,18 @@ class ExtractAPIVisitor
 
   bool shouldDeclBeIncluded(const Decl *D) const { return true; }
   const RawComment *fetchRawCom

[clang] [clang][ExtractAPI] Flatten all enum cases from anonymous enums at top level (PR #93559)

2024-05-28 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Daniel Grumberg (daniel-grumberg)


Changes

rdar://128863241

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


4 Files Affected:

- (modified) clang/include/clang/ExtractAPI/ExtractAPIVisitor.h (+34-23) 
- (modified) clang/test/ExtractAPI/anonymous_record_no_typedef.c (+13-29) 
- (modified) clang/test/ExtractAPI/enum.c (-112) 
- (modified) clang/tools/libclang/CXExtractAPI.cpp (+3) 


``diff
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h 
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 8ccebe457ed53..9df5138a223da 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -21,6 +21,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/ParentMapContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/Module.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Specifiers.h"
@@ -127,7 +128,7 @@ class ExtractAPIVisitorBase : public 
RecursiveASTVisitor {
 protected:
   /// Collect API information for the enum constants and associate with the
   /// parent enum.
-  void recordEnumConstants(EnumRecord *EnumRecord,
+  void recordEnumConstants(SymbolReference Container,
const EnumDecl::enumerator_range Constants);
 
   /// Collect API information for the Objective-C methods and associate with 
the
@@ -248,12 +249,8 @@ 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);
-auto *NewRecord = cast(NewRecordContext);
-if (NewRecord->Comment.empty())
-  NewRecord->Comment = Record->Comment;
-  }
 }
   }
 };
@@ -394,17 +391,6 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   if (!getDerivedExtractAPIVisitor().shouldDeclBeIncluded(Decl))
 return true;
 
-  SmallString<128> QualifiedNameBuffer;
-  // Collect symbol information.
-  StringRef Name = Decl->getName();
-  if (Name.empty())
-Name = getTypedefName(Decl);
-  if (Name.empty()) {
-llvm::raw_svector_ostream OS(QualifiedNameBuffer);
-Decl->printQualifiedName(OS);
-Name = QualifiedNameBuffer;
-  }
-
   SmallString<128> USR;
   index::generateUSRForDecl(Decl, USR);
   PresumedLoc Loc =
@@ -420,13 +406,27 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   DeclarationFragmentsBuilder::getFragmentsForEnum(Decl);
   DeclarationFragments SubHeading =
   DeclarationFragmentsBuilder::getSubHeading(Decl);
-  auto *ER = API.createRecord(
+
+  // Collect symbol information.
+  SymbolReference ParentContainer;
+
+  if (Decl->hasNameForLinkage()) {
+StringRef Name = Decl->getName();
+if (Name.empty())
+  Name = getTypedefName(Decl);
+
+auto *ER = API.createRecord(
   USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
   AvailabilityInfo::createFromDecl(Decl), Comment, Declaration, SubHeading,
-  isInSystemHeader(Decl), isEmbeddedInVarDeclarator(*Decl));
+  isInSystemHeader(Decl), false);
+ParentContainer = SymbolReference(ER);
+  } else {
+// If this an anonymous enum then the parent scope of the constants is the 
top level namespace.
+ParentContainer = {};
+  }
 
   // Now collect information about the enumerators in this enum.
-  getDerivedExtractAPIVisitor().recordEnumConstants(ER, Decl->enumerators());
+  getDerivedExtractAPIVisitor().recordEnumConstants(ParentContainer, 
Decl->enumerators());
 
   return true;
 }
@@ -1197,7 +1197,7 @@ bool 
ExtractAPIVisitorBase::VisitObjCCategoryDecl(
 /// parent enum.
 template 
 void ExtractAPIVisitorBase::recordEnumConstants(
-EnumRecord *EnumRecord, const EnumDecl::enumerator_range Constants) {
+SymbolReference Container, const EnumDecl::enumerator_range Constants) {
   for (const auto *Constant : Constants) {
 // Collect symbol information.
 StringRef Name = Constant->getName();
@@ -1218,7 +1218,7 @@ void ExtractAPIVisitorBase::recordEnumConstants(
 DeclarationFragmentsBuilder::getSubHeading(Constant);
 
 API.createRecord(
-USR, Name, createHierarchyInformationForDecl(*Constant), Loc,
+USR, Name, Container, Loc,
 AvailabilityInfo::createFromDecl(Constant), Comment, Declaration,
 SubHeading, isInSystemHeader(Constant));
   }
@@ -1469,7 +1469,18 @@ class ExtractAPIVisitor
 
   bool shouldDeclBeIncluded(const Decl *D) const { return true; }
   const RawComment *fetchRawCommentForDecl(const Decl *D) const {
-return this->Context.getRawCommentForDeclNoCache(D);
+if (const auto *Comment = this->Context.getRawCommentForDeclNoCache(D))
+  return Co

[clang] [clang][ExtractAPI] Flatten all enum cases from anonymous enums at top level (PR #93559)

2024-05-28 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 8a395b00b85ad64a56a9c65055cc9a26a9499a8d 
d453e5b21d369d8c2b1d06c640887ec81ace -- 
clang/include/clang/ExtractAPI/ExtractAPIVisitor.h 
clang/test/ExtractAPI/anonymous_record_no_typedef.c 
clang/test/ExtractAPI/enum.c clang/tools/libclang/CXExtractAPI.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h 
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 9df5138a22..76d7fd798b 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -416,17 +416,19 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   Name = getTypedefName(Decl);
 
 auto *ER = API.createRecord(
-  USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
-  AvailabilityInfo::createFromDecl(Decl), Comment, Declaration, SubHeading,
-  isInSystemHeader(Decl), false);
+USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
+AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
+SubHeading, isInSystemHeader(Decl), false);
 ParentContainer = SymbolReference(ER);
   } else {
-// If this an anonymous enum then the parent scope of the constants is the 
top level namespace.
+// If this an anonymous enum then the parent scope of the constants is the
+// top level namespace.
 ParentContainer = {};
   }
 
   // Now collect information about the enumerators in this enum.
-  getDerivedExtractAPIVisitor().recordEnumConstants(ParentContainer, 
Decl->enumerators());
+  getDerivedExtractAPIVisitor().recordEnumConstants(ParentContainer,
+Decl->enumerators());
 
   return true;
 }
@@ -1218,9 +1220,8 @@ void ExtractAPIVisitorBase::recordEnumConstants(
 DeclarationFragmentsBuilder::getSubHeading(Constant);
 
 API.createRecord(
-USR, Name, Container, Loc,
-AvailabilityInfo::createFromDecl(Constant), Comment, Declaration,
-SubHeading, isInSystemHeader(Constant));
+USR, Name, Container, Loc, AvailabilityInfo::createFromDecl(Constant),
+Comment, Declaration, SubHeading, isInSystemHeader(Constant));
   }
 }
 
@@ -1473,10 +1474,9 @@ public:
   return Comment;
 
 if (const auto *Declarator = dyn_cast(D)) {
-  const auto *TagTypeDecl =
-  Declarator->getType()->getAsTagDecl();
+  const auto *TagTypeDecl = Declarator->getType()->getAsTagDecl();
   if (TagTypeDecl && TagTypeDecl->isEmbeddedInDeclarator() &&
-TagTypeDecl->isCompleteDefinition())
+  TagTypeDecl->isCompleteDefinition())
 return this->Context.getRawCommentForDeclNoCache(TagTypeDecl);
 }
 

``




https://github.com/llvm/llvm-project/pull/93559
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][ExtractAPI] Flatten all enum cases from anonymous enums at top level (PR #93559)

2024-05-28 Thread Daniel Grumberg via cfe-commits

https://github.com/daniel-grumberg updated 
https://github.com/llvm/llvm-project/pull/93559

>From 5c8258fb2bcc102d431af9f3ae41cf72ecc335b6 Mon Sep 17 00:00:00 2001
From: Daniel Grumberg 
Date: Tue, 28 May 2024 15:43:45 +0100
Subject: [PATCH] [clang][ExtractAPI] Flatten all enum cases from anonymous
 enums at top level

rdar://128863241
---
 .../clang/ExtractAPI/ExtractAPIVisitor.h  |  65 +-
 .../ExtractAPI/anonymous_record_no_typedef.c  |  42 ++-
 clang/test/ExtractAPI/enum.c  | 112 --
 clang/tools/libclang/CXExtractAPI.cpp |   3 +
 4 files changed, 54 insertions(+), 168 deletions(-)

diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h 
b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
index 8ccebe457ed53..76d7fd798bed3 100644
--- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
+++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
@@ -21,6 +21,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/ParentMapContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/Module.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/Specifiers.h"
@@ -127,7 +128,7 @@ class ExtractAPIVisitorBase : public 
RecursiveASTVisitor {
 protected:
   /// Collect API information for the enum constants and associate with the
   /// parent enum.
-  void recordEnumConstants(EnumRecord *EnumRecord,
+  void recordEnumConstants(SymbolReference Container,
const EnumDecl::enumerator_range Constants);
 
   /// Collect API information for the Objective-C methods and associate with 
the
@@ -248,12 +249,8 @@ 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);
-auto *NewRecord = cast(NewRecordContext);
-if (NewRecord->Comment.empty())
-  NewRecord->Comment = Record->Comment;
-  }
 }
   }
 };
@@ -394,17 +391,6 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   if (!getDerivedExtractAPIVisitor().shouldDeclBeIncluded(Decl))
 return true;
 
-  SmallString<128> QualifiedNameBuffer;
-  // Collect symbol information.
-  StringRef Name = Decl->getName();
-  if (Name.empty())
-Name = getTypedefName(Decl);
-  if (Name.empty()) {
-llvm::raw_svector_ostream OS(QualifiedNameBuffer);
-Decl->printQualifiedName(OS);
-Name = QualifiedNameBuffer;
-  }
-
   SmallString<128> USR;
   index::generateUSRForDecl(Decl, USR);
   PresumedLoc Loc =
@@ -420,13 +406,29 @@ bool ExtractAPIVisitorBase::VisitEnumDecl(const 
EnumDecl *Decl) {
   DeclarationFragmentsBuilder::getFragmentsForEnum(Decl);
   DeclarationFragments SubHeading =
   DeclarationFragmentsBuilder::getSubHeading(Decl);
-  auto *ER = API.createRecord(
-  USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
-  AvailabilityInfo::createFromDecl(Decl), Comment, Declaration, SubHeading,
-  isInSystemHeader(Decl), isEmbeddedInVarDeclarator(*Decl));
+
+  // Collect symbol information.
+  SymbolReference ParentContainer;
+
+  if (Decl->hasNameForLinkage()) {
+StringRef Name = Decl->getName();
+if (Name.empty())
+  Name = getTypedefName(Decl);
+
+auto *ER = API.createRecord(
+USR, Name, createHierarchyInformationForDecl(*Decl), Loc,
+AvailabilityInfo::createFromDecl(Decl), Comment, Declaration,
+SubHeading, isInSystemHeader(Decl), false);
+ParentContainer = SymbolReference(ER);
+  } else {
+// If this an anonymous enum then the parent scope of the constants is the
+// top level namespace.
+ParentContainer = {};
+  }
 
   // Now collect information about the enumerators in this enum.
-  getDerivedExtractAPIVisitor().recordEnumConstants(ER, Decl->enumerators());
+  getDerivedExtractAPIVisitor().recordEnumConstants(ParentContainer,
+Decl->enumerators());
 
   return true;
 }
@@ -1197,7 +1199,7 @@ bool 
ExtractAPIVisitorBase::VisitObjCCategoryDecl(
 /// parent enum.
 template 
 void ExtractAPIVisitorBase::recordEnumConstants(
-EnumRecord *EnumRecord, const EnumDecl::enumerator_range Constants) {
+SymbolReference Container, const EnumDecl::enumerator_range Constants) {
   for (const auto *Constant : Constants) {
 // Collect symbol information.
 StringRef Name = Constant->getName();
@@ -1218,9 +1220,8 @@ void ExtractAPIVisitorBase::recordEnumConstants(
 DeclarationFragmentsBuilder::getSubHeading(Constant);
 
 API.createRecord(
-USR, Name, createHierarchyInformationForDecl(*Constant), Loc,
-AvailabilityInfo::createFromDecl(Constant), Comment, Declaration,
-SubHeading, isInSystemHeader(Constan

[clang] [clang][ExtractAPI] Flatten all enum cases from anonymous enums at top level (PR #93559)

2024-05-28 Thread via cfe-commits

https://github.com/QuietMisdreavus approved this pull request.


https://github.com/llvm/llvm-project/pull/93559
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][ExtractAPI] Flatten all enum cases from anonymous enums at top level (PR #93559)

2024-05-29 Thread Daniel Grumberg via cfe-commits

https://github.com/daniel-grumberg closed 
https://github.com/llvm/llvm-project/pull/93559
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits