Skip to content

[ExtractAPI] Cherry-pick multiple correctness fixes #10882

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

Open
wants to merge 4 commits into
base: swift/release/6.2
Choose a base branch
from
Open
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
43 changes: 29 additions & 14 deletions clang/lib/ExtractAPI/DeclarationFragments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,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 @@ -638,7 +643,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 @@ -713,18 +718,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 Expand Up @@ -890,6 +897,9 @@ DeclarationFragments DeclarationFragmentsBuilder::getFragmentsForCXXMethod(
if (Method->isVolatile())
Fragments.append("volatile", DeclarationFragments::FragmentKind::Keyword)
.appendSpace();
if (Method->isVirtual())
Fragments.append("virtual", DeclarationFragments::FragmentKind::Keyword)
.appendSpace();

// Build return type
DeclarationFragments After;
Expand Down Expand Up @@ -1615,10 +1625,13 @@ DeclarationFragmentsBuilder::getFunctionSignature(const ObjCMethodDecl *);
DeclarationFragments
DeclarationFragmentsBuilder::getSubHeading(const NamedDecl *Decl) {
DeclarationFragments Fragments;
if (isa<CXXConstructorDecl>(Decl) || isa<CXXDestructorDecl>(Decl))
if (isa<CXXConstructorDecl>(Decl)) {
Fragments.append(cast<CXXRecordDecl>(Decl->getDeclContext())->getName(),
DeclarationFragments::FragmentKind::Identifier);
else if (isa<CXXConversionDecl>(Decl)) {
} else if (isa<CXXDestructorDecl>(Decl)) {
Fragments.append(cast<CXXDestructorDecl>(Decl)->getNameAsString(),
DeclarationFragments::FragmentKind::Identifier);
} else if (isa<CXXConversionDecl>(Decl)) {
Fragments.append(
cast<CXXConversionDecl>(Decl)->getConversionType().getAsString(),
DeclarationFragments::FragmentKind::Identifier);
Expand All @@ -1632,9 +1645,11 @@ DeclarationFragmentsBuilder::getSubHeading(const NamedDecl *Decl) {
} else if (Decl->getIdentifier()) {
Fragments.append(Decl->getName(),
DeclarationFragments::FragmentKind::Identifier);
} else
} else {
Fragments.append(Decl->getDeclName().getAsString(),
DeclarationFragments::FragmentKind::Identifier);
}

return Fragments;
}

Expand Down
16 changes: 16 additions & 0 deletions clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ Object serializeNames(const APIRecord *Record) {
serializeArray(Names, "subHeading",
serializeDeclarationFragments(Record->SubHeading));
DeclarationFragments NavigatorFragments;
// The +/- prefix for Objective-C methods is important information, and
// should be included in the navigator fragment. The entire subheading is
// not included as it can contain too much information for other records.
switch (Record->getKind()) {
case APIRecord::RK_ObjCClassMethod:
NavigatorFragments.append("+ ", DeclarationFragments::FragmentKind::Text,
/*PreciseIdentifier*/ "");
break;
case APIRecord::RK_ObjCInstanceMethod:
NavigatorFragments.append("- ", DeclarationFragments::FragmentKind::Text,
/*PreciseIdentifier*/ "");
break;
default:
break;
}

NavigatorFragments.append(Record->Name,
DeclarationFragments::FragmentKind::Identifier,
/*PreciseIdentifier*/ "");
Expand Down
2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/constructor_destructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class Foo {
"subHeading": [
{
"kind": "identifier",
"spelling": "Foo"
"spelling": "~Foo"
}
],
"title": "~Foo"
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
10 changes: 9 additions & 1 deletion clang/test/ExtractAPI/methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ class Foo {
// GETCOUNT-NEXT: ]

// RUN: FileCheck %s --input-file %t/output.symbols.json --check-prefix SETL
void setLength(int length) noexcept;
virtual void setLength(int length) noexcept;
// SETL: "!testRelLabel": "memberOf $ c:@S@Foo@F@setLength#I# $ c:@S@Foo"
// SETL-LABEL: "!testLabel": "c:@S@Foo@F@setLength#I#"
// SETL: "declarationFragments": [
// SETL-NEXT: {
// SETL-NEXT: "kind": "keyword",
// SETL-NEXT: "spelling": "virtual"
// SETL-NEXT: },
// SETL-NEXT: {
// SETL-NEXT: "kind": "text",
// SETL-NEXT: "spelling": " "
// SETL-NEXT: },
// SETL-NEXT: {
// SETL-NEXT: "kind": "typeIdentifier",
// SETL-NEXT: "preciseIdentifier": "c:v",
// SETL-NEXT: "spelling": "void"
Expand Down
8 changes: 8 additions & 0 deletions clang/test/ExtractAPI/objc_instancetype.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ - (id) reset;
},
"names": {
"navigator": [
{
"kind": "text",
"spelling": "- "
},
{
"kind": "identifier",
"spelling": "init"
Expand Down Expand Up @@ -228,6 +232,10 @@ - (id) reset;
},
"names": {
"navigator": [
{
"kind": "text",
"spelling": "- "
},
{
"kind": "identifier",
"spelling": "reset"
Expand Down
Loading