[llvm-branch-commits] [clang-tools-extra] [clang-doc] Clean up implementation with better casting (PR #202060)
Paul Kirth via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Jun 6 09:54:40 PDT 2026
https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/202060
>From 0b156e1200ca33540912af11573f4acc17e26cdc Mon Sep 17 00:00:00 2001
From: Paul Kirth <pk1574 at gmail.com>
Date: Fri, 5 Jun 2026 22:26:47 -0700
Subject: [PATCH] [clang-doc] Clean up implementation with better casting
Having access to RTTI style casting lets us use slightly nicer
structures to clean up the overly complicated dispatch logic in merging
and other places.
---
.../clang-doc/Representation.cpp | 70 +++++--------------
clang-tools-extra/clang-doc/Representation.h | 20 ++++--
2 files changed, 34 insertions(+), 56 deletions(-)
diff --git a/clang-tools-extra/clang-doc/Representation.cpp b/clang-tools-extra/clang-doc/Representation.cpp
index 9c13f6bfa566d..6b05e95629cbf 100644
--- a/clang-tools-extra/clang-doc/Representation.cpp
+++ b/clang-tools-extra/clang-doc/Representation.cpp
@@ -180,70 +180,38 @@ void mergeUnkeyed<CommentInfo>(DocList<CommentInfo> &Target,
}
}
+template <typename T>
+static llvm::Error mergeTypedInfo(Info *&Reduced, Info *NewInfo,
+ llvm::BumpPtrAllocator &Arena) {
+ if (!Reduced)
+ Reduced = allocatePtr<T>(Arena, NewInfo->USR);
+ cast<T>(Reduced)->merge(std::move(*cast<T>(NewInfo)));
+ return llvm::Error::success();
+}
+
llvm::Error mergeSingleInfo(doc::Info *&Reduced, doc::Info *NewInfo,
llvm::BumpPtrAllocator &Arena) {
- if (!Reduced) {
- switch (NewInfo->IT) {
- case InfoType::IT_namespace:
- Reduced = allocatePtr<NamespaceInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_record:
- Reduced = allocatePtr<RecordInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_enum:
- Reduced = allocatePtr<EnumInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_function:
- Reduced = allocatePtr<FunctionInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_typedef:
- Reduced = allocatePtr<TypedefInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_concept:
- Reduced = allocatePtr<ConceptInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_variable:
- Reduced = allocatePtr<VarInfo>(Arena, NewInfo->USR);
- break;
- case InfoType::IT_friend:
- Reduced = allocatePtr<FriendInfo>(Arena, NewInfo->USR);
- break;
- default:
- return llvm::createStringError(llvm::inconvertibleErrorCode(),
- "unknown info type");
- }
- }
-
- if (Reduced->IT != NewInfo->IT)
+ if (Reduced && Reduced->IT != NewInfo->IT)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"info types mismatch");
- switch (Reduced->IT) {
+ switch (NewInfo->IT) {
case InfoType::IT_namespace:
- cast<NamespaceInfo>(Reduced)->merge(
- std::move(*cast<NamespaceInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<NamespaceInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_record:
- cast<RecordInfo>(Reduced)->merge(std::move(*cast<RecordInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<RecordInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_enum:
- cast<EnumInfo>(Reduced)->merge(std::move(*cast<EnumInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<EnumInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_function:
- cast<FunctionInfo>(Reduced)->merge(std::move(*cast<FunctionInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<FunctionInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_typedef:
- cast<TypedefInfo>(Reduced)->merge(std::move(*cast<TypedefInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<TypedefInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_concept:
- cast<ConceptInfo>(Reduced)->merge(std::move(*cast<ConceptInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<ConceptInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_variable:
- cast<VarInfo>(Reduced)->merge(std::move(*cast<VarInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<VarInfo>(Reduced, NewInfo, Arena);
case InfoType::IT_friend:
- cast<FriendInfo>(Reduced)->merge(std::move(*cast<FriendInfo>(NewInfo)));
- break;
+ return mergeTypedInfo<FriendInfo>(Reduced, NewInfo, Arena);
default:
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"unknown info type");
diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h
index e3ad7fa3f375d..3189d4e19e5e3 100644
--- a/clang-tools-extra/clang-doc/Representation.h
+++ b/clang-tools-extra/clang-doc/Representation.h
@@ -586,7 +586,9 @@ struct NamespaceInfo : public Info {
NamespaceInfo(SymbolID USR = SymbolID(), StringRef Name = StringRef(),
StringRef Path = StringRef());
- static bool classof(const Info *I) { return I->IT == InfoType::IT_namespace; }
+ static bool classof(const Info *I) {
+ return I->IT == InfoType::IT_namespace;
+ }
void merge(NamespaceInfo &&I);
@@ -665,7 +667,9 @@ struct VarInfo : public SymbolInfo {
VarInfo() : SymbolInfo(InfoType::IT_variable) {}
explicit VarInfo(SymbolID USR) : SymbolInfo(InfoType::IT_variable, USR) {}
- static bool classof(const Info *I) { return I->IT == InfoType::IT_variable; }
+ static bool classof(const Info *I) {
+ return I->IT == InfoType::IT_variable;
+ }
void merge(VarInfo &&I);
@@ -678,7 +682,9 @@ struct FunctionInfo : public SymbolInfo {
FunctionInfo(SymbolID USR = SymbolID())
: SymbolInfo(InfoType::IT_function, USR) {}
- static bool classof(const Info *I) { return I->IT == InfoType::IT_function; }
+ static bool classof(const Info *I) {
+ return I->IT == InfoType::IT_function;
+ }
void merge(FunctionInfo &&I);
@@ -745,7 +751,9 @@ struct TypedefInfo : public SymbolInfo {
TypedefInfo(SymbolID USR = SymbolID())
: SymbolInfo(InfoType::IT_typedef, USR) {}
- static bool classof(const Info *I) { return I->IT == InfoType::IT_typedef; }
+ static bool classof(const Info *I) {
+ return I->IT == InfoType::IT_typedef;
+ }
void merge(TypedefInfo &&I);
@@ -833,7 +841,9 @@ struct ConceptInfo : public SymbolInfo {
ConceptInfo() : SymbolInfo(InfoType::IT_concept) {}
ConceptInfo(SymbolID USR) : SymbolInfo(InfoType::IT_concept, USR) {}
- static bool classof(const Info *I) { return I->IT == InfoType::IT_concept; }
+ static bool classof(const Info *I) {
+ return I->IT == InfoType::IT_concept;
+ }
void merge(ConceptInfo &&I);
More information about the llvm-branch-commits
mailing list