357 changes: 48 additions & 309 deletions clang/test/ExtractAPI/objc_id_protocol.m
Original file line number Diff line number Diff line change
@@ -1,317 +1,56 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang -extract-api -x objective-c-header -target arm64-apple-macosx \
// RUN: %t/input.h -o %t/output.json | FileCheck -allow-empty %s
// RUN: %clang_cc1 -extract-api --pretty-sgf --emit-sgf-symbol-labels-for-testing \
// RUN: -x objective-c-header -triple arm64-apple-macosx %s -o - -verify | FileCheck %s

// Generator version is not consistent across test runs, normalize it.
// RUN: sed -e "s@\"generator\": \".*\"@\"generator\": \"?\"@g" \
// RUN: %t/output.json >> %t/output-normalized.json
// RUN: diff %t/reference.output.json %t/output-normalized.json

// CHECK-NOT: error:
// CHECK-NOT: warning:

//--- input.h
@protocol MyProtocol
@end

@interface MyInterface
@property(copy, readwrite) id<MyProtocol> obj1;
@property(readwrite) id<MyProtocol> *obj2;
// CHECK-LABEL: "!testLabel": "c:objc(cs)MyInterface(py)obj1"
// CHECK: "declarationFragments": [
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "keyword",
// CHECK-NEXT: "spelling": "@property"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "text",
// CHECK-NEXT: "spelling": " ("
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "keyword",
// CHECK-NEXT: "spelling": "copy"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "text",
// CHECK-NEXT: "spelling": ", "
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "keyword",
// CHECK-NEXT: "spelling": "readwrite"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "text",
// CHECK-NEXT: "spelling": ") "
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "typeIdentifier",
// CHECK-NEXT: "preciseIdentifier": "c:Qoobjc(pl)MyProtocol",
// CHECK-NEXT: "spelling": "id<MyProtocol>"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "text",
// CHECK-NEXT: "spelling": " "
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "identifier",
// CHECK-NEXT: "spelling": "obj1"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "text",
// CHECK-NEXT: "spelling": ";"
// CHECK-NEXT: }
// CHECK-NEXT: ],
@end
//--- reference.output.json.in
{
"metadata": {
"formatVersion": {
"major": 0,
"minor": 5,
"patch": 3
},
"generator": "?"
},
"module": {
"name": "",
"platform": {
"architecture": "arm64",
"operatingSystem": {
"minimumVersion": {
"major": 11,
"minor": 0,
"patch": 0
},
"name": "macosx"
},
"vendor": "apple"
}
},
"relationships": [
{
"kind": "memberOf",
"source": "c:objc(cs)MyInterface(py)obj1",
"target": "c:objc(cs)MyInterface",
"targetFallback": "MyInterface"
},
{
"kind": "memberOf",
"source": "c:objc(cs)MyInterface(py)obj2",
"target": "c:objc(cs)MyInterface",
"targetFallback": "MyInterface"
}
],
"symbols": [
{
"accessLevel": "public",
"declarationFragments": [
{
"kind": "keyword",
"spelling": "@interface"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "MyInterface"
}
],
"identifier": {
"interfaceLanguage": "objective-c",
"precise": "c:objc(cs)MyInterface"
},
"kind": {
"displayName": "Class",
"identifier": "objective-c.class"
},
"location": {
"position": {
"character": 11,
"line": 3
},
"uri": "file://INPUT_DIR/input.h"
},
"names": {
"navigator": [
{
"kind": "identifier",
"spelling": "MyInterface"
}
],
"subHeading": [
{
"kind": "identifier",
"spelling": "MyInterface"
}
],
"title": "MyInterface"
},
"pathComponents": [
"MyInterface"
]
},
{
"accessLevel": "public",
"declarationFragments": [
{
"kind": "keyword",
"spelling": "@property"
},
{
"kind": "text",
"spelling": " ("
},
{
"kind": "keyword",
"spelling": "copy"
},
{
"kind": "text",
"spelling": ", "
},
{
"kind": "keyword",
"spelling": "readwrite"
},
{
"kind": "text",
"spelling": ") "
},
{
"kind": "typeIdentifier",
"preciseIdentifier": "c:Qoobjc(pl)MyProtocol",
"spelling": "id<MyProtocol>"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "obj1"
},
{
"kind": "text",
"spelling": ";"
}
],
"identifier": {
"interfaceLanguage": "objective-c",
"precise": "c:objc(cs)MyInterface(py)obj1"
},
"kind": {
"displayName": "Instance Property",
"identifier": "objective-c.property"
},
"location": {
"position": {
"character": 42,
"line": 4
},
"uri": "file://INPUT_DIR/input.h"
},
"names": {
"navigator": [
{
"kind": "identifier",
"spelling": "obj1"
}
],
"subHeading": [
{
"kind": "identifier",
"spelling": "obj1"
}
],
"title": "obj1"
},
"pathComponents": [
"MyInterface",
"obj1"
]
},
{
"accessLevel": "public",
"declarationFragments": [
{
"kind": "keyword",
"spelling": "@property"
},
{
"kind": "text",
"spelling": " ("
},
{
"kind": "keyword",
"spelling": "readwrite"
},
{
"kind": "text",
"spelling": ") "
},
{
"kind": "typeIdentifier",
"preciseIdentifier": "c:Qoobjc(pl)MyProtocol",
"spelling": "id<MyProtocol>"
},
{
"kind": "text",
"spelling": " * "
},
{
"kind": "identifier",
"spelling": "obj2"
},
{
"kind": "text",
"spelling": ";"
}
],
"identifier": {
"interfaceLanguage": "objective-c",
"precise": "c:objc(cs)MyInterface(py)obj2"
},
"kind": {
"displayName": "Instance Property",
"identifier": "objective-c.property"
},
"location": {
"position": {
"character": 37,
"line": 5
},
"uri": "file://INPUT_DIR/input.h"
},
"names": {
"navigator": [
{
"kind": "identifier",
"spelling": "obj2"
}
],
"subHeading": [
{
"kind": "identifier",
"spelling": "obj2"
}
],
"title": "obj2"
},
"pathComponents": [
"MyInterface",
"obj2"
]
},
{
"accessLevel": "public",
"declarationFragments": [
{
"kind": "keyword",
"spelling": "@protocol"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "MyProtocol"
}
],
"identifier": {
"interfaceLanguage": "objective-c",
"precise": "c:objc(pl)MyProtocol"
},
"kind": {
"displayName": "Protocol",
"identifier": "objective-c.protocol"
},
"location": {
"position": {
"character": 10,
"line": 0
},
"uri": "file://INPUT_DIR/input.h"
},
"names": {
"navigator": [
{
"kind": "identifier",
"spelling": "MyProtocol"
}
],
"subHeading": [
{
"kind": "identifier",
"spelling": "MyProtocol"
}
],
"title": "MyProtocol"
},
"pathComponents": [
"MyProtocol"
]
}
]
}

// expected-no-diagnostics
4 changes: 2 additions & 2 deletions clang/test/ExtractAPI/objc_instancetype.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang_cc1 -extract-api -triple arm64-apple-macosx -x objective-c-header %t/input.h -o %t/output.json -verify
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang_cc1 -extract-api --pretty-sgf -triple arm64-apple-macosx -x objective-c-header %t/input.h -o %t/output.json -verify

// Generator version is not consistent across test runs, normalize it.
// RUN: sed -e "s@\"generator\": \".*\"@\"generator\": \"?\"@g" \
Expand Down
1,033 changes: 346 additions & 687 deletions clang/test/ExtractAPI/objc_interface.m

Large diffs are not rendered by default.

404 changes: 0 additions & 404 deletions clang/test/ExtractAPI/objc_module_category.m

This file was deleted.

600 changes: 9 additions & 591 deletions clang/test/ExtractAPI/objc_property.m

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/objc_protocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang -extract-api -x objective-c-header -target arm64-apple-macosx \
// RUN: %clang -extract-api --pretty-sgf -x objective-c-header -target arm64-apple-macosx \
// RUN: %t/input.h -o %t/output.json | FileCheck -allow-empty %s

// Generator version is not consistent across test runs, normalize it.
Expand Down
507 changes: 0 additions & 507 deletions clang/test/ExtractAPI/objc_various_categories.m

This file was deleted.

2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/operator_overload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang_cc1 -extract-api -triple arm64-apple-macosx \
// RUN: %clang_cc1 -extract-api --pretty-sgf -triple arm64-apple-macosx \
// RUN: -x c++-header %t/input.h -o %t/output.json -verify

// Generator version is not consistent across test runs, normalize it.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/relative_include.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// RUN: %hmaptool write %t/headermap.hmap.json %t/headermap.hmap

// Input headers use paths to the framework root/DSTROOT
// RUN: %clang_cc1 -extract-api -v --product-name=MyFramework \
// RUN: %clang_cc1 -extract-api --pretty-sgf -v --product-name=MyFramework \
// RUN: -triple arm64-apple-macosx \
// RUN: -iquote%t -I%t/headermap.hmap -F%t/Frameworks \
// RUN: -x objective-c-header \
Expand Down
2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/simple_inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang_cc1 -extract-api -triple arm64-apple-macosx \
// RUN: %clang_cc1 -extract-api --pretty-sgf -triple arm64-apple-macosx \
// RUN: -x c++-header %t/input.h -o %t/output.json -verify

// Generator version is not consistent across test runs, normalize it.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang -extract-api -target arm64-apple-macosx \
// RUN: %clang -extract-api --pretty-sgf -target arm64-apple-macosx \
// RUN: %t/input.h -o %t/output.json | FileCheck -allow-empty %s

// Generator version is not consistent across test runs, normalize it.
Expand Down
464 changes: 83 additions & 381 deletions clang/test/ExtractAPI/typedef.c

Large diffs are not rendered by default.

612 changes: 151 additions & 461 deletions clang/test/ExtractAPI/typedef_anonymous_record.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/typedef_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang -extract-api --product-name=TypedefChain -target arm64-apple-macosx \
// RUN: %clang -extract-api --pretty-sgf --product-name=TypedefChain -target arm64-apple-macosx \
// RUN: -x objective-c-header %t/input.h -o %t/output.json | FileCheck -allow-empty %s

// Generator version is not consistent across test runs, normalize it.
Expand Down
561 changes: 131 additions & 430 deletions clang/test/ExtractAPI/typedef_struct_enum.c

Large diffs are not rendered by default.

411 changes: 11 additions & 400 deletions clang/test/ExtractAPI/underscored.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions clang/test/ExtractAPI/union.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: split-file %s %t
// RUN: sed -e "s@INPUT_DIR@%{/t:regex_replacement}@g" \
// RUN: %t/reference.output.json.in >> %t/reference.output.json
// RUN: %clang_cc1 -extract-api -triple arm64-apple-macosx -x c-header\
// RUN: %clang_cc1 -extract-api --pretty-sgf -triple arm64-apple-macosx -x c-header\
// RUN: %t/input.h -o %t/output.json -verify

// Generator version is not consistent across test runs, normalize it.
Expand All @@ -12,7 +12,7 @@

//--- input.h
/// My Union
union Union{
union Union {
/// the a option
int a;
/// the b option
Expand Down
2 changes: 1 addition & 1 deletion clang/test/ExtractAPI/vfs_redirected_include.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// RUN: %t/vfsoverlay.yaml.in >> %t/vfsoverlay.yaml

// Input headers use paths to the framework root/DSTROOT
// RUN: %clang_cc1 -extract-api -v --product-name=MyFramework \
// RUN: %clang_cc1 -extract-api --pretty-sgf -v --product-name=MyFramework \
// RUN: -triple arm64-apple-macosx \
// RUN: -iquote%t -ivfsoverlay %t/vfsoverlay.yaml -F%t/Frameworks \
// RUN: -x objective-c-header \
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Index/extract-api-cursor.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ @implementation Derived
- (void)derivedMethodWithValue:(id<Protocol>)value {
int a = 5;
}
/// Impl only docs
- (void)implOnlyMethod { }
@end

// RUN: c-index-test -single-symbol-sgf-at=%s:4:9 local %s | FileCheck -check-prefix=CHECK-FOO %s
Expand Down Expand Up @@ -118,3 +120,10 @@ - (void)derivedMethodWithValue:(id<Protocol>)value {
// CHECK-DERIVED-METHOD-IMPL: "text":"Derived method docs"
// CHECK-DERIVED-METHOD-IMPL: "kind":{"displayName":"Instance Method","identifier":"objective-c.method"}
// CHECK-DERIVED-METHOD-IMPL: "title":"derivedMethodWithValue:"

// RUN: c-index-test -single-symbol-sgf-at=%s:35:11 local %s | FileCheck -check-prefix=CHECK-IMPL-ONLY %s
// CHECK-IMPL-ONLY: "relatedSymbols":[]
// CHECK-IMPL-ONLY: "relationships":[{"kind":"memberOf","source":"c:objc(cs)Derived(im)implOnlyMethod","target":"c:objc(cs)Derived"
// CHECK-IMPL-ONLY: "text":"Impl only docs"
// CHECK-IMPL-ONLY: "kind":{"displayName":"Instance Method","identifier":"objective-c.method"}
// CHECK-IMPL-ONLY: "title":"implOnlyMethod"
73 changes: 19 additions & 54 deletions clang/tools/libclang/CXExtractAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "clang-c/Index.h"
#include "clang-c/Platform.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/ExtractAPI/API.h"
Expand Down Expand Up @@ -54,63 +55,35 @@ struct LibClangExtractAPIVisitor
if (!shouldDeclBeIncluded(Decl))
return true;

const ObjCInterfaceDecl *Interface = Decl->getClassInterface();
StringRef Name = Interface->getName();
StringRef USR = API.recordUSR(Decl);
PresumedLoc Loc =
Context.getSourceManager().getPresumedLoc(Decl->getLocation());
LinkageInfo Linkage = Decl->getLinkageAndVisibility();
DocComment Comment;
if (auto *RawComment = fetchRawCommentForDecl(Interface))
Comment = RawComment->getFormattedLines(Context.getSourceManager(),
Context.getDiagnostics());

// Build declaration fragments and sub-heading by generating them for the
// interface.
DeclarationFragments Declaration =
DeclarationFragmentsBuilder::getFragmentsForObjCInterface(Interface);
DeclarationFragments SubHeading =
DeclarationFragmentsBuilder::getSubHeading(Decl);

// Collect super class information.
SymbolReference SuperClass;
if (const auto *SuperClassDecl = Decl->getSuperClass()) {
SuperClass.Name = SuperClassDecl->getObjCRuntimeNameAsString();
SuperClass.USR = API.recordUSR(SuperClassDecl);
}
auto *Interface = Decl->getClassInterface();

ObjCInterfaceRecord *ObjCInterfaceRecord = API.addObjCInterface(
Name, USR, Loc, AvailabilityInfo::createFromDecl(Decl), Linkage,
Comment, Declaration, SubHeading, SuperClass, isInSystemHeader(Decl));
if (!VisitObjCInterfaceDecl(Interface))
return false;

// Record all methods (selectors). This doesn't include automatically
// synthesized property methods.
recordObjCMethods(ObjCInterfaceRecord, Decl->methods());
recordObjCProperties(ObjCInterfaceRecord, Decl->properties());
recordObjCInstanceVariables(ObjCInterfaceRecord, Decl->ivars());
SmallString<128> USR;
index::generateUSRForDecl(Interface, USR);

if (auto *InterfaceRecord = dyn_cast_if_present<ObjCInterfaceRecord>(
API.findRecordForUSR(USR))) {
recordObjCMethods(InterfaceRecord, Decl->methods());
recordObjCProperties(InterfaceRecord, Decl->properties());
recordObjCInstanceVariables(InterfaceRecord, Decl->ivars());
}
return true;
}
};
} // namespace

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(APISet, CXAPISet)

static void WalkupFromMostDerivedType(LibClangExtractAPIVisitor &Visitor,
Decl *D);

template <typename DeclTy>
static bool WalkupParentContext(DeclContext *Parent,
LibClangExtractAPIVisitor &Visitor) {
if (auto *D = dyn_cast<DeclTy>(Parent)) {
WalkupFromMostDerivedType(Visitor, D);
return true;
}
return false;
}

// Visits the Decl D and it's transitive DeclContexts recursively, starting from
// the outer-most context. This is guaranteed to visit every Decl we need in the
// right order to generate symbol graph information for D.
static void WalkupFromMostDerivedType(LibClangExtractAPIVisitor &Visitor,
Decl *D) {
if (auto *Parent = D->getDeclContext())
WalkupFromMostDerivedType(Visitor, cast<Decl>(Parent));

switch (D->getKind()) {
#define ABSTRACT_DECL(DECL)
#define DECL(CLASS, BASE) \
Expand All @@ -119,20 +92,12 @@ static void WalkupFromMostDerivedType(LibClangExtractAPIVisitor &Visitor,
break;
#include "clang/AST/DeclNodes.inc"
}

for (auto *Parent = D->getDeclContext(); Parent != nullptr;
Parent = Parent->getParent()) {
if (WalkupParentContext<ObjCContainerDecl>(Parent, Visitor))
return;
if (WalkupParentContext<TagDecl>(Parent, Visitor))
return;
}
}

static CXString GenerateCXStringFromSymbolGraphData(llvm::json::Object Obj) {
llvm::SmallString<0> BackingString;
llvm::raw_svector_ostream OS(BackingString);
OS << Value(std::move(Obj));
OS << llvm::formatv("{0}", Value(std::move(Obj)));
return cxstring::createDup(BackingString.str());
}

Expand Down