aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/ExtractAPI/API.cpp
diff options
context:
space:
mode:
authorDaniel Grumberg <dgrumberg@apple.com>2022-10-26 18:23:37 +0100
committerDaniel Grumberg <dgrumberg@apple.com>2022-12-13 11:18:11 +0000
commit7a85192166b551929d413e8a38549375503371db (patch)
tree90233a2b9aa677bfd370d2a2c473c30839904529 /clang/lib/ExtractAPI/API.cpp
parent7a38c697ca863021a6906cecd96cec0c4c26a79d (diff)
downloadllvm-7a85192166b551929d413e8a38549375503371db.zip
llvm-7a85192166b551929d413e8a38549375503371db.tar.gz
llvm-7a85192166b551929d413e8a38549375503371db.tar.bz2
[clang][ExtractAPI] Add support for single symbol SGF and libclang support
This is mainly adding an entry point to `SymbolGraphSerializer` at `serializeSingleSymbolSGF` and exposing the necessary data to make this possible. Additionaly there are some changes to how symbol kinds and path components are serialized to make the usage more ergonomic in `serializeSingleSymbolSGF`. On the libclang side this introduces APIs to: - create an APISet from a TU - dispose of an APISet - query an APISet for a single symbol SGF for a given USR. - generate a single symbol SGF for a given CXCursor, this only traverses the necessary AST nodes to construct the result as oppposed as going through the entire AST. Differential Revision: https://reviews.llvm.org/D139115
Diffstat (limited to 'clang/lib/ExtractAPI/API.cpp')
-rw-r--r--clang/lib/ExtractAPI/API.cpp185
1 files changed, 123 insertions, 62 deletions
diff --git a/clang/lib/ExtractAPI/API.cpp b/clang/lib/ExtractAPI/API.cpp
index 8ab03a8..553b7bb 100644
--- a/clang/lib/ExtractAPI/API.cpp
+++ b/clang/lib/ExtractAPI/API.cpp
@@ -27,7 +27,8 @@ using namespace llvm;
namespace {
template <typename RecordTy, typename... CtorArgsTy>
-RecordTy *addTopLevelRecord(APISet::RecordMap<RecordTy> &RecordMap,
+RecordTy *addTopLevelRecord(DenseMap<StringRef, APIRecord *> &USRLookupTable,
+ APISet::RecordMap<RecordTy> &RecordMap,
StringRef USR, CtorArgsTy &&...CtorArgs) {
auto Result = RecordMap.insert({USR, nullptr});
@@ -36,7 +37,9 @@ RecordTy *addTopLevelRecord(APISet::RecordMap<RecordTy> &RecordMap,
Result.first->second =
std::make_unique<RecordTy>(USR, std::forward<CtorArgsTy>(CtorArgs)...);
- return Result.first->second.get();
+ auto *Record = Result.first->second.get();
+ USRLookupTable.insert({USR, Record});
+ return Record;
}
} // namespace
@@ -45,20 +48,22 @@ GlobalVariableRecord *
APISet::addGlobalVar(StringRef Name, StringRef USR, PresumedLoc Loc,
AvailabilitySet Availabilities, LinkageInfo Linkage,
const DocComment &Comment, DeclarationFragments Fragments,
- DeclarationFragments SubHeading) {
- return addTopLevelRecord(GlobalVariables, USR, Name, Loc,
+ DeclarationFragments SubHeading, bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, GlobalVariables, USR, Name, Loc,
std::move(Availabilities), Linkage, Comment,
- Fragments, SubHeading);
+ Fragments, SubHeading, IsFromSystemHeader);
}
GlobalFunctionRecord *APISet::addGlobalFunction(
StringRef Name, StringRef USR, PresumedLoc Loc,
AvailabilitySet Availabilities, LinkageInfo Linkage,
const DocComment &Comment, DeclarationFragments Fragments,
- DeclarationFragments SubHeading, FunctionSignature Signature) {
- return addTopLevelRecord(GlobalFunctions, USR, Name, Loc,
+ DeclarationFragments SubHeading, FunctionSignature Signature,
+ bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, GlobalFunctions, USR, Name, Loc,
std::move(Availabilities), Linkage, Comment,
- Fragments, SubHeading, Signature);
+ Fragments, SubHeading, Signature,
+ IsFromSystemHeader);
}
EnumConstantRecord *APISet::addEnumConstant(EnumRecord *Enum, StringRef Name,
@@ -66,10 +71,14 @@ EnumConstantRecord *APISet::addEnumConstant(EnumRecord *Enum, StringRef Name,
AvailabilitySet Availabilities,
const DocComment &Comment,
DeclarationFragments Declaration,
- DeclarationFragments SubHeading) {
+ DeclarationFragments SubHeading,
+ bool IsFromSystemHeader) {
auto Record = std::make_unique<EnumConstantRecord>(
USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
- SubHeading);
+ SubHeading, IsFromSystemHeader);
+ Record->ParentInformation = APIRecord::HierarchyInformation(
+ Enum->USR, Enum->Name, Enum->getKind(), Enum);
+ USRBasedLookupTable.insert({USR, Record.get()});
return Enum->Constants.emplace_back(std::move(Record)).get();
}
@@ -77,9 +86,11 @@ EnumRecord *APISet::addEnum(StringRef Name, StringRef USR, PresumedLoc Loc,
AvailabilitySet Availabilities,
const DocComment &Comment,
DeclarationFragments Declaration,
- DeclarationFragments SubHeading) {
- return addTopLevelRecord(Enums, USR, Name, Loc, std::move(Availabilities),
- Comment, Declaration, SubHeading);
+ DeclarationFragments SubHeading,
+ bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, Enums, USR, Name, Loc,
+ std::move(Availabilities), Comment, Declaration,
+ SubHeading, IsFromSystemHeader);
}
StructFieldRecord *APISet::addStructField(StructRecord *Struct, StringRef Name,
@@ -87,10 +98,14 @@ StructFieldRecord *APISet::addStructField(StructRecord *Struct, StringRef Name,
AvailabilitySet Availabilities,
const DocComment &Comment,
DeclarationFragments Declaration,
- DeclarationFragments SubHeading) {
+ DeclarationFragments SubHeading,
+ bool IsFromSystemHeader) {
auto Record = std::make_unique<StructFieldRecord>(
USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
- SubHeading);
+ SubHeading, IsFromSystemHeader);
+ Record->ParentInformation = APIRecord::HierarchyInformation(
+ Struct->USR, Struct->Name, Struct->getKind(), Struct);
+ USRBasedLookupTable.insert({USR, Record.get()});
return Struct->Fields.emplace_back(std::move(Record)).get();
}
@@ -98,22 +113,23 @@ StructRecord *APISet::addStruct(StringRef Name, StringRef USR, PresumedLoc Loc,
AvailabilitySet Availabilities,
const DocComment &Comment,
DeclarationFragments Declaration,
- DeclarationFragments SubHeading) {
- return addTopLevelRecord(Structs, USR, Name, Loc, std::move(Availabilities),
- Comment, Declaration, SubHeading);
+ DeclarationFragments SubHeading,
+ bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, Structs, USR, Name, Loc,
+ std::move(Availabilities), Comment, Declaration,
+ SubHeading, IsFromSystemHeader);
}
-ObjCCategoryRecord *APISet::addObjCCategory(StringRef Name, StringRef USR,
- PresumedLoc Loc,
- AvailabilitySet Availabilities,
- const DocComment &Comment,
- DeclarationFragments Declaration,
- DeclarationFragments SubHeading,
- SymbolReference Interface) {
+ObjCCategoryRecord *APISet::addObjCCategory(
+ StringRef Name, StringRef USR, PresumedLoc Loc,
+ AvailabilitySet Availabilities, const DocComment &Comment,
+ DeclarationFragments Declaration, DeclarationFragments SubHeading,
+ SymbolReference Interface, bool IsFromSystemHeader) {
// Create the category record.
- auto *Record = addTopLevelRecord(ObjCCategories, USR, Name, Loc,
- std::move(Availabilities), Comment,
- Declaration, SubHeading, Interface);
+ auto *Record =
+ addTopLevelRecord(USRBasedLookupTable, ObjCCategories, USR, Name, Loc,
+ std::move(Availabilities), Comment, Declaration,
+ SubHeading, Interface, IsFromSystemHeader);
// If this category is extending a known interface, associate it with the
// ObjCInterfaceRecord.
@@ -124,24 +140,38 @@ ObjCCategoryRecord *APISet::addObjCCategory(StringRef Name, StringRef USR,
return Record;
}
-ObjCInterfaceRecord *APISet::addObjCInterface(
- StringRef Name, StringRef USR, PresumedLoc Loc,
- AvailabilitySet Availabilities, LinkageInfo Linkage,
- const DocComment &Comment, DeclarationFragments Declaration,
- DeclarationFragments SubHeading, SymbolReference SuperClass) {
- return addTopLevelRecord(ObjCInterfaces, USR, Name, Loc,
+ObjCInterfaceRecord *
+APISet::addObjCInterface(StringRef Name, StringRef USR, PresumedLoc Loc,
+ AvailabilitySet Availabilities, LinkageInfo Linkage,
+ const DocComment &Comment,
+ DeclarationFragments Declaration,
+ DeclarationFragments SubHeading,
+ SymbolReference SuperClass, bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, ObjCInterfaces, USR, Name, Loc,
std::move(Availabilities), Linkage, Comment,
- Declaration, SubHeading, SuperClass);
+ Declaration, SubHeading, SuperClass,
+ IsFromSystemHeader);
}
ObjCMethodRecord *APISet::addObjCMethod(
ObjCContainerRecord *Container, StringRef Name, StringRef USR,
PresumedLoc Loc, AvailabilitySet Availabilities, const DocComment &Comment,
DeclarationFragments Declaration, DeclarationFragments SubHeading,
- FunctionSignature Signature, bool IsInstanceMethod) {
- auto Record = std::make_unique<ObjCMethodRecord>(
- USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
- SubHeading, Signature, IsInstanceMethod);
+ FunctionSignature Signature, bool IsInstanceMethod,
+ bool IsFromSystemHeader) {
+ std::unique_ptr<ObjCMethodRecord> Record;
+ if (IsInstanceMethod)
+ Record = std::make_unique<ObjCInstanceMethodRecord>(
+ USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
+ SubHeading, Signature, IsFromSystemHeader);
+ else
+ Record = std::make_unique<ObjCClassMethodRecord>(
+ USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
+ SubHeading, Signature, IsFromSystemHeader);
+
+ Record->ParentInformation = APIRecord::HierarchyInformation(
+ Container->USR, Container->Name, Container->getKind(), Container);
+ USRBasedLookupTable.insert({USR, Record.get()});
return Container->Methods.emplace_back(std::move(Record)).get();
}
@@ -150,10 +180,22 @@ ObjCPropertyRecord *APISet::addObjCProperty(
PresumedLoc Loc, AvailabilitySet Availabilities, const DocComment &Comment,
DeclarationFragments Declaration, DeclarationFragments SubHeading,
ObjCPropertyRecord::AttributeKind Attributes, StringRef GetterName,
- StringRef SetterName, bool IsOptional) {
- auto Record = std::make_unique<ObjCPropertyRecord>(
- USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
- SubHeading, Attributes, GetterName, SetterName, IsOptional);
+ StringRef SetterName, bool IsOptional, bool IsInstanceProperty,
+ bool IsFromSystemHeader) {
+ std::unique_ptr<ObjCPropertyRecord> Record;
+ if (IsInstanceProperty)
+ Record = std::make_unique<ObjCInstancePropertyRecord>(
+ USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
+ SubHeading, Attributes, GetterName, SetterName, IsOptional,
+ IsFromSystemHeader);
+ else
+ Record = std::make_unique<ObjCClassPropertyRecord>(
+ USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
+ SubHeading, Attributes, GetterName, SetterName, IsOptional,
+ IsFromSystemHeader);
+ Record->ParentInformation = APIRecord::HierarchyInformation(
+ Container->USR, Container->Name, Container->getKind(), Container);
+ USRBasedLookupTable.insert({USR, Record.get()});
return Container->Properties.emplace_back(std::move(Record)).get();
}
@@ -161,10 +203,13 @@ ObjCInstanceVariableRecord *APISet::addObjCInstanceVariable(
ObjCContainerRecord *Container, StringRef Name, StringRef USR,
PresumedLoc Loc, AvailabilitySet Availabilities, const DocComment &Comment,
DeclarationFragments Declaration, DeclarationFragments SubHeading,
- ObjCInstanceVariableRecord::AccessControl Access) {
+ ObjCInstanceVariableRecord::AccessControl Access, bool IsFromSystemHeader) {
auto Record = std::make_unique<ObjCInstanceVariableRecord>(
USR, Name, Loc, std::move(Availabilities), Comment, Declaration,
- SubHeading, Access);
+ SubHeading, Access, IsFromSystemHeader);
+ Record->ParentInformation = APIRecord::HierarchyInformation(
+ Container->USR, Container->Name, Container->getKind(), Container);
+ USRBasedLookupTable.insert({USR, Record.get()});
return Container->Ivars.emplace_back(std::move(Record)).get();
}
@@ -173,28 +218,41 @@ ObjCProtocolRecord *APISet::addObjCProtocol(StringRef Name, StringRef USR,
AvailabilitySet Availabilities,
const DocComment &Comment,
DeclarationFragments Declaration,
- DeclarationFragments SubHeading) {
- return addTopLevelRecord(ObjCProtocols, USR, Name, Loc,
+ DeclarationFragments SubHeading,
+ bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, ObjCProtocols, USR, Name, Loc,
std::move(Availabilities), Comment, Declaration,
- SubHeading);
+ SubHeading, IsFromSystemHeader);
}
MacroDefinitionRecord *
APISet::addMacroDefinition(StringRef Name, StringRef USR, PresumedLoc Loc,
DeclarationFragments Declaration,
- DeclarationFragments SubHeading) {
- return addTopLevelRecord(Macros, USR, Name, Loc, Declaration, SubHeading);
+ DeclarationFragments SubHeading,
+ bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, Macros, USR, Name, Loc,
+ Declaration, SubHeading, IsFromSystemHeader);
}
-TypedefRecord *APISet::addTypedef(StringRef Name, StringRef USR,
- PresumedLoc Loc,
- AvailabilitySet Availabilities,
- const DocComment &Comment,
- DeclarationFragments Declaration,
- DeclarationFragments SubHeading,
- SymbolReference UnderlyingType) {
- return addTopLevelRecord(Typedefs, USR, Name, Loc, std::move(Availabilities),
- Comment, Declaration, SubHeading, UnderlyingType);
+TypedefRecord *
+APISet::addTypedef(StringRef Name, StringRef USR, PresumedLoc Loc,
+ AvailabilitySet Availabilities, const DocComment &Comment,
+ DeclarationFragments Declaration,
+ DeclarationFragments SubHeading,
+ SymbolReference UnderlyingType, bool IsFromSystemHeader) {
+ return addTopLevelRecord(USRBasedLookupTable, Typedefs, USR, Name, Loc,
+ std::move(Availabilities), Comment, Declaration,
+ SubHeading, UnderlyingType, IsFromSystemHeader);
+}
+
+APIRecord *APISet::findRecordForUSR(StringRef USR) const {
+ if (USR.empty())
+ return nullptr;
+
+ auto It = USRBasedLookupTable.find(USR);
+ if (It != USRBasedLookupTable.end())
+ return It->second;
+ return nullptr;
}
StringRef APISet::recordUSR(const Decl *D) {
@@ -224,8 +282,9 @@ StringRef APISet::copyString(StringRef String) {
}
APIRecord::~APIRecord() {}
-
ObjCContainerRecord::~ObjCContainerRecord() {}
+ObjCMethodRecord::~ObjCMethodRecord() {}
+ObjCPropertyRecord::~ObjCPropertyRecord() {}
void GlobalFunctionRecord::anchor() {}
void GlobalVariableRecord::anchor() {}
@@ -233,9 +292,11 @@ void EnumConstantRecord::anchor() {}
void EnumRecord::anchor() {}
void StructFieldRecord::anchor() {}
void StructRecord::anchor() {}
-void ObjCPropertyRecord::anchor() {}
+void ObjCInstancePropertyRecord::anchor() {}
+void ObjCClassPropertyRecord::anchor() {}
void ObjCInstanceVariableRecord::anchor() {}
-void ObjCMethodRecord::anchor() {}
+void ObjCInstanceMethodRecord::anchor() {}
+void ObjCClassMethodRecord::anchor() {}
void ObjCCategoryRecord::anchor() {}
void ObjCInterfaceRecord::anchor() {}
void ObjCProtocolRecord::anchor() {}