diff options
author | Prajwal Nadig <pnadig@apple.com> | 2025-06-30 15:55:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-30 08:55:35 -0600 |
commit | 53102a395f3bd07762c183c9ecd213a4759559dd (patch) | |
tree | d0c1c4e38a30a78652e0c2b93e3b6d49922db299 /clang/lib/ExtractAPI/DeclarationFragments.cpp | |
parent | 5ab3114bd12cdafc1e4e384e3a06c7258723ebde (diff) | |
download | llvm-53102a395f3bd07762c183c9ecd213a4759559dd.zip llvm-53102a395f3bd07762c183c9ecd213a4759559dd.tar.gz llvm-53102a395f3bd07762c183c9ecd213a4759559dd.tar.bz2 |
[ExtractAPI] Format pointer types correctly (#146182)
Pointer types in function signatures must place the asterisk before the
identifier without a space in between. This patch removes the space and
also ensures that pointers to pointers are formatted correctly.
rdar://131780418
rdar://154533037
Diffstat (limited to 'clang/lib/ExtractAPI/DeclarationFragments.cpp')
-rw-r--r-- | clang/lib/ExtractAPI/DeclarationFragments.cpp | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp index 348e758..5234932 100644 --- a/clang/lib/ExtractAPI/DeclarationFragments.cpp +++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp @@ -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 `*`. @@ -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(), @@ -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); |