clangparser: add support for parents

Summary: Add support in ClangTypeParser for parents.

Reviewed By: JakeHillion

Differential Revision: D53708619
This commit is contained in:
Jonathan Haslam 2024-02-15 12:22:38 -08:00 committed by Facebook Community Bot
parent 1ba742b433
commit c367e7fa01
2 changed files with 29 additions and 1 deletions

View File

@ -17,6 +17,7 @@
#include <clang/AST/ASTContext.h>
#include <clang/AST/Decl.h>
#include <clang/AST/DeclCXX.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/AST/QualTypeNames.h>
#include <clang/AST/RecordLayout.h>
@ -214,7 +215,7 @@ Type& ClangTypeParser::enumerateClass(const clang::RecordType& ty) {
if (options_.mustProcessTemplateParams.contains(fqnWithoutTemplateParams))
enumerateClassTemplateParams(ty, c.templateParams);
// enumerateClassParents(ty, c.parents);
enumerateClassParents(ty, c.parents);
enumerateClassMembers(ty, c.members);
return c;
@ -312,6 +313,31 @@ std::optional<TemplateParam> ClangTypeParser::enumerateTemplateTemplateParam(
}
}
void ClangTypeParser::enumerateClassParents(const clang::RecordType& ty,
std::vector<Parent>& parents) {
assert(parents.empty());
auto* decl = ty.getDecl();
auto* cxxDecl = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
if (cxxDecl == nullptr)
return;
const auto& layout = decl->getASTContext().getASTRecordLayout(cxxDecl);
for (const auto& base : cxxDecl->bases()) {
auto baseType = base.getType();
if (baseType.isNull())
continue;
auto* baseCxxDecl = baseType->getAsCXXRecordDecl();
if (baseCxxDecl == nullptr)
continue;
auto offset = layout.getBaseClassOffset(baseCxxDecl).getQuantity();
auto& ptype = enumerateType(*baseType);
parents.emplace_back(Parent{ptype, static_cast<uint64_t>(offset)});
}
}
void ClangTypeParser::enumerateClassMembers(const clang::RecordType& ty,
std::vector<Member>& members) {
assert(members.empty());

View File

@ -50,6 +50,7 @@ class Array;
class Class;
class Enum;
class Member;
struct Parent;
class Primitive;
class Reference;
class Type;
@ -116,6 +117,7 @@ class ClangTypeParser {
std::optional<TemplateParam> enumerateTemplateTemplateParam(
const clang::TemplateName&);
void enumerateClassParents(const clang::RecordType&, std::vector<Parent>&);
void enumerateClassMembers(const clang::RecordType&, std::vector<Member>&);
ContainerInfo* getContainerInfo(const std::string& fqName) const;