@@ -23,6 +23,11 @@ class JSONGenerator : public Generator {
2323
2424const char *JSONGenerator::Format = " json" ;
2525
26+ static void serializeInfo (const TypedefInfo &I, json::Object &Obj,
27+ std::optional<StringRef> RepositoryUrl);
28+ static void serializeInfo (const EnumInfo &I, json::Object &Obj,
29+ std::optional<StringRef> RepositoryUrl);
30+
2631static json::Object serializeLocation (const Location &Loc,
2732 std::optional<StringRef> RepositoryUrl) {
2833 Object LocationObj = Object ();
@@ -103,7 +108,58 @@ static void serializeCommonAttributes(const Info &I, json::Object &Obj,
103108 }
104109}
105110
106- static void serializeTypeInfo (const TypeInfo &I, Object &Obj) {
111+ static void serializeReference (const Reference &Ref, Object &ReferenceObj,
112+ SmallString<64 > CurrentDirectory) {
113+ SmallString<64 > Path = Ref.getRelativeFilePath (CurrentDirectory);
114+ sys::path::append (Path, Ref.getFileBaseName () + " .json" );
115+ sys::path::native (Path, sys::path::Style::posix);
116+ ReferenceObj[" Link" ] = Path;
117+ ReferenceObj[" Name" ] = Ref.Name ;
118+ ReferenceObj[" QualName" ] = Ref.QualName ;
119+ ReferenceObj[" ID" ] = toHex (toStringRef (Ref.USR ));
120+ }
121+
122+ // Although namespaces and records both have ScopeChildren, they serialize them
123+ // differently. Only enums, records, and typedefs are handled here.
124+ static void serializeCommonChildren (const ScopeChildren &Children,
125+ json::Object &Obj,
126+ std::optional<StringRef> RepositoryUrl) {
127+ if (!Children.Enums .empty ()) {
128+ json::Value EnumsArray = Array ();
129+ auto &EnumsArrayRef = *EnumsArray.getAsArray ();
130+ for (const auto &Enum : Children.Enums ) {
131+ json::Object EnumObj;
132+ serializeInfo (Enum, EnumObj, RepositoryUrl);
133+ EnumsArrayRef.push_back (std::move (EnumObj));
134+ }
135+ Obj[" Enums" ] = std::move (EnumsArray);
136+ }
137+
138+ if (!Children.Typedefs .empty ()) {
139+ json::Value TypedefsArray = Array ();
140+ auto &TypedefsArrayRef = *TypedefsArray.getAsArray ();
141+ for (const auto &Typedef : Children.Typedefs ) {
142+ json::Object TypedefObj;
143+ serializeInfo (Typedef, TypedefObj, RepositoryUrl);
144+ TypedefsArrayRef.push_back (std::move (TypedefObj));
145+ }
146+ Obj[" Typedefs" ] = std::move (TypedefsArray);
147+ }
148+
149+ if (!Children.Records .empty ()) {
150+ json::Value RecordsArray = Array ();
151+ auto &RecordsArrayRef = *RecordsArray.getAsArray ();
152+ for (const auto &Record : Children.Records ) {
153+ json::Object RecordObj;
154+ SmallString<64 > BasePath = Record.getRelativeFilePath (" " );
155+ serializeReference (Record, RecordObj, BasePath);
156+ RecordsArrayRef.push_back (std::move (RecordObj));
157+ }
158+ Obj[" Records" ] = std::move (RecordsArray);
159+ }
160+ }
161+
162+ static void serializeInfo (const TypeInfo &I, Object &Obj) {
107163 Obj[" Name" ] = I.Type .Name ;
108164 Obj[" QualName" ] = I.Type .QualName ;
109165 Obj[" ID" ] = toHex (toStringRef (I.Type .USR ));
@@ -117,7 +173,7 @@ static void serializeInfo(const FunctionInfo &F, json::Object &Obj,
117173 Obj[" IsStatic" ] = F.IsStatic ;
118174
119175 auto ReturnTypeObj = Object ();
120- serializeTypeInfo (F.ReturnType , ReturnTypeObj);
176+ serializeInfo (F.ReturnType , ReturnTypeObj);
121177 Obj[" ReturnType" ] = std::move (ReturnTypeObj);
122178
123179 if (!F.Params .empty ()) {
@@ -168,7 +224,7 @@ static void serializeInfo(const TypedefInfo &I, json::Object &Obj,
168224 Obj[" TypeDeclaration" ] = I.TypeDeclaration ;
169225 Obj[" IsUsing" ] = I.IsUsing ;
170226 Object TypeObj = Object ();
171- serializeTypeInfo (I.Underlying , TypeObj);
227+ serializeInfo (I.Underlying , TypeObj);
172228 Obj[" Underlying" ] = std::move (TypeObj);
173229}
174230
@@ -224,27 +280,7 @@ static void serializeInfo(const RecordInfo &I, json::Object &Obj,
224280 Obj[" ProtectedMembers" ] = std::move (ProtectedMembers);
225281 }
226282
227- if (!I.Children .Enums .empty ()) {
228- json::Value EnumsArray = Array ();
229- auto &EnumsArrayRef = *EnumsArray.getAsArray ();
230- for (const auto &Enum : I.Children .Enums ) {
231- json::Object EnumObj;
232- serializeInfo (Enum, EnumObj, RepositoryUrl);
233- EnumsArrayRef.push_back (std::move (EnumObj));
234- }
235- Obj[" Enums" ] = std::move (EnumsArray);
236- }
237-
238- if (!I.Children .Typedefs .empty ()) {
239- json::Value TypedefsArray = Array ();
240- auto &TypedefsArrayRef = *TypedefsArray.getAsArray ();
241- for (const auto &Typedef : I.Children .Typedefs ) {
242- json::Object TypedefObj;
243- serializeInfo (Typedef, TypedefObj, RepositoryUrl);
244- TypedefsArrayRef.push_back (std::move (TypedefObj));
245- }
246- Obj[" Typedefs" ] = std::move (TypedefsArray);
247- }
283+ serializeCommonChildren (I.Children , Obj, RepositoryUrl);
248284}
249285
250286Error JSONGenerator::generateDocs (
0 commit comments