header-checker: Re-group the code in abi_wrappers
Bug: 74764811
Test: ./tests/test.py
Change-Id: I86c126909f9ca14fb9af358e98a52448639283f0
diff --git a/vndk/tools/header-checker/src/dumper/abi_wrappers.cpp b/vndk/tools/header-checker/src/dumper/abi_wrappers.cpp
index 2f767f6..cd2ed70 100644
--- a/vndk/tools/header-checker/src/dumper/abi_wrappers.cpp
+++ b/vndk/tools/header-checker/src/dumper/abi_wrappers.cpp
@@ -31,6 +31,30 @@
namespace dumper {
+//------------------------------------------------------------------------------
+// Helper Function
+//------------------------------------------------------------------------------
+
+static repr::AccessSpecifierIR AccessClangToIR(
+ const clang::AccessSpecifier sp) {
+ switch (sp) {
+ case clang::AS_private: {
+ return repr::AccessSpecifierIR::PrivateAccess;
+ }
+ case clang::AS_protected: {
+ return repr::AccessSpecifierIR::ProtectedAccess;
+ }
+ default: {
+ return repr::AccessSpecifierIR::PublicAccess;
+ }
+ }
+}
+
+
+//------------------------------------------------------------------------------
+// ABI Wrapper
+//------------------------------------------------------------------------------
+
ABIWrapper::ABIWrapper(
clang::MangleContext *mangle_contextp,
clang::ASTContext *ast_contextp,
@@ -43,6 +67,21 @@
module_(module),
ast_caches_(ast_caches) {}
+std::string ABIWrapper::GetDeclSourceFile(const clang::Decl *decl,
+ const clang::CompilerInstance *cip) {
+ clang::SourceManager &sm = cip->getSourceManager();
+ clang::SourceLocation location = decl->getLocation();
+ // We need to use the expansion location to identify whether we should recurse
+ // into the AST Node or not. For eg: macros specifying LinkageSpecDecl can
+ // have their spelling location defined somewhere outside a source / header
+ // file belonging to a library. This should not allow the AST node to be
+ // skipped. Its expansion location will still be the source-file / header
+ // belonging to the library.
+ clang::SourceLocation expansion_location = sm.getExpansionLoc(location);
+ llvm::StringRef file_name = sm.getFilename(expansion_location);
+ return utils::RealPath(file_name.str());
+}
+
std::string ABIWrapper::GetCachedDeclSourceFile(
const clang::Decl *decl, const clang::CompilerInstance *cip) {
assert(decl != nullptr);
@@ -53,6 +92,55 @@
return result->second;
}
+std::string ABIWrapper::GetMangledNameDecl(
+ const clang::NamedDecl *decl, clang::MangleContext *mangle_contextp) {
+ if (!mangle_contextp->shouldMangleDeclName(decl)) {
+ clang::IdentifierInfo *identifier = decl->getIdentifier();
+ return identifier ? identifier->getName() : "";
+ }
+ std::string mangled_name;
+ llvm::raw_string_ostream ostream(mangled_name);
+ mangle_contextp->mangleName(decl, ostream);
+ ostream.flush();
+ return mangled_name;
+}
+
+bool ABIWrapper::SetupTemplateArguments(const clang::TemplateArgumentList *tl,
+ repr::TemplatedArtifactIR *ta,
+ const std::string &source_file) {
+ repr::TemplateInfoIR template_info;
+ for (int i = 0; i < tl->size(); i++) {
+ const clang::TemplateArgument &arg = (*tl)[i];
+ // TODO: More comprehensive checking needed.
+ if (arg.getKind() != clang::TemplateArgument::Type) {
+ continue;
+ }
+ clang::QualType type = arg.getAsType();
+ template_info.AddTemplateElement(
+ repr::TemplateElementIR(
+ ast_caches_->GetTypeId(GetKeyForTypeId(type))));
+ if (!CreateBasicNamedAndTypedDecl(type, source_file)) {
+ llvm::errs() << "Setting up template arguments failed\n";
+ return false;
+ }
+ }
+ ta->SetTemplateInfo(std::move(template_info));
+ return true;
+}
+
+bool ABIWrapper::SetupFunctionParameter(
+ repr::CFunctionLikeIR *functionp, const clang::QualType qual_type,
+ bool has_default_arg, const std::string &source_file, bool is_this_ptr) {
+ if (!CreateBasicNamedAndTypedDecl(qual_type, source_file)) {
+ llvm::errs() << "Setting up function parameter failed\n";
+ return false;
+ }
+ functionp->AddParameter(repr::ParamIR(
+ ast_caches_->GetTypeId(GetKeyForTypeId(qual_type)), has_default_arg,
+ is_this_ptr));
+ return true;
+}
+
static const clang::TagDecl *GetTagDecl(clang::QualType qual_type) {
const clang::Type *type_ptr = qual_type.getCanonicalType().getTypePtr();
assert(type_ptr != nullptr);
@@ -105,7 +193,25 @@
return is_array || is_ptr || is_reference || qual_type.hasLocalQualifiers();
}
-static clang::QualType GetReferencedType(const clang::QualType qual_type);
+// Get type 'referenced' by qual_type. Referenced type implies, in order:
+// 1) Strip off all qualifiers if qual_type has CVR qualifiers.
+// 2) Strip off a pointer level if qual_type is a pointer.
+// 3) Strip off the reference if qual_type is a reference.
+// Note: qual_type is expected to be a canonical type.
+static clang::QualType GetReferencedType(const clang::QualType qual_type) {
+ const clang::Type *type_ptr = qual_type.getTypePtr();
+ if (qual_type.hasLocalQualifiers()) {
+ return qual_type.getLocalUnqualifiedType();
+ }
+ if (type_ptr->isPointerType()) {
+ return type_ptr->getPointeeType();
+ }
+ if (type_ptr->isArrayType()) {
+ return
+ type_ptr->getArrayElementTypeNoTypeQual()->getCanonicalTypeInternal();
+ }
+ return qual_type.getNonReferenceType();
+}
static clang::QualType GetFinalReferencedType(clang::QualType qual_type) {
while (IsReferencingType(qual_type)) {
@@ -114,7 +220,8 @@
return qual_type;
}
-std::string ABIWrapper::TypeNameWithFinalDestination(clang::QualType qual_type) {
+std::string ABIWrapper::TypeNameWithFinalDestination(
+ clang::QualType qual_type) {
clang::QualType canonical_qual_type = qual_type.getCanonicalType();
const std::string qual_type_name = QualTypeToString(canonical_qual_type);
clang::QualType final_destination_type =
@@ -141,65 +248,12 @@
GetTypeUniqueId(GetTagDecl(final_destination_type));
}
-std::string ABIWrapper::GetDeclSourceFile(const clang::Decl *decl,
- const clang::CompilerInstance *cip) {
- clang::SourceManager &sm = cip->getSourceManager();
- clang::SourceLocation location = decl->getLocation();
- // We need to use the expansion location to identify whether we should recurse
- // into the AST Node or not. For eg: macros specifying LinkageSpecDecl can
- // have their spelling location defined somewhere outside a source / header
- // file belonging to a library. This should not allow the AST node to be
- // skipped. Its expansion location will still be the source-file / header
- // belonging to the library.
- clang::SourceLocation expansion_location = sm.getExpansionLoc(location);
- llvm::StringRef file_name = sm.getFilename(expansion_location);
- return utils::RealPath(file_name.str());
-}
-
-static repr::AccessSpecifierIR AccessClangToIR(
- const clang::AccessSpecifier sp) {
- switch (sp) {
- case clang::AS_private: {
- return repr::AccessSpecifierIR::PrivateAccess;
- break;
- }
- case clang::AS_protected: {
- return repr::AccessSpecifierIR::ProtectedAccess;
- break;
- }
- default: {
- return repr::AccessSpecifierIR::PublicAccess;
- break;
- }
- }
-}
-
bool ABIWrapper::CreateAnonymousRecord(const clang::RecordDecl *record_decl) {
RecordDeclWrapper record_decl_wrapper(mangle_contextp_, ast_contextp_, cip_,
record_decl, module_, ast_caches_);
return record_decl_wrapper.GetRecordDecl();
}
-// Get type 'referenced' by qual_type. Referenced type implies, in order:
-// 1) Strip off all qualifiers if qual_type has CVR qualifiers.
-// 2) Strip off a pointer level if qual_type is a pointer.
-// 3) Strip off the reference if qual_type is a reference.
-// Note: qual_type is expected to be a canonical type.
-static clang::QualType GetReferencedType(const clang::QualType qual_type) {
- const clang::Type *type_ptr = qual_type.getTypePtr();
- if (qual_type.hasLocalQualifiers()) {
- return qual_type.getLocalUnqualifiedType();
- }
- if (type_ptr->isPointerType()) {
- return type_ptr->getPointeeType();
- }
- if (type_ptr->isArrayType()) {
- return
- type_ptr->getArrayElementTypeNoTypeQual()->getCanonicalTypeInternal();
- }
- return qual_type.getNonReferenceType();
-}
-
bool ABIWrapper::CreateExtendedType(clang::QualType qual_type,
repr::TypeIR *typep) {
const clang::QualType canonical_type = qual_type.getCanonicalType();
@@ -207,50 +261,6 @@
return CreateBasicNamedAndTypedDecl(canonical_type, typep, "");
}
-// This overload takes in a qualtype and adds its information to the abi-dump on
-// its own.
-bool ABIWrapper::CreateBasicNamedAndTypedDecl(clang::QualType qual_type,
- const std::string &source_file) {
- const std::string &type_key = GetKeyForTypeId(qual_type);
- const clang::QualType canonical_type = qual_type.getCanonicalType();
- const clang::Type *base_type = canonical_type.getTypePtr();
- bool is_builtin = base_type->isBuiltinType();
- bool should_continue_with_recursive_type_creation =
- IsReferencingType(canonical_type) || is_builtin ||
- base_type->isFunctionType() ||
- (GetAnonymousRecord(canonical_type) != nullptr);
- if (!should_continue_with_recursive_type_creation ||
- !ast_caches_->type_cache_.insert(type_key).second) {
- return true;
- }
- // Do something similar to what is being done right now. Create an object
- // extending Type and return a pointer to that and pass it to CreateBasic...
- // CreateBasic...(qualtype, Type *) fills in size, alignemnt etc.
- auto type_and_status = SetTypeKind(canonical_type, source_file);
- std::unique_ptr<repr::TypeIR> typep = std::move(type_and_status.typep_);
- if (!base_type->isVoidType() && type_and_status.should_create_type_ &&
- !typep) {
- llvm::errs() << "nullptr with valid type while creating basic type\n";
- return false;
- }
- if (!type_and_status.should_create_type_) {
- return true;
- }
- return (CreateBasicNamedAndTypedDecl(
- canonical_type, typep.get(), source_file) &&
- module_->AddLinkableMessage(*typep));
-}
-
-std::string RecordDeclWrapper::GetMangledRTTI(
- const clang::CXXRecordDecl *cxx_record_decl) {
- clang::QualType qual_type =
- cxx_record_decl->getTypeForDecl()->getCanonicalTypeInternal();
- llvm::SmallString<256> uid;
- llvm::raw_svector_ostream out(uid);
- mangle_contextp_->mangleCXXRTTI(qual_type, out);
- return uid.str();
-}
-
std::string ABIWrapper::GetTypeUniqueId(const clang::TagDecl *tag_decl) {
if (!tag_decl) {
return "";
@@ -277,20 +287,22 @@
const clang::Type *base_type = canonical_type.getTypePtr();
assert(base_type != nullptr);
clang::Type::TypeClass type_class = base_type->getTypeClass();
- // Temporary hack for auto type sizes. Not determinable.
- if ((type_class != clang::Type::Auto) && !base_type->isIncompleteType() &&
- !(base_type->isDependentType())) {
+
+ // Set the size and alignment of the type.
+ // Temporary hack: Skip the auto types, incomplete types and dependent types.
+ if (type_class != clang::Type::Auto && !base_type->isIncompleteType() &&
+ !base_type->isDependentType()) {
std::pair<clang::CharUnits, clang::CharUnits> size_and_alignment =
- ast_contextp_->getTypeInfoInChars(canonical_type);
- size_t size = size_and_alignment.first.getQuantity();
- size_t alignment = size_and_alignment.second.getQuantity();
- typep->SetSize(size);
- typep->SetAlignment(alignment);
+ ast_contextp_->getTypeInfoInChars(canonical_type);
+ typep->SetSize(size_and_alignment.first.getQuantity());
+ typep->SetAlignment(size_and_alignment.second.getQuantity());
}
+
std::string type_name_with_destination =
TypeNameWithFinalDestination(canonical_type);
typep->SetName(type_name_with_destination);
typep->SetLinkerSetKey(type_name_with_destination);
+
// Default values are false, we don't set them since explicitly doing that
// makes the ABI dumps more verbose.
// This type has a reference type if its a pointer / reference OR it has CVR
@@ -298,11 +310,50 @@
clang::QualType referenced_type = GetReferencedType(canonical_type);
typep->SetReferencedType(
ast_caches_->GetTypeId(GetKeyForTypeId(referenced_type)));
+
typep->SetSelfType(ast_caches_->GetTypeId(GetKeyForTypeId(canonical_type)));
+
// Create the type for referenced type.
return CreateBasicNamedAndTypedDecl(referenced_type, source_file);
}
+// This overload takes in a qualtype and adds its information to the abi-dump on
+// its own.
+bool ABIWrapper::CreateBasicNamedAndTypedDecl(clang::QualType qual_type,
+ const std::string &source_file) {
+ const std::string &type_key = GetKeyForTypeId(qual_type);
+ const clang::QualType canonical_type = qual_type.getCanonicalType();
+ const clang::Type *base_type = canonical_type.getTypePtr();
+ bool is_builtin = base_type->isBuiltinType();
+ bool should_continue_with_recursive_type_creation =
+ IsReferencingType(canonical_type) || is_builtin ||
+ base_type->isFunctionType() ||
+ (GetAnonymousRecord(canonical_type) != nullptr);
+ if (!should_continue_with_recursive_type_creation ||
+ !ast_caches_->type_cache_.insert(type_key).second) {
+ return true;
+ }
+
+ // Do something similar to what is being done right now. Create an object
+ // extending Type and return a pointer to that and pass it to CreateBasic...
+ // CreateBasic...(qualtype, Type *) fills in size, alignemnt etc.
+ auto type_and_status = SetTypeKind(canonical_type, source_file);
+ std::unique_ptr<repr::TypeIR> typep = std::move(type_and_status.typep_);
+ if (!base_type->isVoidType() && type_and_status.should_create_type_ &&
+ !typep) {
+ llvm::errs() << "nullptr with valid type while creating basic type\n";
+ return false;
+ }
+
+ if (!type_and_status.should_create_type_) {
+ return true;
+ }
+
+ return (CreateBasicNamedAndTypedDecl(
+ canonical_type, typep.get(), source_file) &&
+ module_->AddLinkableMessage(*typep));
+}
+
// This method returns a TypeAndCreationStatus object. This object contains a
// type and information to tell the clients of this method whether the caller
// should continue creating the type.
@@ -373,19 +424,6 @@
return TypeAndCreationStatus(nullptr, false);
}
-std::string ABIWrapper::GetMangledNameDecl(
- const clang::NamedDecl *decl, clang::MangleContext *mangle_contextp) {
- if (!mangle_contextp->shouldMangleDeclName(decl)) {
- clang::IdentifierInfo *identifier = decl->getIdentifier();
- return identifier ? identifier->getName() : "";
- }
- std::string mangled_name;
- llvm::raw_string_ostream ostream(mangled_name);
- mangle_contextp->mangleName(decl, ostream);
- ostream.flush();
- return mangled_name;
-}
-
std::string ABIWrapper::GetTagDeclQualifiedName(const clang::TagDecl *decl) {
if (decl->getTypedefNameForAnonDecl()) {
return decl->getTypedefNameForAnonDecl()->getQualifiedNameAsString();
@@ -393,29 +431,6 @@
return decl->getQualifiedNameAsString();
}
-bool ABIWrapper::SetupTemplateArguments(const clang::TemplateArgumentList *tl,
- repr::TemplatedArtifactIR *ta,
- const std::string &source_file) {
- repr::TemplateInfoIR template_info;
- for (int i = 0; i < tl->size(); i++) {
- const clang::TemplateArgument &arg = (*tl)[i];
- // TODO: More comprehensive checking needed.
- if (arg.getKind() != clang::TemplateArgument::Type) {
- continue;
- }
- clang::QualType type = arg.getAsType();
- template_info.AddTemplateElement(
- repr::TemplateElementIR(
- ast_caches_->GetTypeId(GetKeyForTypeId(type))));
- if (!CreateBasicNamedAndTypedDecl(type, source_file)) {
- llvm::errs() << "Setting up template arguments failed\n";
- return false;
- }
- }
- ta->SetTemplateInfo(std::move(template_info));
- return true;
-}
-
std::string ABIWrapper::QualTypeToString(const clang::QualType &sweet_qt) {
const clang::QualType salty_qt = sweet_qt.getCanonicalType();
// clang::TypeName::getFullyQualifiedName removes the part of the type related
@@ -427,6 +442,11 @@
salty_qt, *ast_contextp_, ast_contextp_->getPrintingPolicy());
}
+
+//------------------------------------------------------------------------------
+// Function Type Wrapper
+//------------------------------------------------------------------------------
+
FunctionTypeWrapper::FunctionTypeWrapper(
clang::MangleContext *mangle_contextp, clang::ASTContext *ast_contextp,
const clang::CompilerInstance *compiler_instance_p,
@@ -469,6 +489,11 @@
module_->AddLinkableMessage(*abi_decl);
}
+
+//------------------------------------------------------------------------------
+// Function Decl Wrapper
+//------------------------------------------------------------------------------
+
FunctionDeclWrapper::FunctionDeclWrapper(
clang::MangleContext *mangle_contextp,
clang::ASTContext *ast_contextp,
@@ -492,19 +517,6 @@
return SetupFunctionParameter(functionp, this_type, false, source_file, true);
}
-bool ABIWrapper::SetupFunctionParameter(
- repr::CFunctionLikeIR *functionp, const clang::QualType qual_type,
- bool has_default_arg, const std::string &source_file, bool is_this_ptr) {
- if (!CreateBasicNamedAndTypedDecl(qual_type, source_file)) {
- llvm::errs() << "Setting up function parameter failed\n";
- return false;
- }
- functionp->AddParameter(repr::ParamIR(
- ast_caches_->GetTypeId(GetKeyForTypeId(qual_type)), has_default_arg,
- is_this_ptr));
- return true;
-}
-
bool FunctionDeclWrapper::SetupFunctionParameters(
repr::FunctionIR *functionp,
const std::string &source_file) {
@@ -574,6 +586,11 @@
return abi_decl;
}
+
+//------------------------------------------------------------------------------
+// Record Decl Wrapper
+//------------------------------------------------------------------------------
+
RecordDeclWrapper::RecordDeclWrapper(
clang::MangleContext *mangle_contextp,
clang::ASTContext *ast_contextp,
@@ -852,6 +869,21 @@
return module_->AddLinkableMessage(*abi_decl);
}
+std::string RecordDeclWrapper::GetMangledRTTI(
+ const clang::CXXRecordDecl *cxx_record_decl) {
+ clang::QualType qual_type =
+ cxx_record_decl->getTypeForDecl()->getCanonicalTypeInternal();
+ llvm::SmallString<256> uid;
+ llvm::raw_svector_ostream out(uid);
+ mangle_contextp_->mangleCXXRTTI(qual_type, out);
+ return uid.str();
+}
+
+
+//------------------------------------------------------------------------------
+// Enum Decl Wrapper
+//------------------------------------------------------------------------------
+
EnumDeclWrapper::EnumDeclWrapper(
clang::MangleContext *mangle_contextp,
clang::ASTContext *ast_contextp,
@@ -904,6 +936,11 @@
return module_->AddLinkableMessage(*abi_decl);
}
+
+//------------------------------------------------------------------------------
+// Global Decl Wrapper
+//------------------------------------------------------------------------------
+
GlobalVarDeclWrapper::GlobalVarDeclWrapper(
clang::MangleContext *mangle_contextp,
clang::ASTContext *ast_contextp,
diff --git a/vndk/tools/header-checker/src/dumper/abi_wrappers.h b/vndk/tools/header-checker/src/dumper/abi_wrappers.h
index 6d18446..7337bce 100644
--- a/vndk/tools/header-checker/src/dumper/abi_wrappers.h
+++ b/vndk/tools/header-checker/src/dumper/abi_wrappers.h
@@ -46,30 +46,36 @@
repr::ModuleIR *module,
ASTCaches *ast_caches);
+ public:
static std::string GetDeclSourceFile(const clang::Decl *decl,
const clang::CompilerInstance *cip);
- static std::string GetMangledNameDecl(const clang::NamedDecl *decl,
- clang::MangleContext *mangle_context);
-
protected:
std::string GetCachedDeclSourceFile(const clang::Decl *decl,
const clang::CompilerInstance *cip);
- std::string GetKeyForTypeId(clang::QualType qual_type);
+ public:
+ static std::string GetMangledNameDecl(const clang::NamedDecl *decl,
+ clang::MangleContext *mangle_context);
- std::string TypeNameWithFinalDestination(clang::QualType qual_type);
-
+ protected:
+ // Shared between FunctionDeclWrapper and RecordDeclWrapper.
bool SetupTemplateArguments(const clang::TemplateArgumentList *tl,
repr::TemplatedArtifactIR *ta,
const std::string &source_file);
+ protected:
+ // Shared between FunctionTypeWrapper and FunctionDeclWrapper.
bool SetupFunctionParameter(repr::CFunctionLikeIR *functionp,
const clang::QualType qual_type,
bool has_default_arg,
const std::string &source_file,
bool is_this_parameter = false);
+ protected:
+ // Type-related functions
+ std::string GetKeyForTypeId(clang::QualType qual_type);
+
std::string QualTypeToString(const clang::QualType &sweet_qt);
std::string GetTagDeclQualifiedName(const clang::TagDecl *decl);
@@ -84,12 +90,15 @@
bool CreateExtendedType(clang::QualType canonical_type,
repr::TypeIR *typep);
- bool CreateAnonymousRecord(const clang::RecordDecl *decl);
+ std::string GetTypeUniqueId(const clang::TagDecl *tag_decl);
+
+ private:
+ std::string TypeNameWithFinalDestination(clang::QualType qual_type);
TypeAndCreationStatus SetTypeKind(const clang::QualType qtype,
const std::string &source_file);
- std::string GetTypeUniqueId(const clang::TagDecl *tag_decl);
+ bool CreateAnonymousRecord(const clang::RecordDecl *decl);
protected:
const clang::CompilerInstance *cip_;