xref: /llvm-project/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp (revision d8e9c5d9cab51f0ec21d4953014f41fe4dc603d9)
1 //===- ExtractAPI/Serialization/SymbolGraphSerializer.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 SymbolGraphSerializer.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/ExtractAPI/Serialization/SymbolGraphSerializer.h"
15 #include "clang/Basic/SourceLocation.h"
16 #include "clang/Basic/Version.h"
17 #include "clang/ExtractAPI/DeclarationFragments.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/STLFunctionalExtras.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/VersionTuple.h"
24 #include <optional>
25 #include <type_traits>
26 
27 using namespace clang;
28 using namespace clang::extractapi;
29 using namespace llvm;
30 using namespace llvm::json;
31 
32 namespace {
33 
34 /// Helper function to inject a JSON object \p Obj into another object \p Paren
35 /// at position \p Key.
36 void serializeObject(Object &Paren, StringRef Key, std::optional<Object> Obj) {
37   if (Obj)
38     Paren[Key] = std::move(*Obj);
39 }
40 
41 /// Helper function to inject a StringRef \p String into an object \p Paren at
42 /// position \p Key
43 void serializeString(Object &Paren, StringRef Key,
44                      std::optional<std::string> String) {
45   if (String)
46     Paren[Key] = std::move(*String);
47 }
48 
49 /// Helper function to inject a JSON array \p Array into object \p Paren at
50 /// position \p Key.
51 void serializeArray(Object &Paren, StringRef Key, std::optional<Array> Array) {
52   if (Array)
53     Paren[Key] = std::move(*Array);
54 }
55 
56 /// Serialize a \c VersionTuple \p V with the Symbol Graph semantic version
57 /// format.
58 ///
59 /// A semantic version object contains three numeric fields, representing the
60 /// \c major, \c minor, and \c patch parts of the version tuple.
61 /// For example version tuple 1.0.3 is serialized as:
62 /// \code
63 ///   {
64 ///     "major" : 1,
65 ///     "minor" : 0,
66 ///     "patch" : 3
67 ///   }
68 /// \endcode
69 ///
70 /// \returns \c std::nullopt if the version \p V is empty, or an \c Object
71 /// containing the semantic version representation of \p V.
72 std::optional<Object> serializeSemanticVersion(const VersionTuple &V) {
73   if (V.empty())
74     return std::nullopt;
75 
76   Object Version;
77   Version["major"] = V.getMajor();
78   Version["minor"] = V.getMinor().value_or(0);
79   Version["patch"] = V.getSubminor().value_or(0);
80   return Version;
81 }
82 
83 /// Serialize the OS information in the Symbol Graph platform property.
84 ///
85 /// The OS information in Symbol Graph contains the \c name of the OS, and an
86 /// optional \c minimumVersion semantic version field.
87 Object serializeOperatingSystem(const Triple &T) {
88   Object OS;
89   OS["name"] = T.getOSTypeName(T.getOS());
90   serializeObject(OS, "minimumVersion",
91                   serializeSemanticVersion(T.getMinimumSupportedOSVersion()));
92   return OS;
93 }
94 
95 /// Serialize the platform information in the Symbol Graph module section.
96 ///
97 /// The platform object describes a target platform triple in corresponding
98 /// three fields: \c architecture, \c vendor, and \c operatingSystem.
99 Object serializePlatform(const Triple &T) {
100   Object Platform;
101   Platform["architecture"] = T.getArchName();
102   Platform["vendor"] = T.getVendorName();
103   Platform["operatingSystem"] = serializeOperatingSystem(T);
104   return Platform;
105 }
106 
107 /// Serialize a source position.
108 Object serializeSourcePosition(const PresumedLoc &Loc) {
109   assert(Loc.isValid() && "invalid source position");
110 
111   Object SourcePosition;
112   SourcePosition["line"] = Loc.getLine();
113   SourcePosition["character"] = Loc.getColumn();
114 
115   return SourcePosition;
116 }
117 
118 /// Serialize a source location in file.
119 ///
120 /// \param Loc The presumed location to serialize.
121 /// \param IncludeFileURI If true, include the file path of \p Loc as a URI.
122 /// Defaults to false.
123 Object serializeSourceLocation(const PresumedLoc &Loc,
124                                bool IncludeFileURI = false) {
125   Object SourceLocation;
126   serializeObject(SourceLocation, "position", serializeSourcePosition(Loc));
127 
128   if (IncludeFileURI) {
129     std::string FileURI = "file://";
130     // Normalize file path to use forward slashes for the URI.
131     FileURI += sys::path::convert_to_slash(Loc.getFilename());
132     SourceLocation["uri"] = FileURI;
133   }
134 
135   return SourceLocation;
136 }
137 
138 /// Serialize a source range with begin and end locations.
139 Object serializeSourceRange(const PresumedLoc &BeginLoc,
140                             const PresumedLoc &EndLoc) {
141   Object SourceRange;
142   serializeObject(SourceRange, "start", serializeSourcePosition(BeginLoc));
143   serializeObject(SourceRange, "end", serializeSourcePosition(EndLoc));
144   return SourceRange;
145 }
146 
147 /// Serialize the availability attributes of a symbol.
148 ///
149 /// Availability information contains the introduced, deprecated, and obsoleted
150 /// versions of the symbol for a given domain (roughly corresponds to a
151 /// platform) as semantic versions, if not default.  Availability information
152 /// also contains flags to indicate if the symbol is unconditionally unavailable
153 /// or deprecated, i.e. \c __attribute__((unavailable)) and \c
154 /// __attribute__((deprecated)).
155 ///
156 /// \returns \c std::nullopt if the symbol has default availability attributes,
157 /// or an \c Array containing the formatted availability information.
158 std::optional<Array>
159 serializeAvailability(const AvailabilitySet &Availabilities) {
160   if (Availabilities.isDefault())
161     return std::nullopt;
162 
163   Array AvailabilityArray;
164 
165   if (Availabilities.isUnconditionallyDeprecated()) {
166     Object UnconditionallyDeprecated;
167     UnconditionallyDeprecated["domain"] = "*";
168     UnconditionallyDeprecated["isUnconditionallyDeprecated"] = true;
169     AvailabilityArray.emplace_back(std::move(UnconditionallyDeprecated));
170   }
171 
172   // Note unconditionally unavailable records are skipped.
173 
174   for (const auto &AvailInfo : Availabilities) {
175     Object Availability;
176     Availability["domain"] = AvailInfo.Domain;
177     if (AvailInfo.Unavailable)
178       Availability["isUnconditionallyUnavailable"] = true;
179     else {
180       serializeObject(Availability, "introducedVersion",
181                       serializeSemanticVersion(AvailInfo.Introduced));
182       serializeObject(Availability, "deprecatedVersion",
183                       serializeSemanticVersion(AvailInfo.Deprecated));
184       serializeObject(Availability, "obsoletedVersion",
185                       serializeSemanticVersion(AvailInfo.Obsoleted));
186     }
187     AvailabilityArray.emplace_back(std::move(Availability));
188   }
189 
190   return AvailabilityArray;
191 }
192 
193 /// Get the language name string for interface language references.
194 StringRef getLanguageName(Language Lang) {
195   switch (Lang) {
196   case Language::C:
197     return "c";
198   case Language::ObjC:
199     return "objective-c";
200   case Language::CXX:
201     return "c++";
202 
203   // Unsupported language currently
204   case Language::ObjCXX:
205   case Language::OpenCL:
206   case Language::OpenCLCXX:
207   case Language::CUDA:
208   case Language::RenderScript:
209   case Language::HIP:
210   case Language::HLSL:
211 
212   // Languages that the frontend cannot parse and compile
213   case Language::Unknown:
214   case Language::Asm:
215   case Language::LLVM_IR:
216     llvm_unreachable("Unsupported language kind");
217   }
218 
219   llvm_unreachable("Unhandled language kind");
220 }
221 
222 /// Serialize the identifier object as specified by the Symbol Graph format.
223 ///
224 /// The identifier property of a symbol contains the USR for precise and unique
225 /// references, and the interface language name.
226 Object serializeIdentifier(const APIRecord &Record, Language Lang) {
227   Object Identifier;
228   Identifier["precise"] = Record.USR;
229   Identifier["interfaceLanguage"] = getLanguageName(Lang);
230 
231   return Identifier;
232 }
233 
234 /// Serialize the documentation comments attached to a symbol, as specified by
235 /// the Symbol Graph format.
236 ///
237 /// The Symbol Graph \c docComment object contains an array of lines. Each line
238 /// represents one line of striped documentation comment, with source range
239 /// information.
240 /// e.g.
241 /// \code
242 ///   /// This is a documentation comment
243 ///       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'  First line.
244 ///   ///     with multiple lines.
245 ///       ^~~~~~~~~~~~~~~~~~~~~~~'         Second line.
246 /// \endcode
247 ///
248 /// \returns \c std::nullopt if \p Comment is empty, or an \c Object containing
249 /// the formatted lines.
250 std::optional<Object> serializeDocComment(const DocComment &Comment) {
251   if (Comment.empty())
252     return std::nullopt;
253 
254   Object DocComment;
255   Array LinesArray;
256   for (const auto &CommentLine : Comment) {
257     Object Line;
258     Line["text"] = CommentLine.Text;
259     serializeObject(Line, "range",
260                     serializeSourceRange(CommentLine.Begin, CommentLine.End));
261     LinesArray.emplace_back(std::move(Line));
262   }
263   serializeArray(DocComment, "lines", LinesArray);
264 
265   return DocComment;
266 }
267 
268 /// Serialize the declaration fragments of a symbol.
269 ///
270 /// The Symbol Graph declaration fragments is an array of tagged important
271 /// parts of a symbol's declaration. The fragments sequence can be joined to
272 /// form spans of declaration text, with attached information useful for
273 /// purposes like syntax-highlighting etc. For example:
274 /// \code
275 ///   const int pi; -> "declarationFragments" : [
276 ///                      {
277 ///                        "kind" : "keyword",
278 ///                        "spelling" : "const"
279 ///                      },
280 ///                      {
281 ///                        "kind" : "text",
282 ///                        "spelling" : " "
283 ///                      },
284 ///                      {
285 ///                        "kind" : "typeIdentifier",
286 ///                        "preciseIdentifier" : "c:I",
287 ///                        "spelling" : "int"
288 ///                      },
289 ///                      {
290 ///                        "kind" : "text",
291 ///                        "spelling" : " "
292 ///                      },
293 ///                      {
294 ///                        "kind" : "identifier",
295 ///                        "spelling" : "pi"
296 ///                      }
297 ///                    ]
298 /// \endcode
299 ///
300 /// \returns \c std::nullopt if \p DF is empty, or an \c Array containing the
301 /// formatted declaration fragments array.
302 std::optional<Array>
303 serializeDeclarationFragments(const DeclarationFragments &DF) {
304   if (DF.getFragments().empty())
305     return std::nullopt;
306 
307   Array Fragments;
308   for (const auto &F : DF.getFragments()) {
309     Object Fragment;
310     Fragment["spelling"] = F.Spelling;
311     Fragment["kind"] = DeclarationFragments::getFragmentKindString(F.Kind);
312     if (!F.PreciseIdentifier.empty())
313       Fragment["preciseIdentifier"] = F.PreciseIdentifier;
314     Fragments.emplace_back(std::move(Fragment));
315   }
316 
317   return Fragments;
318 }
319 
320 /// Serialize the \c names field of a symbol as specified by the Symbol Graph
321 /// format.
322 ///
323 /// The Symbol Graph names field contains multiple representations of a symbol
324 /// that can be used for different applications:
325 ///   - \c title : The simple declared name of the symbol;
326 ///   - \c subHeading : An array of declaration fragments that provides tags,
327 ///     and potentially more tokens (for example the \c +/- symbol for
328 ///     Objective-C methods). Can be used as sub-headings for documentation.
329 Object serializeNames(const APIRecord &Record) {
330   Object Names;
331   if (auto *CategoryRecord =
332           dyn_cast_or_null<const ObjCCategoryRecord>(&Record))
333     Names["title"] =
334         (CategoryRecord->Interface.Name + " (" + Record.Name + ")").str();
335   else
336     Names["title"] = Record.Name;
337 
338   serializeArray(Names, "subHeading",
339                  serializeDeclarationFragments(Record.SubHeading));
340   DeclarationFragments NavigatorFragments;
341   NavigatorFragments.append(Record.Name,
342                             DeclarationFragments::FragmentKind::Identifier,
343                             /*PreciseIdentifier*/ "");
344   serializeArray(Names, "navigator",
345                  serializeDeclarationFragments(NavigatorFragments));
346 
347   return Names;
348 }
349 
350 Object serializeSymbolKind(APIRecord::RecordKind RK, Language Lang) {
351   auto AddLangPrefix = [&Lang](StringRef S) -> std::string {
352     return (getLanguageName(Lang) + "." + S).str();
353   };
354 
355   Object Kind;
356   switch (RK) {
357   case APIRecord::RK_Unknown:
358     llvm_unreachable("Records should have an explicit kind");
359     break;
360   case APIRecord::RK_GlobalFunction:
361     Kind["identifier"] = AddLangPrefix("func");
362     Kind["displayName"] = "Function";
363     break;
364   case APIRecord::RK_GlobalFunctionTemplate:
365     Kind["identifier"] = AddLangPrefix("func");
366     Kind["displayName"] = "Function Template";
367     break;
368   case APIRecord::RK_GlobalFunctionTemplateSpecialization:
369     Kind["identifier"] = AddLangPrefix("func");
370     Kind["displayName"] = "Function Template Specialization";
371     break;
372   case APIRecord::RK_GlobalVariableTemplate:
373     Kind["identifier"] = AddLangPrefix("var");
374     Kind["displayName"] = "Global Variable Template";
375     break;
376   case APIRecord::RK_GlobalVariableTemplateSpecialization:
377     Kind["identifier"] = AddLangPrefix("var");
378     Kind["displayName"] = "Global Variable Template Specialization";
379     break;
380   case APIRecord::RK_GlobalVariableTemplatePartialSpecialization:
381     Kind["identifier"] = AddLangPrefix("var");
382     Kind["displayName"] = "Global Variable Template Partial Specialization";
383     break;
384   case APIRecord::RK_GlobalVariable:
385     Kind["identifier"] = AddLangPrefix("var");
386     Kind["displayName"] = "Global Variable";
387     break;
388   case APIRecord::RK_EnumConstant:
389     Kind["identifier"] = AddLangPrefix("enum.case");
390     Kind["displayName"] = "Enumeration Case";
391     break;
392   case APIRecord::RK_Enum:
393     Kind["identifier"] = AddLangPrefix("enum");
394     Kind["displayName"] = "Enumeration";
395     break;
396   case APIRecord::RK_StructField:
397     Kind["identifier"] = AddLangPrefix("property");
398     Kind["displayName"] = "Instance Property";
399     break;
400   case APIRecord::RK_Struct:
401     Kind["identifier"] = AddLangPrefix("struct");
402     Kind["displayName"] = "Structure";
403     break;
404   case APIRecord::RK_CXXField:
405     Kind["identifier"] = AddLangPrefix("property");
406     Kind["displayName"] = "Instance Property";
407     break;
408   case APIRecord::RK_Union:
409     Kind["identifier"] = AddLangPrefix("union");
410     Kind["displayName"] = "Union";
411     break;
412   case APIRecord::RK_StaticField:
413     Kind["identifier"] = AddLangPrefix("type.property");
414     Kind["displayName"] = "Type Property";
415     break;
416   case APIRecord::RK_ClassTemplate:
417   case APIRecord::RK_ClassTemplateSpecialization:
418   case APIRecord::RK_ClassTemplatePartialSpecialization:
419   case APIRecord::RK_CXXClass:
420     Kind["identifier"] = AddLangPrefix("class");
421     Kind["displayName"] = "Class";
422     break;
423   case APIRecord::RK_CXXMethodTemplate:
424     Kind["identifier"] = AddLangPrefix("method");
425     Kind["displayName"] = "Method Template";
426     break;
427   case APIRecord::RK_CXXMethodTemplateSpecialization:
428     Kind["identifier"] = AddLangPrefix("method");
429     Kind["displayName"] = "Method Template Specialization";
430     break;
431   case APIRecord::RK_Concept:
432     Kind["identifier"] = AddLangPrefix("concept");
433     Kind["displayName"] = "Concept";
434     break;
435   case APIRecord::RK_CXXStaticMethod:
436     Kind["identifier"] = AddLangPrefix("type.method");
437     Kind["displayName"] = "Static Method";
438     break;
439   case APIRecord::RK_CXXInstanceMethod:
440     Kind["identifier"] = AddLangPrefix("method");
441     Kind["displayName"] = "Instance Method";
442     break;
443   case APIRecord::RK_CXXConstructorMethod:
444     Kind["identifier"] = AddLangPrefix("method");
445     Kind["displayName"] = "Constructor";
446     break;
447   case APIRecord::RK_CXXDestructorMethod:
448     Kind["identifier"] = AddLangPrefix("method");
449     Kind["displayName"] = "Destructor";
450     break;
451   case APIRecord::RK_ObjCIvar:
452     Kind["identifier"] = AddLangPrefix("ivar");
453     Kind["displayName"] = "Instance Variable";
454     break;
455   case APIRecord::RK_ObjCInstanceMethod:
456     Kind["identifier"] = AddLangPrefix("method");
457     Kind["displayName"] = "Instance Method";
458     break;
459   case APIRecord::RK_ObjCClassMethod:
460     Kind["identifier"] = AddLangPrefix("type.method");
461     Kind["displayName"] = "Type Method";
462     break;
463   case APIRecord::RK_ObjCInstanceProperty:
464     Kind["identifier"] = AddLangPrefix("property");
465     Kind["displayName"] = "Instance Property";
466     break;
467   case APIRecord::RK_ObjCClassProperty:
468     Kind["identifier"] = AddLangPrefix("type.property");
469     Kind["displayName"] = "Type Property";
470     break;
471   case APIRecord::RK_ObjCInterface:
472     Kind["identifier"] = AddLangPrefix("class");
473     Kind["displayName"] = "Class";
474     break;
475   case APIRecord::RK_ObjCCategory:
476     Kind["identifier"] = AddLangPrefix("class.extension");
477     Kind["displayName"] = "Class Extension";
478     break;
479   case APIRecord::RK_ObjCCategoryModule:
480     Kind["identifier"] = AddLangPrefix("module.extension");
481     Kind["displayName"] = "Module Extension";
482     break;
483   case APIRecord::RK_ObjCProtocol:
484     Kind["identifier"] = AddLangPrefix("protocol");
485     Kind["displayName"] = "Protocol";
486     break;
487   case APIRecord::RK_MacroDefinition:
488     Kind["identifier"] = AddLangPrefix("macro");
489     Kind["displayName"] = "Macro";
490     break;
491   case APIRecord::RK_Typedef:
492     Kind["identifier"] = AddLangPrefix("typealias");
493     Kind["displayName"] = "Type Alias";
494     break;
495   }
496 
497   return Kind;
498 }
499 
500 /// Serialize the symbol kind information.
501 ///
502 /// The Symbol Graph symbol kind property contains a shorthand \c identifier
503 /// which is prefixed by the source language name, useful for tooling to parse
504 /// the kind, and a \c displayName for rendering human-readable names.
505 Object serializeSymbolKind(const APIRecord &Record, Language Lang) {
506   return serializeSymbolKind(Record.getKind(), Lang);
507 }
508 
509 template <typename RecordTy>
510 std::optional<Object>
511 serializeFunctionSignatureMixinImpl(const RecordTy &Record, std::true_type) {
512   const auto &FS = Record.Signature;
513   if (FS.empty())
514     return std::nullopt;
515 
516   Object Signature;
517   serializeArray(Signature, "returns",
518                  serializeDeclarationFragments(FS.getReturnType()));
519 
520   Array Parameters;
521   for (const auto &P : FS.getParameters()) {
522     Object Parameter;
523     Parameter["name"] = P.Name;
524     serializeArray(Parameter, "declarationFragments",
525                    serializeDeclarationFragments(P.Fragments));
526     Parameters.emplace_back(std::move(Parameter));
527   }
528 
529   if (!Parameters.empty())
530     Signature["parameters"] = std::move(Parameters);
531 
532   return Signature;
533 }
534 
535 template <typename RecordTy>
536 std::optional<Object>
537 serializeFunctionSignatureMixinImpl(const RecordTy &Record, std::false_type) {
538   return std::nullopt;
539 }
540 
541 /// Serialize the function signature field, as specified by the
542 /// Symbol Graph format.
543 ///
544 /// The Symbol Graph function signature property contains two arrays.
545 ///   - The \c returns array is the declaration fragments of the return type;
546 ///   - The \c parameters array contains names and declaration fragments of the
547 ///     parameters.
548 ///
549 /// \returns \c std::nullopt if \p FS is empty, or an \c Object containing the
550 /// formatted function signature.
551 template <typename RecordTy>
552 void serializeFunctionSignatureMixin(Object &Paren, const RecordTy &Record) {
553   serializeObject(Paren, "functionSignature",
554                   serializeFunctionSignatureMixinImpl(
555                       Record, has_function_signature<RecordTy>()));
556 }
557 
558 template <typename RecordTy>
559 std::optional<std::string> serializeAccessMixinImpl(const RecordTy &Record,
560                                                     std::true_type) {
561   const auto &AccessControl = Record.Access;
562   std::string Access;
563   if (AccessControl.empty())
564     return std::nullopt;
565   Access = AccessControl.getAccess();
566   return Access;
567 }
568 
569 template <typename RecordTy>
570 std::optional<std::string> serializeAccessMixinImpl(const RecordTy &Record,
571                                                     std::false_type) {
572   return std::nullopt;
573 }
574 
575 template <typename RecordTy>
576 void serializeAccessMixin(Object &Paren, const RecordTy &Record) {
577   auto accessLevel = serializeAccessMixinImpl(Record, has_access<RecordTy>());
578   if (!accessLevel.has_value())
579     accessLevel = "public";
580   serializeString(Paren, "accessLevel", accessLevel);
581 }
582 
583 template <typename RecordTy>
584 std::optional<Object> serializeTemplateMixinImpl(const RecordTy &Record,
585                                                  std::true_type) {
586   const auto &Template = Record.Templ;
587   if (Template.empty())
588     return std::nullopt;
589 
590   Object Generics;
591   Array GenericParameters;
592   for (const auto Param : Template.getParameters()) {
593     Object Parameter;
594     Parameter["name"] = Param.Name;
595     Parameter["index"] = Param.Index;
596     Parameter["depth"] = Param.Depth;
597     GenericParameters.emplace_back(std::move(Parameter));
598   }
599   if (!GenericParameters.empty())
600     Generics["parameters"] = std::move(GenericParameters);
601 
602   Array GenericConstraints;
603   for (const auto Constr : Template.getConstraints()) {
604     Object Constraint;
605     Constraint["kind"] = Constr.Kind;
606     Constraint["lhs"] = Constr.LHS;
607     Constraint["rhs"] = Constr.RHS;
608     GenericConstraints.emplace_back(std::move(Constraint));
609   }
610 
611   if (!GenericConstraints.empty())
612     Generics["constraints"] = std::move(GenericConstraints);
613 
614   return Generics;
615 }
616 
617 template <typename RecordTy>
618 std::optional<Object> serializeTemplateMixinImpl(const RecordTy &Record,
619                                                  std::false_type) {
620   return std::nullopt;
621 }
622 
623 template <typename RecordTy>
624 void serializeTemplateMixin(Object &Paren, const RecordTy &Record) {
625   serializeObject(Paren, "swiftGenerics",
626                   serializeTemplateMixinImpl(Record, has_template<RecordTy>()));
627 }
628 
629 struct PathComponent {
630   StringRef USR;
631   StringRef Name;
632   APIRecord::RecordKind Kind;
633 
634   PathComponent(StringRef USR, StringRef Name, APIRecord::RecordKind Kind)
635       : USR(USR), Name(Name), Kind(Kind) {}
636 };
637 
638 template <typename RecordTy>
639 bool generatePathComponents(
640     const RecordTy &Record, const APISet &API,
641     function_ref<void(const PathComponent &)> ComponentTransformer) {
642   SmallVector<PathComponent, 4> ReverseComponenents;
643   ReverseComponenents.emplace_back(Record.USR, Record.Name, Record.getKind());
644   const auto *CurrentParent = &Record.ParentInformation;
645   bool FailedToFindParent = false;
646   while (CurrentParent && !CurrentParent->empty()) {
647     PathComponent CurrentParentComponent(CurrentParent->ParentUSR,
648                                          CurrentParent->ParentName,
649                                          CurrentParent->ParentKind);
650 
651     auto *ParentRecord = CurrentParent->ParentRecord;
652     // Slow path if we don't have a direct reference to the ParentRecord
653     if (!ParentRecord)
654       ParentRecord = API.findRecordForUSR(CurrentParent->ParentUSR);
655 
656     // If the parent is a category extended from internal module then we need to
657     // pretend this belongs to the associated interface.
658     if (auto *CategoryRecord =
659             dyn_cast_or_null<ObjCCategoryRecord>(ParentRecord)) {
660       if (!CategoryRecord->IsFromExternalModule) {
661         ParentRecord = API.findRecordForUSR(CategoryRecord->Interface.USR);
662         CurrentParentComponent = PathComponent(CategoryRecord->Interface.USR,
663                                                CategoryRecord->Interface.Name,
664                                                APIRecord::RK_ObjCInterface);
665       }
666     }
667 
668     // The parent record doesn't exist which means the symbol shouldn't be
669     // treated as part of the current product.
670     if (!ParentRecord) {
671       FailedToFindParent = true;
672       break;
673     }
674 
675     ReverseComponenents.push_back(std::move(CurrentParentComponent));
676     CurrentParent = &ParentRecord->ParentInformation;
677   }
678 
679   for (const auto &PC : reverse(ReverseComponenents))
680     ComponentTransformer(PC);
681 
682   return FailedToFindParent;
683 }
684 
685 Object serializeParentContext(const PathComponent &PC, Language Lang) {
686   Object ParentContextElem;
687   ParentContextElem["usr"] = PC.USR;
688   ParentContextElem["name"] = PC.Name;
689   ParentContextElem["kind"] = serializeSymbolKind(PC.Kind, Lang)["identifier"];
690   return ParentContextElem;
691 }
692 
693 template <typename RecordTy>
694 Array generateParentContexts(const RecordTy &Record, const APISet &API,
695                              Language Lang) {
696   Array ParentContexts;
697   generatePathComponents(
698       Record, API, [Lang, &ParentContexts](const PathComponent &PC) {
699         ParentContexts.push_back(serializeParentContext(PC, Lang));
700       });
701 
702   return ParentContexts;
703 }
704 } // namespace
705 
706 /// Defines the format version emitted by SymbolGraphSerializer.
707 const VersionTuple SymbolGraphSerializer::FormatVersion{0, 5, 3};
708 
709 Object SymbolGraphSerializer::serializeMetadata() const {
710   Object Metadata;
711   serializeObject(Metadata, "formatVersion",
712                   serializeSemanticVersion(FormatVersion));
713   Metadata["generator"] = clang::getClangFullVersion();
714   return Metadata;
715 }
716 
717 Object SymbolGraphSerializer::serializeModule() const {
718   Object Module;
719   // The user is expected to always pass `--product-name=` on the command line
720   // to populate this field.
721   Module["name"] = API.ProductName;
722   serializeObject(Module, "platform", serializePlatform(API.getTarget()));
723   return Module;
724 }
725 
726 bool SymbolGraphSerializer::shouldSkip(const APIRecord &Record) const {
727   // Skip explicitly ignored symbols.
728   if (IgnoresList.shouldIgnore(Record.Name))
729     return true;
730 
731   // Skip unconditionally unavailable symbols
732   if (Record.Availabilities.isUnconditionallyUnavailable())
733     return true;
734 
735   // Filter out symbols prefixed with an underscored as they are understood to
736   // be symbols clients should not use.
737   if (Record.Name.startswith("_"))
738     return true;
739 
740   return false;
741 }
742 
743 template <typename RecordTy>
744 std::optional<Object>
745 SymbolGraphSerializer::serializeAPIRecord(const RecordTy &Record) const {
746   if (shouldSkip(Record))
747     return std::nullopt;
748 
749   Object Obj;
750   serializeObject(Obj, "identifier",
751                   serializeIdentifier(Record, API.getLanguage()));
752   serializeObject(Obj, "kind", serializeSymbolKind(Record, API.getLanguage()));
753   serializeObject(Obj, "names", serializeNames(Record));
754   serializeObject(
755       Obj, "location",
756       serializeSourceLocation(Record.Location, /*IncludeFileURI=*/true));
757   serializeArray(Obj, "availability",
758                  serializeAvailability(Record.Availabilities));
759   serializeObject(Obj, "docComment", serializeDocComment(Record.Comment));
760   serializeArray(Obj, "declarationFragments",
761                  serializeDeclarationFragments(Record.Declaration));
762   SmallVector<StringRef, 4> PathComponentsNames;
763   // If this returns true it indicates that we couldn't find a symbol in the
764   // hierarchy.
765   if (generatePathComponents(Record, API,
766                              [&PathComponentsNames](const PathComponent &PC) {
767                                PathComponentsNames.push_back(PC.Name);
768                              }))
769     return {};
770 
771   serializeArray(Obj, "pathComponents", Array(PathComponentsNames));
772 
773   serializeFunctionSignatureMixin(Obj, Record);
774   serializeAccessMixin(Obj, Record);
775   serializeTemplateMixin(Obj, Record);
776 
777   return Obj;
778 }
779 
780 template <typename MemberTy>
781 void SymbolGraphSerializer::serializeMembers(
782     const APIRecord &Record,
783     const SmallVector<std::unique_ptr<MemberTy>> &Members) {
784   // Members should not be serialized if we aren't recursing.
785   if (!ShouldRecurse)
786     return;
787   for (const auto &Member : Members) {
788     auto MemberRecord = serializeAPIRecord(*Member);
789     if (!MemberRecord)
790       continue;
791 
792     Symbols.emplace_back(std::move(*MemberRecord));
793     serializeRelationship(RelationshipKind::MemberOf, *Member, Record);
794   }
795 }
796 
797 StringRef SymbolGraphSerializer::getRelationshipString(RelationshipKind Kind) {
798   switch (Kind) {
799   case RelationshipKind::MemberOf:
800     return "memberOf";
801   case RelationshipKind::InheritsFrom:
802     return "inheritsFrom";
803   case RelationshipKind::ConformsTo:
804     return "conformsTo";
805   case RelationshipKind::ExtensionTo:
806     return "extensionTo";
807   }
808   llvm_unreachable("Unhandled relationship kind");
809 }
810 
811 StringRef SymbolGraphSerializer::getConstraintString(ConstraintKind Kind) {
812   switch (Kind) {
813   case ConstraintKind::Conformance:
814     return "conformance";
815   case ConstraintKind::ConditionalConformance:
816     return "conditionalConformance";
817   }
818   llvm_unreachable("Unhandled constraint kind");
819 }
820 
821 void SymbolGraphSerializer::serializeRelationship(RelationshipKind Kind,
822                                                   SymbolReference Source,
823                                                   SymbolReference Target) {
824   Object Relationship;
825   Relationship["source"] = Source.USR;
826   Relationship["target"] = Target.USR;
827   Relationship["targetFallback"] = Target.Name;
828   Relationship["kind"] = getRelationshipString(Kind);
829 
830   Relationships.emplace_back(std::move(Relationship));
831 }
832 
833 void SymbolGraphSerializer::visitGlobalFunctionRecord(
834     const GlobalFunctionRecord &Record) {
835   auto Obj = serializeAPIRecord(Record);
836   if (!Obj)
837     return;
838 
839   Symbols.emplace_back(std::move(*Obj));
840 }
841 
842 void SymbolGraphSerializer::visitGlobalVariableRecord(
843     const GlobalVariableRecord &Record) {
844   auto Obj = serializeAPIRecord(Record);
845   if (!Obj)
846     return;
847 
848   Symbols.emplace_back(std::move(*Obj));
849 }
850 
851 void SymbolGraphSerializer::visitEnumRecord(const EnumRecord &Record) {
852   auto Enum = serializeAPIRecord(Record);
853   if (!Enum)
854     return;
855 
856   Symbols.emplace_back(std::move(*Enum));
857   serializeMembers(Record, Record.Constants);
858 }
859 
860 void SymbolGraphSerializer::visitStructRecord(const StructRecord &Record) {
861   auto Struct = serializeAPIRecord(Record);
862   if (!Struct)
863     return;
864 
865   Symbols.emplace_back(std::move(*Struct));
866   serializeMembers(Record, Record.Fields);
867 }
868 
869 void SymbolGraphSerializer::visitStaticFieldRecord(
870     const StaticFieldRecord &Record) {
871   auto StaticField = serializeAPIRecord(Record);
872   if (!StaticField)
873     return;
874   Symbols.emplace_back(std::move(*StaticField));
875   serializeRelationship(RelationshipKind::MemberOf, Record, Record.Context);
876 }
877 
878 void SymbolGraphSerializer::visitCXXClassRecord(const CXXClassRecord &Record) {
879   auto Class = serializeAPIRecord(Record);
880   if (!Class)
881     return;
882 
883   Symbols.emplace_back(std::move(*Class));
884   serializeMembers(Record, Record.Fields);
885   serializeMembers(Record, Record.Methods);
886 
887   for (const auto Base : Record.Bases)
888     serializeRelationship(RelationshipKind::InheritsFrom, Record, Base);
889 }
890 
891 void SymbolGraphSerializer::visitClassTemplateRecord(
892     const ClassTemplateRecord &Record) {
893   auto Class = serializeAPIRecord(Record);
894   if (!Class)
895     return;
896 
897   Symbols.emplace_back(std::move(*Class));
898   serializeMembers(Record, Record.Fields);
899   serializeMembers(Record, Record.Methods);
900 
901   for (const auto Base : Record.Bases)
902     serializeRelationship(RelationshipKind::InheritsFrom, Record, Base);
903 }
904 
905 void SymbolGraphSerializer::visitClassTemplateSpecializationRecord(
906     const ClassTemplateSpecializationRecord &Record) {
907   auto Class = serializeAPIRecord(Record);
908   if (!Class)
909     return;
910 
911   Symbols.emplace_back(std::move(*Class));
912   serializeMembers(Record, Record.Fields);
913   serializeMembers(Record, Record.Methods);
914 
915   for (const auto Base : Record.Bases)
916     serializeRelationship(RelationshipKind::InheritsFrom, Record, Base);
917 }
918 
919 void SymbolGraphSerializer::visitClassTemplatePartialSpecializationRecord(
920     const ClassTemplatePartialSpecializationRecord &Record) {
921   auto Class = serializeAPIRecord(Record);
922   if (!Class)
923     return;
924 
925   Symbols.emplace_back(std::move(*Class));
926   serializeMembers(Record, Record.Fields);
927   serializeMembers(Record, Record.Methods);
928 
929   for (const auto Base : Record.Bases)
930     serializeRelationship(RelationshipKind::InheritsFrom, Record, Base);
931 }
932 
933 void SymbolGraphSerializer::visitMethodTemplateRecord(
934     const CXXMethodTemplateRecord &Record) {
935   if (!ShouldRecurse)
936     // Ignore child symbols
937     return;
938   auto MethodTemplate = serializeAPIRecord(Record);
939   if (!MethodTemplate)
940     return;
941   Symbols.emplace_back(std::move(*MethodTemplate));
942   serializeRelationship(RelationshipKind::MemberOf, Record,
943                         Record.ParentInformation.ParentRecord);
944 }
945 
946 void SymbolGraphSerializer::visitMethodTemplateSpecializationRecord(
947     const CXXMethodTemplateSpecializationRecord &Record) {
948   if (!ShouldRecurse)
949     // Ignore child symbols
950     return;
951   auto MethodTemplateSpecialization = serializeAPIRecord(Record);
952   if (!MethodTemplateSpecialization)
953     return;
954   Symbols.emplace_back(std::move(*MethodTemplateSpecialization));
955   serializeRelationship(RelationshipKind::MemberOf, Record,
956                         Record.ParentInformation.ParentRecord);
957 }
958 
959 void SymbolGraphSerializer::visitConceptRecord(const ConceptRecord &Record) {
960   auto Concept = serializeAPIRecord(Record);
961   if (!Concept)
962     return;
963 
964   Symbols.emplace_back(std::move(*Concept));
965 }
966 
967 void SymbolGraphSerializer::visitGlobalVariableTemplateRecord(
968     const GlobalVariableTemplateRecord &Record) {
969   auto GlobalVariableTemplate = serializeAPIRecord(Record);
970   if (!GlobalVariableTemplate)
971     return;
972   Symbols.emplace_back(std::move(*GlobalVariableTemplate));
973 }
974 
975 void SymbolGraphSerializer::visitGlobalVariableTemplateSpecializationRecord(
976     const GlobalVariableTemplateSpecializationRecord &Record) {
977   auto GlobalVariableTemplateSpecialization = serializeAPIRecord(Record);
978   if (!GlobalVariableTemplateSpecialization)
979     return;
980   Symbols.emplace_back(std::move(*GlobalVariableTemplateSpecialization));
981 }
982 
983 void SymbolGraphSerializer::
984     visitGlobalVariableTemplatePartialSpecializationRecord(
985         const GlobalVariableTemplatePartialSpecializationRecord &Record) {
986   auto GlobalVariableTemplatePartialSpecialization = serializeAPIRecord(Record);
987   if (!GlobalVariableTemplatePartialSpecialization)
988     return;
989   Symbols.emplace_back(std::move(*GlobalVariableTemplatePartialSpecialization));
990 }
991 
992 void SymbolGraphSerializer::visitGlobalFunctionTemplateRecord(
993     const GlobalFunctionTemplateRecord &Record) {
994   auto GlobalFunctionTemplate = serializeAPIRecord(Record);
995   if (!GlobalFunctionTemplate)
996     return;
997   Symbols.emplace_back(std::move(*GlobalFunctionTemplate));
998 }
999 
1000 void SymbolGraphSerializer::visitGlobalFunctionTemplateSpecializationRecord(
1001     const GlobalFunctionTemplateSpecializationRecord &Record) {
1002   auto GlobalFunctionTemplateSpecialization = serializeAPIRecord(Record);
1003   if (!GlobalFunctionTemplateSpecialization)
1004     return;
1005   Symbols.emplace_back(std::move(*GlobalFunctionTemplateSpecialization));
1006 }
1007 
1008 void SymbolGraphSerializer::visitObjCContainerRecord(
1009     const ObjCContainerRecord &Record) {
1010   auto ObjCContainer = serializeAPIRecord(Record);
1011   if (!ObjCContainer)
1012     return;
1013 
1014   Symbols.emplace_back(std::move(*ObjCContainer));
1015 
1016   serializeMembers(Record, Record.Ivars);
1017   serializeMembers(Record, Record.Methods);
1018   serializeMembers(Record, Record.Properties);
1019 
1020   for (const auto &Protocol : Record.Protocols)
1021     // Record that Record conforms to Protocol.
1022     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
1023 
1024   if (auto *ObjCInterface = dyn_cast<ObjCInterfaceRecord>(&Record)) {
1025     if (!ObjCInterface->SuperClass.empty())
1026       // If Record is an Objective-C interface record and it has a super class,
1027       // record that Record is inherited from SuperClass.
1028       serializeRelationship(RelationshipKind::InheritsFrom, Record,
1029                             ObjCInterface->SuperClass);
1030 
1031     // Members of categories extending an interface are serialized as members of
1032     // the interface.
1033     for (const auto *Category : ObjCInterface->Categories) {
1034       serializeMembers(Record, Category->Ivars);
1035       serializeMembers(Record, Category->Methods);
1036       serializeMembers(Record, Category->Properties);
1037 
1038       // Surface the protocols of the category to the interface.
1039       for (const auto &Protocol : Category->Protocols)
1040         serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
1041     }
1042   }
1043 }
1044 
1045 void SymbolGraphSerializer::visitObjCCategoryRecord(
1046     const ObjCCategoryRecord &Record) {
1047   if (!Record.IsFromExternalModule)
1048     return;
1049 
1050   // Check if the current Category' parent has been visited before, if so skip.
1051   if (!visitedCategories.contains(Record.Interface.Name)) {
1052     visitedCategories.insert(Record.Interface.Name);
1053     Object Obj;
1054     serializeObject(Obj, "identifier",
1055                     serializeIdentifier(Record, API.getLanguage()));
1056     serializeObject(Obj, "kind",
1057                     serializeSymbolKind(APIRecord::RK_ObjCCategoryModule,
1058                                         API.getLanguage()));
1059     Obj["accessLevel"] = "public";
1060     Symbols.emplace_back(std::move(Obj));
1061   }
1062 
1063   Object Relationship;
1064   Relationship["source"] = Record.USR;
1065   Relationship["target"] = Record.Interface.USR;
1066   Relationship["targetFallback"] = Record.Interface.Name;
1067   Relationship["kind"] = getRelationshipString(RelationshipKind::ExtensionTo);
1068   Relationships.emplace_back(std::move(Relationship));
1069 
1070   auto ObjCCategory = serializeAPIRecord(Record);
1071 
1072   if (!ObjCCategory)
1073     return;
1074 
1075   Symbols.emplace_back(std::move(*ObjCCategory));
1076   serializeMembers(Record, Record.Methods);
1077   serializeMembers(Record, Record.Properties);
1078 
1079   // Surface the protocols of the category to the interface.
1080   for (const auto &Protocol : Record.Protocols)
1081     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
1082 }
1083 
1084 void SymbolGraphSerializer::visitMacroDefinitionRecord(
1085     const MacroDefinitionRecord &Record) {
1086   auto Macro = serializeAPIRecord(Record);
1087 
1088   if (!Macro)
1089     return;
1090 
1091   Symbols.emplace_back(std::move(*Macro));
1092 }
1093 
1094 void SymbolGraphSerializer::serializeSingleRecord(const APIRecord *Record) {
1095   switch (Record->getKind()) {
1096   case APIRecord::RK_Unknown:
1097     llvm_unreachable("Records should have a known kind!");
1098   case APIRecord::RK_GlobalFunction:
1099     visitGlobalFunctionRecord(*cast<GlobalFunctionRecord>(Record));
1100     break;
1101   case APIRecord::RK_GlobalVariable:
1102     visitGlobalVariableRecord(*cast<GlobalVariableRecord>(Record));
1103     break;
1104   case APIRecord::RK_Enum:
1105     visitEnumRecord(*cast<EnumRecord>(Record));
1106     break;
1107   case APIRecord::RK_Struct:
1108     visitStructRecord(*cast<StructRecord>(Record));
1109     break;
1110   case APIRecord::RK_StaticField:
1111     visitStaticFieldRecord(*cast<StaticFieldRecord>(Record));
1112     break;
1113   case APIRecord::RK_CXXClass:
1114     visitCXXClassRecord(*cast<CXXClassRecord>(Record));
1115     break;
1116   case APIRecord::RK_ObjCInterface:
1117     visitObjCContainerRecord(*cast<ObjCInterfaceRecord>(Record));
1118     break;
1119   case APIRecord::RK_ObjCProtocol:
1120     visitObjCContainerRecord(*cast<ObjCProtocolRecord>(Record));
1121     break;
1122   case APIRecord::RK_ObjCCategory:
1123     visitObjCCategoryRecord(*cast<ObjCCategoryRecord>(Record));
1124     break;
1125   case APIRecord::RK_MacroDefinition:
1126     visitMacroDefinitionRecord(*cast<MacroDefinitionRecord>(Record));
1127     break;
1128   case APIRecord::RK_Typedef:
1129     visitTypedefRecord(*cast<TypedefRecord>(Record));
1130     break;
1131   default:
1132     if (auto Obj = serializeAPIRecord(*Record)) {
1133       Symbols.emplace_back(std::move(*Obj));
1134       auto &ParentInformation = Record->ParentInformation;
1135       if (!ParentInformation.empty())
1136         serializeRelationship(RelationshipKind::MemberOf, *Record,
1137                               *ParentInformation.ParentRecord);
1138     }
1139     break;
1140   }
1141 }
1142 
1143 void SymbolGraphSerializer::visitTypedefRecord(const TypedefRecord &Record) {
1144   // Typedefs of anonymous types have their entries unified with the underlying
1145   // type.
1146   bool ShouldDrop = Record.UnderlyingType.Name.empty();
1147   // enums declared with `NS_OPTION` have a named enum and a named typedef, with
1148   // the same name
1149   ShouldDrop |= (Record.UnderlyingType.Name == Record.Name);
1150   if (ShouldDrop)
1151     return;
1152 
1153   auto Typedef = serializeAPIRecord(Record);
1154   if (!Typedef)
1155     return;
1156 
1157   (*Typedef)["type"] = Record.UnderlyingType.USR;
1158 
1159   Symbols.emplace_back(std::move(*Typedef));
1160 }
1161 
1162 Object SymbolGraphSerializer::serialize() {
1163   traverseAPISet();
1164   return serializeCurrentGraph();
1165 }
1166 
1167 Object SymbolGraphSerializer::serializeCurrentGraph() {
1168   Object Root;
1169   serializeObject(Root, "metadata", serializeMetadata());
1170   serializeObject(Root, "module", serializeModule());
1171 
1172   Root["symbols"] = std::move(Symbols);
1173   Root["relationships"] = std::move(Relationships);
1174 
1175   return Root;
1176 }
1177 
1178 void SymbolGraphSerializer::serialize(raw_ostream &os) {
1179   Object root = serialize();
1180   if (Options.Compact)
1181     os << formatv("{0}", Value(std::move(root))) << "\n";
1182   else
1183     os << formatv("{0:2}", Value(std::move(root))) << "\n";
1184 }
1185 
1186 std::optional<Object>
1187 SymbolGraphSerializer::serializeSingleSymbolSGF(StringRef USR,
1188                                                 const APISet &API) {
1189   APIRecord *Record = API.findRecordForUSR(USR);
1190   if (!Record)
1191     return {};
1192 
1193   Object Root;
1194   APIIgnoresList EmptyIgnores;
1195   SymbolGraphSerializer Serializer(API, EmptyIgnores,
1196                                    /*Options.Compact*/ {true},
1197                                    /*ShouldRecurse*/ false);
1198   Serializer.serializeSingleRecord(Record);
1199   serializeObject(Root, "symbolGraph", Serializer.serializeCurrentGraph());
1200 
1201   Language Lang = API.getLanguage();
1202   serializeArray(Root, "parentContexts",
1203                  generateParentContexts(*Record, API, Lang));
1204 
1205   Array RelatedSymbols;
1206 
1207   for (const auto &Fragment : Record->Declaration.getFragments()) {
1208     // If we don't have a USR there isn't much we can do.
1209     if (Fragment.PreciseIdentifier.empty())
1210       continue;
1211 
1212     APIRecord *RelatedRecord = API.findRecordForUSR(Fragment.PreciseIdentifier);
1213 
1214     // If we can't find the record let's skip.
1215     if (!RelatedRecord)
1216       continue;
1217 
1218     Object RelatedSymbol;
1219     RelatedSymbol["usr"] = RelatedRecord->USR;
1220     RelatedSymbol["declarationLanguage"] = getLanguageName(Lang);
1221     // TODO: once we record this properly let's serialize it right.
1222     RelatedSymbol["accessLevel"] = "public";
1223     RelatedSymbol["filePath"] = RelatedRecord->Location.getFilename();
1224     RelatedSymbol["moduleName"] = API.ProductName;
1225     RelatedSymbol["isSystem"] = RelatedRecord->IsFromSystemHeader;
1226 
1227     serializeArray(RelatedSymbol, "parentContexts",
1228                    generateParentContexts(*RelatedRecord, API, Lang));
1229     RelatedSymbols.push_back(std::move(RelatedSymbol));
1230   }
1231 
1232   serializeArray(Root, "relatedSymbols", RelatedSymbols);
1233   return Root;
1234 }
1235