clang 20.0.0git
API.cpp
Go to the documentation of this file.
1//===- ExtractAPI/API.cpp ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the APIRecord and derived record structs,
11/// and the APISet class.
12///
13//===----------------------------------------------------------------------===//
14
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/ErrorHandling.h"
20#include <memory>
21
22using namespace clang::extractapi;
23using namespace llvm;
24
26 : Name(R->Name), USR(R->USR), Record(R) {}
27
29 switch (Ctx->getKind()) {
30#define RECORD_CONTEXT(CLASS, KIND) \
31 case KIND: \
32 return static_cast<CLASS *>(const_cast<RecordContext *>(Ctx));
34 default:
35 return nullptr;
36 // llvm_unreachable("RecordContext derived class isn't propertly
37 // implemented");
38 }
39}
40
42 if (!Record)
43 return nullptr;
44 switch (Record->getKind()) {
45#define RECORD_CONTEXT(CLASS, KIND) \
46 case KIND: \
47 return static_cast<CLASS *>(const_cast<APIRecord *>(Record));
49 default:
50 return nullptr;
51 // llvm_unreachable("RecordContext derived class isn't propertly
52 // implemented");
53 }
54}
55
56bool RecordContext::IsWellFormed() const {
57 // Check that First and Last are both null or both non-null.
58 return (First == nullptr) == (Last == nullptr);
59}
60
62 assert(IsWellFormed());
63 // If we don't have an empty chain append Other's chain into ours.
64 if (First)
65 Last->NextInContext = Other.First;
66 else
67 First = Other.First;
68
69 Last = Other.Last;
70
71 // Delete Other's chain to ensure we don't accidentally traverse it.
72 Other.First = nullptr;
73 Other.Last = nullptr;
74}
75
77 assert(IsWellFormed());
78 if (!First) {
79 First = Record;
80 Last = Record;
81 return;
82 }
83
84 Last->NextInContext = Record;
85 Last = Record;
86}
87
88APIRecord *APISet::findRecordForUSR(StringRef USR) const {
89 if (USR.empty())
90 return nullptr;
91
92 auto FindIt = USRBasedLookupTable.find(USR);
93 if (FindIt != USRBasedLookupTable.end())
94 return FindIt->getSecond().get();
95
96 return nullptr;
97}
98
99StringRef APISet::copyString(StringRef String) {
100 if (String.empty())
101 return {};
102
103 // No need to allocate memory and copy if the string has already been stored.
104 if (Allocator.identifyObject(String.data()))
105 return String;
106
107 void *Ptr = Allocator.Allocate(String.size(), 1);
108 memcpy(Ptr, String.data(), String.size());
109 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
110}
111
112SymbolReference APISet::createSymbolReference(StringRef Name, StringRef USR,
113 StringRef Source) {
114 return SymbolReference(copyString(Name), copyString(USR), copyString(Source));
115}
116
121ObjCContainerRecord::~ObjCContainerRecord() {}
125
126void GlobalFunctionRecord::anchor() {}
127void GlobalVariableRecord::anchor() {}
128void EnumConstantRecord::anchor() {}
129void EnumRecord::anchor() {}
130void StructFieldRecord::anchor() {}
131void StructRecord::anchor() {}
132void UnionFieldRecord::anchor() {}
133void UnionRecord::anchor() {}
134void CXXFieldRecord::anchor() {}
135void CXXClassRecord::anchor() {}
136void CXXConstructorRecord::anchor() {}
137void CXXDestructorRecord::anchor() {}
138void CXXInstanceMethodRecord::anchor() {}
139void CXXStaticMethodRecord::anchor() {}
140void ObjCInstancePropertyRecord::anchor() {}
141void ObjCClassPropertyRecord::anchor() {}
142void ObjCInstanceVariableRecord::anchor() {}
143void ObjCInstanceMethodRecord::anchor() {}
144void ObjCClassMethodRecord::anchor() {}
145void ObjCCategoryRecord::anchor() {}
146void ObjCInterfaceRecord::anchor() {}
147void ObjCProtocolRecord::anchor() {}
148void MacroDefinitionRecord::anchor() {}
149void TypedefRecord::anchor() {}
This file defines the APIRecord-based structs and the APISet class.
llvm::MachO::Record Record
Definition: MachO.h:31
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
SymbolReference createSymbolReference(StringRef Name, StringRef USR, StringRef Source="")
Definition: API.cpp:112
StringRef copyString(StringRef String)
Copy String into the Allocator in this APISet.
Definition: API.cpp:99
APIRecord * findRecordForUSR(StringRef USR) const
Finds the APIRecord for a given USR.
Definition: API.cpp:88
Base class used for specific record types that have children records this is analogous to the DeclCon...
Definition: API.h:313
APIRecord::RecordKind getKind() const
Definition: API.h:331
void stealRecordChain(RecordContext &Other)
Append Other children chain into ours and empty out Other's record chain.
Definition: API.cpp:61
void addToRecordChain(APIRecord *) const
Definition: API.cpp:76
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
The base representation of an API record. Holds common symbol information.
Definition: API.h:192
virtual ~APIRecord()=0
Definition: API.cpp:117
static APIRecord * castFromRecordContext(const RecordContext *Ctx)
Definition: API.cpp:28
static RecordContext * castToRecordContext(const APIRecord *Record)
Definition: API.cpp:41
virtual ~TagRecord()=0
Definition: API.cpp:118