189f6b26fSZixu Wang //===- ExtractAPI/API.cpp ---------------------------------------*- C++ -*-===// 289f6b26fSZixu Wang // 389f6b26fSZixu Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 489f6b26fSZixu Wang // See https://llvm.org/LICENSE.txt for license information. 589f6b26fSZixu Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 689f6b26fSZixu Wang // 789f6b26fSZixu Wang //===----------------------------------------------------------------------===// 889f6b26fSZixu Wang /// 989f6b26fSZixu Wang /// \file 1089f6b26fSZixu Wang /// This file implements the APIRecord and derived record structs, 1189f6b26fSZixu Wang /// and the APISet class. 1289f6b26fSZixu Wang /// 1389f6b26fSZixu Wang //===----------------------------------------------------------------------===// 1489f6b26fSZixu Wang 1589f6b26fSZixu Wang #include "clang/ExtractAPI/API.h" 1689f6b26fSZixu Wang #include "clang/AST/CommentCommandTraits.h" 1789f6b26fSZixu Wang #include "clang/AST/CommentLexer.h" 1889f6b26fSZixu Wang #include "clang/AST/RawCommentList.h" 1989f6b26fSZixu Wang #include "clang/Index/USRGeneration.h" 2089f6b26fSZixu Wang #include "llvm/Support/Allocator.h" 2189f6b26fSZixu Wang 2289f6b26fSZixu Wang using namespace clang::extractapi; 2389f6b26fSZixu Wang using namespace llvm; 2489f6b26fSZixu Wang 2589f6b26fSZixu Wang GlobalRecord *APISet::addGlobal(GVKind Kind, StringRef Name, StringRef USR, 2689f6b26fSZixu Wang PresumedLoc Loc, 2789f6b26fSZixu Wang const AvailabilityInfo &Availability, 2889f6b26fSZixu Wang LinkageInfo Linkage, const DocComment &Comment, 2989f6b26fSZixu Wang DeclarationFragments Fragments, 3089f6b26fSZixu Wang DeclarationFragments SubHeading, 3189f6b26fSZixu Wang FunctionSignature Signature) { 3289f6b26fSZixu Wang auto Result = Globals.insert({Name, nullptr}); 3389f6b26fSZixu Wang if (Result.second) { 3489f6b26fSZixu Wang // Create the record if it does not already exist. 3589f6b26fSZixu Wang auto Record = APIRecordUniquePtr<GlobalRecord>(new (Allocator) GlobalRecord{ 3689f6b26fSZixu Wang Kind, Name, USR, Loc, Availability, Linkage, Comment, Fragments, 3789f6b26fSZixu Wang SubHeading, Signature}); 3889f6b26fSZixu Wang Result.first->second = std::move(Record); 3989f6b26fSZixu Wang } 4089f6b26fSZixu Wang return Result.first->second.get(); 4189f6b26fSZixu Wang } 4289f6b26fSZixu Wang 4389f6b26fSZixu Wang GlobalRecord * 4489f6b26fSZixu Wang APISet::addGlobalVar(StringRef Name, StringRef USR, PresumedLoc Loc, 4589f6b26fSZixu Wang const AvailabilityInfo &Availability, LinkageInfo Linkage, 4689f6b26fSZixu Wang const DocComment &Comment, DeclarationFragments Fragments, 4789f6b26fSZixu Wang DeclarationFragments SubHeading) { 4889f6b26fSZixu Wang return addGlobal(GVKind::Variable, Name, USR, Loc, Availability, Linkage, 4989f6b26fSZixu Wang Comment, Fragments, SubHeading, {}); 5089f6b26fSZixu Wang } 5189f6b26fSZixu Wang 5289f6b26fSZixu Wang GlobalRecord * 5389f6b26fSZixu Wang APISet::addFunction(StringRef Name, StringRef USR, PresumedLoc Loc, 5489f6b26fSZixu Wang const AvailabilityInfo &Availability, LinkageInfo Linkage, 5589f6b26fSZixu Wang const DocComment &Comment, DeclarationFragments Fragments, 5689f6b26fSZixu Wang DeclarationFragments SubHeading, 5789f6b26fSZixu Wang FunctionSignature Signature) { 5889f6b26fSZixu Wang return addGlobal(GVKind::Function, Name, USR, Loc, Availability, Linkage, 5989f6b26fSZixu Wang Comment, Fragments, SubHeading, Signature); 6089f6b26fSZixu Wang } 6189f6b26fSZixu Wang 6271b4c226SZixu Wang EnumConstantRecord *APISet::addEnumConstant( 6371b4c226SZixu Wang EnumRecord *Enum, StringRef Name, StringRef USR, PresumedLoc Loc, 6471b4c226SZixu Wang const AvailabilityInfo &Availability, const DocComment &Comment, 6571b4c226SZixu Wang DeclarationFragments Declaration, DeclarationFragments SubHeading) { 6671b4c226SZixu Wang auto Record = 6771b4c226SZixu Wang APIRecordUniquePtr<EnumConstantRecord>(new (Allocator) EnumConstantRecord{ 6871b4c226SZixu Wang Name, USR, Loc, Availability, Comment, Declaration, SubHeading}); 6971b4c226SZixu Wang return Enum->Constants.emplace_back(std::move(Record)).get(); 7071b4c226SZixu Wang } 7171b4c226SZixu Wang 7271b4c226SZixu Wang EnumRecord *APISet::addEnum(StringRef Name, StringRef USR, PresumedLoc Loc, 7371b4c226SZixu Wang const AvailabilityInfo &Availability, 7471b4c226SZixu Wang const DocComment &Comment, 7571b4c226SZixu Wang DeclarationFragments Declaration, 7671b4c226SZixu Wang DeclarationFragments SubHeading) { 7771b4c226SZixu Wang auto Result = Enums.insert({Name, nullptr}); 7871b4c226SZixu Wang if (Result.second) { 7971b4c226SZixu Wang // Create the record if it does not already exist. 8071b4c226SZixu Wang auto Record = APIRecordUniquePtr<EnumRecord>(new (Allocator) EnumRecord{ 8171b4c226SZixu Wang Name, USR, Loc, Availability, Comment, Declaration, SubHeading}); 8271b4c226SZixu Wang Result.first->second = std::move(Record); 8371b4c226SZixu Wang } 8471b4c226SZixu Wang return Result.first->second.get(); 8571b4c226SZixu Wang } 8671b4c226SZixu Wang 87*5bb5704cSZixu Wang StructFieldRecord *APISet::addStructField(StructRecord *Struct, StringRef Name, 88*5bb5704cSZixu Wang StringRef USR, PresumedLoc Loc, 89*5bb5704cSZixu Wang const AvailabilityInfo &Availability, 90*5bb5704cSZixu Wang const DocComment &Comment, 91*5bb5704cSZixu Wang DeclarationFragments Declaration, 92*5bb5704cSZixu Wang DeclarationFragments SubHeading) { 93*5bb5704cSZixu Wang auto Record = 94*5bb5704cSZixu Wang APIRecordUniquePtr<StructFieldRecord>(new (Allocator) StructFieldRecord{ 95*5bb5704cSZixu Wang Name, USR, Loc, Availability, Comment, Declaration, SubHeading}); 96*5bb5704cSZixu Wang return Struct->Fields.emplace_back(std::move(Record)).get(); 97*5bb5704cSZixu Wang } 98*5bb5704cSZixu Wang 99*5bb5704cSZixu Wang StructRecord *APISet::addStruct(StringRef Name, StringRef USR, PresumedLoc Loc, 100*5bb5704cSZixu Wang const AvailabilityInfo &Availability, 101*5bb5704cSZixu Wang const DocComment &Comment, 102*5bb5704cSZixu Wang DeclarationFragments Declaration, 103*5bb5704cSZixu Wang DeclarationFragments SubHeading) { 104*5bb5704cSZixu Wang auto Result = Structs.insert({Name, nullptr}); 105*5bb5704cSZixu Wang if (Result.second) { 106*5bb5704cSZixu Wang // Create the record if it does not already exist. 107*5bb5704cSZixu Wang auto Record = APIRecordUniquePtr<StructRecord>(new (Allocator) StructRecord{ 108*5bb5704cSZixu Wang Name, USR, Loc, Availability, Comment, Declaration, SubHeading}); 109*5bb5704cSZixu Wang Result.first->second = std::move(Record); 110*5bb5704cSZixu Wang } 111*5bb5704cSZixu Wang return Result.first->second.get(); 112*5bb5704cSZixu Wang } 113*5bb5704cSZixu Wang 11489f6b26fSZixu Wang StringRef APISet::recordUSR(const Decl *D) { 11589f6b26fSZixu Wang SmallString<128> USR; 11689f6b26fSZixu Wang index::generateUSRForDecl(D, USR); 11789f6b26fSZixu Wang return copyString(USR); 11889f6b26fSZixu Wang } 11989f6b26fSZixu Wang 12089f6b26fSZixu Wang StringRef APISet::copyString(StringRef String) { 12189f6b26fSZixu Wang if (String.empty()) 12289f6b26fSZixu Wang return {}; 12389f6b26fSZixu Wang 12489f6b26fSZixu Wang // No need to allocate memory and copy if the string has already been stored. 12589f6b26fSZixu Wang if (Allocator.identifyObject(String.data())) 12689f6b26fSZixu Wang return String; 12789f6b26fSZixu Wang 12889f6b26fSZixu Wang void *Ptr = Allocator.Allocate(String.size(), 1); 12989f6b26fSZixu Wang memcpy(Ptr, String.data(), String.size()); 13089f6b26fSZixu Wang return StringRef(reinterpret_cast<const char *>(Ptr), String.size()); 13189f6b26fSZixu Wang } 13289f6b26fSZixu Wang 13389f6b26fSZixu Wang APIRecord::~APIRecord() {} 13489f6b26fSZixu Wang 13589f6b26fSZixu Wang void GlobalRecord::anchor() {} 136