Skip to content

Commit d032fcf

Browse files
committed
[ExtractAPI] Format pointer types correctly (llvm#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 1e118a9 commit d032fcf

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
@@ -331,10 +331,15 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForType(
331331

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

339344
// For Objective-C `id` and `Class` pointers
340345
// we do not spell out the `*`.
@@ -638,7 +643,7 @@ DeclarationFragmentsBuilder::getFragmentsForParam(const ParmVarDecl *Param) {
638643
DeclarationFragments::FragmentKind::InternalParam);
639644
} else {
640645
Fragments.append(std::move(TypeFragments));
641-
if (!T->isBlockPointerType())
646+
if (!T->isAnyPointerType() && !T->isBlockPointerType())
642647
Fragments.appendSpace();
643648
Fragments
644649
.append(Param->getName(),
@@ -713,18 +718,20 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {
713718

714719
// FIXME: Is `after` actually needed here?
715720
DeclarationFragments After;
721+
QualType ReturnType = Func->getReturnType();
716722
auto ReturnValueFragment =
717-
getFragmentsForType(Func->getReturnType(), Func->getASTContext(), After);
723+
getFragmentsForType(ReturnType, Func->getASTContext(), After);
718724
if (StringRef(ReturnValueFragment.begin()->Spelling)
719725
.starts_with("type-parameter")) {
720-
std::string ProperArgName = Func->getReturnType().getAsString();
726+
std::string ProperArgName = ReturnType.getAsString();
721727
ReturnValueFragment.begin()->Spelling.swap(ProperArgName);
722728
}
723729

724-
Fragments.append(std::move(ReturnValueFragment))
725-
.appendSpace()
726-
.append(Func->getNameAsString(),
727-
DeclarationFragments::FragmentKind::Identifier);
730+
Fragments.append(std::move(ReturnValueFragment));
731+
if (!ReturnType->isAnyPointerType())
732+
Fragments.appendSpace();
733+
Fragments.append(Func->getNameAsString(),
734+
DeclarationFragments::FragmentKind::Identifier);
728735

729736
if (Func->getTemplateSpecializationInfo()) {
730737
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)