clang_generator: Collect function types directly

Do not try to peel off all layers of pointers/references when collecting
types, recursion works well here. Also collect the function type itself,
even if it is a pointer or if the function was the underlying type of a
typedef.

PiperOrigin-RevId: 490475937
Change-Id: I13cb3d9d3de7d25b9e627f43112c46758c7e6a22
This commit is contained in:
Christian Blichmann 2022-11-23 05:18:30 -08:00 committed by Copybara-Service
parent 3155cb0a67
commit 13c5b564b6
2 changed files with 23 additions and 7 deletions

View File

@ -105,6 +105,25 @@ TEST_F(EmitterTest, RelatedTypes) {
"typedef struct { int member; } MyStruct"));
}
TEST_F(EmitterTest, CollectFunctionPointer) {
EmitterForTesting emitter;
EXPECT_THAT(
RunFrontendAction(
R"(typedef void (callback_t)(void*);
struct HandlerData {
int member;
callback_t* cb;
};
extern "C" int Structize(HandlerData*);)",
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
IsOk());
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
EXPECT_THAT(
UglifyAll(emitter.SpellingsForNS("")),
ElementsAre("struct HandlerData { int member; callback_t *cb; }"));
}
TEST_F(EmitterTest, TypedefNames) {
EmitterForTesting emitter;
ASSERT_THAT(

View File

@ -61,22 +61,19 @@ void TypeCollector::CollectRelatedTypes(clang::QualType qual) {
qual->isMemberFunctionPointerType()) {
if (const auto* function_type = qual->getPointeeOrArrayElementType()
->getAs<clang::FunctionProtoType>()) {
// Note: Do not add the function type itself, as this will always be a
// pointer argument. We only need to collect all its related types.
// Collect the return type, the parameter types as well as the function
// pointer type itself.
CollectRelatedTypes(function_type->getReturnType());
for (const clang::QualType& param : function_type->getParamTypes()) {
CollectRelatedTypes(param);
}
collected_.insert(qual);
return;
}
}
if (IsPointerOrReference(qual)) {
clang::QualType pointee = qual;
do {
pointee = pointee->getPointeeType();
} while (IsPointerOrReference(pointee));
CollectRelatedTypes(pointee);
CollectRelatedTypes(qual->getPointeeType());
return;
}