Skip to content

Commit 53102a3

Browse files
authored
[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
1 parent 5ab3114 commit 53102a3

File tree

5 files changed

+412
-17
lines changed

5 files changed

+412
-17
lines changed

clang/lib/ExtractAPI/DeclarationFragments.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,15 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(
324324

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

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

707712
// FIXME: Is `after` actually needed here?
708713
DeclarationFragments After;
714+
QualType ReturnType = Func->getReturnType();
709715
auto ReturnValueFragment =
710-
getFragmentsForType(Func->getReturnType(), Func->getASTContext(), After);
716+
getFragmentsForType(ReturnType, Func->getASTContext(), After);
711717
if (StringRef(ReturnValueFragment.begin()->Spelling)
712718
.starts_with("type-parameter")) {
713-
std::string ProperArgName = Func->getReturnType().getAsString();
719+
std::string ProperArgName = ReturnType.getAsString();
714720
ReturnValueFragment.begin()->Spelling.swap(ProperArgName);
715721
}
716722

717-
Fragments.append(std::move(ReturnValueFragment))
718-
.appendSpace()
719-
.append(Func->getNameAsString(),
720-
DeclarationFragments::FragmentKind::Identifier);
723+
Fragments.append(std::move(ReturnValueFragment));
724+
if (!ReturnType->isAnyPointerType())
725+
Fragments.appendSpace();
726+
Fragments.append(Func->getNameAsString(),
727+
DeclarationFragments::FragmentKind::Identifier);
721728

722729
if (Func->getTemplateSpecializationInfo()) {
723730
Fragments.append("<", DeclarationFragments::FragmentKind::Text);

clang/test/ExtractAPI/global_record.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ char unavailable __attribute__((unavailable));
185185
},
186186
{
187187
"kind": "text",
188-
"spelling": " * "
188+
"spelling": " *"
189189
},
190190
{
191191
"kind": "internalParam",
@@ -341,7 +341,7 @@ char unavailable __attribute__((unavailable));
341341
},
342342
{
343343
"kind": "text",
344-
"spelling": " * "
344+
"spelling": " *"
345345
},
346346
{
347347
"kind": "internalParam",

clang/test/ExtractAPI/global_record_multifile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ char unavailable __attribute__((unavailable));
187187
},
188188
{
189189
"kind": "text",
190-
"spelling": " * "
190+
"spelling": " *"
191191
},
192192
{
193193
"kind": "internalParam",
@@ -343,7 +343,7 @@ char unavailable __attribute__((unavailable));
343343
},
344344
{
345345
"kind": "text",
346-
"spelling": " * "
346+
"spelling": " *"
347347
},
348348
{
349349
"kind": "internalParam",

clang/test/ExtractAPI/macro_undefined.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ FUNC_GEN(bar, const int *, unsigned);
148148
},
149149
{
150150
"kind": "text",
151-
"spelling": " * "
151+
"spelling": " *"
152152
},
153153
{
154154
"kind": "internalParam",
@@ -195,7 +195,7 @@ FUNC_GEN(bar, const int *, unsigned);
195195
},
196196
{
197197
"kind": "text",
198-
"spelling": " * "
198+
"spelling": " *"
199199
},
200200
{
201201
"kind": "internalParam",

0 commit comments

Comments
 (0)