Skip to content

[ExtractAPI] Format pointer types correctly #146182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions clang/lib/ExtractAPI/DeclarationFragments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,15 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(

// Declaration fragments of a pointer type is the declaration fragments of
// the pointee type followed by a `*`,
if (T->isPointerType() && !T->isFunctionPointerType())
return Fragments
.append(getFragmentsForType(T->getPointeeType(), Context, After))
.append(" *", DeclarationFragments::FragmentKind::Text);
if (T->isPointerType() && !T->isFunctionPointerType()) {
QualType PointeeT = T->getPointeeType();
Fragments.append(getFragmentsForType(PointeeT, Context, After));
// If the pointee is itself a pointer, we do not want to insert a space
// before the `*` as the preceding character in the type name is a `*`.
if (!PointeeT->isAnyPointerType())
Fragments.appendSpace();
return Fragments.append("*", DeclarationFragments::FragmentKind::Text);
}

// For Objective-C `id` and `Class` pointers
// we do not spell out the `*`.
Expand Down Expand Up @@ -631,7 +636,7 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) {
DeclarationFragments::FragmentKind::InternalParam);
} else {
Fragments.append(std::move(TypeFragments));
if (!T->isBlockPointerType())
if (!T->isAnyPointerType() && !T->isBlockPointerType())
Fragments.appendSpace();
Fragments
.append(Param->getName(),
Expand Down Expand Up @@ -706,18 +711,20 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {

// FIXME: Is `after` actually needed here?
DeclarationFragments After;
QualType ReturnType = Func->getReturnType();
auto ReturnValueFragment =
getFragmentsForType(Func->getReturnType(), Func->getASTContext(), After);
getFragmentsForType(ReturnType, Func->getASTContext(), After);
if (StringRef(ReturnValueFragment.begin()->Spelling)
.starts_with("type-parameter")) {
std::string ProperArgName = Func->getReturnType().getAsString();
std::string ProperArgName = ReturnType.getAsString();
ReturnValueFragment.begin()->Spelling.swap(ProperArgName);
}

Fragments.append(std::move(ReturnValueFragment))
.appendSpace()
.append(Func->getNameAsString(),
DeclarationFragments::FragmentKind::Identifier);
Fragments.append(std::move(ReturnValueFragment));
if (!ReturnType->isAnyPointerType())
Fragments.appendSpace();
Fragments.append(Func->getNameAsString(),
DeclarationFragments::FragmentKind::Identifier);

if (Func->getTemplateSpecializationInfo()) {
Fragments.append("<", DeclarationFragments::FragmentKind::Text);
Expand Down
4 changes: 2 additions & 2 deletions clang/test/ExtractAPI/global_record.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ char unavailable __attribute__((unavailable));
},
{
"kind": "text",
"spelling": " * "
"spelling": " *"
},
{
"kind": "internalParam",
Expand Down Expand Up @@ -341,7 +341,7 @@ char unavailable __attribute__((unavailable));
},
{
"kind": "text",
"spelling": " * "
"spelling": " *"
},
{
"kind": "internalParam",
Expand Down
4 changes: 2 additions & 2 deletions clang/test/ExtractAPI/global_record_multifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ char unavailable __attribute__((unavailable));
},
{
"kind": "text",
"spelling": " * "
"spelling": " *"
},
{
"kind": "internalParam",
Expand Down Expand Up @@ -343,7 +343,7 @@ char unavailable __attribute__((unavailable));
},
{
"kind": "text",
"spelling": " * "
"spelling": " *"
},
{
"kind": "internalParam",
Expand Down
4 changes: 2 additions & 2 deletions clang/test/ExtractAPI/macro_undefined.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ FUNC_GEN(bar, const int *, unsigned);
},
{
"kind": "text",
"spelling": " * "
"spelling": " *"
},
{
"kind": "internalParam",
Expand Down Expand Up @@ -195,7 +195,7 @@ FUNC_GEN(bar, const int *, unsigned);
},
{
"kind": "text",
"spelling": " * "
"spelling": " *"
},
{
"kind": "internalParam",
Expand Down
Loading