xref: /llvm-project/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp (revision 314a5d707fe70369e3e1a6ac561d64884896dbd7)
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_GlobalVariable:
365     Kind["identifier"] = AddLangPrefix("var");
366     Kind["displayName"] = "Global Variable";
367     break;
368   case APIRecord::RK_EnumConstant:
369     Kind["identifier"] = AddLangPrefix("enum.case");
370     Kind["displayName"] = "Enumeration Case";
371     break;
372   case APIRecord::RK_Enum:
373     Kind["identifier"] = AddLangPrefix("enum");
374     Kind["displayName"] = "Enumeration";
375     break;
376   case APIRecord::RK_StructField:
377     Kind["identifier"] = AddLangPrefix("property");
378     Kind["displayName"] = "Instance Property";
379     break;
380   case APIRecord::RK_Struct:
381     Kind["identifier"] = AddLangPrefix("struct");
382     Kind["displayName"] = "Structure";
383     break;
384   case APIRecord::RK_CXXField:
385     Kind["identifier"] = AddLangPrefix("property");
386     Kind["displayName"] = "Instance Property";
387     break;
388   case APIRecord::RK_Union:
389     Kind["identifier"] = AddLangPrefix("union");
390     Kind["displayName"] = "Union";
391     break;
392   case APIRecord::RK_StaticField:
393     Kind["identifier"] = AddLangPrefix("type.property");
394     Kind["displayName"] = "Type Property";
395     break;
396   case APIRecord::RK_CXXClass:
397     Kind["identifier"] = AddLangPrefix("class");
398     Kind["displayName"] = "Class";
399     break;
400   case APIRecord::RK_CXXStaticMethod:
401     Kind["identifier"] = AddLangPrefix("type.method");
402     Kind["displayName"] = "Static Method";
403     break;
404   case APIRecord::RK_CXXInstanceMethod:
405     Kind["identifier"] = AddLangPrefix("method");
406     Kind["displayName"] = "Instance Method";
407     break;
408   case APIRecord::RK_CXXConstructorMethod:
409     Kind["identifier"] = AddLangPrefix("method");
410     Kind["displayName"] = "Constructor";
411     break;
412   case APIRecord::RK_CXXDestructorMethod:
413     Kind["identifier"] = AddLangPrefix("method");
414     Kind["displayName"] = "Destructor";
415     break;
416   case APIRecord::RK_ObjCIvar:
417     Kind["identifier"] = AddLangPrefix("ivar");
418     Kind["displayName"] = "Instance Variable";
419     break;
420   case APIRecord::RK_ObjCInstanceMethod:
421     Kind["identifier"] = AddLangPrefix("method");
422     Kind["displayName"] = "Instance Method";
423     break;
424   case APIRecord::RK_ObjCClassMethod:
425     Kind["identifier"] = AddLangPrefix("type.method");
426     Kind["displayName"] = "Type Method";
427     break;
428   case APIRecord::RK_ObjCInstanceProperty:
429     Kind["identifier"] = AddLangPrefix("property");
430     Kind["displayName"] = "Instance Property";
431     break;
432   case APIRecord::RK_ObjCClassProperty:
433     Kind["identifier"] = AddLangPrefix("type.property");
434     Kind["displayName"] = "Type Property";
435     break;
436   case APIRecord::RK_ObjCInterface:
437     Kind["identifier"] = AddLangPrefix("class");
438     Kind["displayName"] = "Class";
439     break;
440   case APIRecord::RK_ObjCCategory:
441     Kind["identifier"] = AddLangPrefix("class.extension");
442     Kind["displayName"] = "Class Extension";
443     break;
444   case APIRecord::RK_ObjCCategoryModule:
445     Kind["identifier"] = AddLangPrefix("module.extension");
446     Kind["displayName"] = "Module Extension";
447     break;
448   case APIRecord::RK_ObjCProtocol:
449     Kind["identifier"] = AddLangPrefix("protocol");
450     Kind["displayName"] = "Protocol";
451     break;
452   case APIRecord::RK_MacroDefinition:
453     Kind["identifier"] = AddLangPrefix("macro");
454     Kind["displayName"] = "Macro";
455     break;
456   case APIRecord::RK_Typedef:
457     Kind["identifier"] = AddLangPrefix("typealias");
458     Kind["displayName"] = "Type Alias";
459     break;
460   }
461 
462   return Kind;
463 }
464 
465 /// Serialize the symbol kind information.
466 ///
467 /// The Symbol Graph symbol kind property contains a shorthand \c identifier
468 /// which is prefixed by the source language name, useful for tooling to parse
469 /// the kind, and a \c displayName for rendering human-readable names.
470 Object serializeSymbolKind(const APIRecord &Record, Language Lang) {
471   return serializeSymbolKind(Record.getKind(), Lang);
472 }
473 
474 template <typename RecordTy>
475 std::optional<Object>
476 serializeFunctionSignatureMixinImpl(const RecordTy &Record, std::true_type) {
477   const auto &FS = Record.Signature;
478   if (FS.empty())
479     return std::nullopt;
480 
481   Object Signature;
482   serializeArray(Signature, "returns",
483                  serializeDeclarationFragments(FS.getReturnType()));
484 
485   Array Parameters;
486   for (const auto &P : FS.getParameters()) {
487     Object Parameter;
488     Parameter["name"] = P.Name;
489     serializeArray(Parameter, "declarationFragments",
490                    serializeDeclarationFragments(P.Fragments));
491     Parameters.emplace_back(std::move(Parameter));
492   }
493 
494   if (!Parameters.empty())
495     Signature["parameters"] = std::move(Parameters);
496 
497   return Signature;
498 }
499 
500 template <typename RecordTy>
501 std::optional<Object>
502 serializeFunctionSignatureMixinImpl(const RecordTy &Record, std::false_type) {
503   return std::nullopt;
504 }
505 
506 /// Serialize the function signature field, as specified by the
507 /// Symbol Graph format.
508 ///
509 /// The Symbol Graph function signature property contains two arrays.
510 ///   - The \c returns array is the declaration fragments of the return type;
511 ///   - The \c parameters array contains names and declaration fragments of the
512 ///     parameters.
513 ///
514 /// \returns \c std::nullopt if \p FS is empty, or an \c Object containing the
515 /// formatted function signature.
516 template <typename RecordTy>
517 void serializeFunctionSignatureMixin(Object &Paren, const RecordTy &Record) {
518   serializeObject(Paren, "functionSignature",
519                   serializeFunctionSignatureMixinImpl(
520                       Record, has_function_signature<RecordTy>()));
521 }
522 
523 template <typename RecordTy>
524 std::optional<std::string> serializeAccessMixinImpl(const RecordTy &Record,
525                                                     std::true_type) {
526   const auto &AccessControl = Record.Access;
527   std::string Access;
528   if (AccessControl.empty())
529     return std::nullopt;
530   Access = AccessControl.getAccess();
531   return Access;
532 }
533 
534 template <typename RecordTy>
535 std::optional<std::string> serializeAccessMixinImpl(const RecordTy &Record,
536                                                     std::false_type) {
537   return std::nullopt;
538 }
539 
540 template <typename RecordTy>
541 void serializeAccessMixin(Object &Paren, const RecordTy &Record) {
542   auto accessLevel = serializeAccessMixinImpl(Record, has_access<RecordTy>());
543   if (!accessLevel.has_value())
544     accessLevel = "public";
545   serializeString(Paren, "accessLevel", accessLevel);
546 }
547 
548 struct PathComponent {
549   StringRef USR;
550   StringRef Name;
551   APIRecord::RecordKind Kind;
552 
553   PathComponent(StringRef USR, StringRef Name, APIRecord::RecordKind Kind)
554       : USR(USR), Name(Name), Kind(Kind) {}
555 };
556 
557 template <typename RecordTy>
558 bool generatePathComponents(
559     const RecordTy &Record, const APISet &API,
560     function_ref<void(const PathComponent &)> ComponentTransformer) {
561   SmallVector<PathComponent, 4> ReverseComponenents;
562   ReverseComponenents.emplace_back(Record.USR, Record.Name, Record.getKind());
563   const auto *CurrentParent = &Record.ParentInformation;
564   bool FailedToFindParent = false;
565   while (CurrentParent && !CurrentParent->empty()) {
566     PathComponent CurrentParentComponent(CurrentParent->ParentUSR,
567                                          CurrentParent->ParentName,
568                                          CurrentParent->ParentKind);
569 
570     auto *ParentRecord = CurrentParent->ParentRecord;
571     // Slow path if we don't have a direct reference to the ParentRecord
572     if (!ParentRecord)
573       ParentRecord = API.findRecordForUSR(CurrentParent->ParentUSR);
574 
575     // If the parent is a category extended from internal module then we need to
576     // pretend this belongs to the associated interface.
577     if (auto *CategoryRecord =
578             dyn_cast_or_null<ObjCCategoryRecord>(ParentRecord)) {
579       if (!CategoryRecord->IsFromExternalModule) {
580         ParentRecord = API.findRecordForUSR(CategoryRecord->Interface.USR);
581         CurrentParentComponent = PathComponent(CategoryRecord->Interface.USR,
582                                                CategoryRecord->Interface.Name,
583                                                APIRecord::RK_ObjCInterface);
584       }
585     }
586 
587     // The parent record doesn't exist which means the symbol shouldn't be
588     // treated as part of the current product.
589     if (!ParentRecord) {
590       FailedToFindParent = true;
591       break;
592     }
593 
594     ReverseComponenents.push_back(std::move(CurrentParentComponent));
595     CurrentParent = &ParentRecord->ParentInformation;
596   }
597 
598   for (const auto &PC : reverse(ReverseComponenents))
599     ComponentTransformer(PC);
600 
601   return FailedToFindParent;
602 }
603 
604 Object serializeParentContext(const PathComponent &PC, Language Lang) {
605   Object ParentContextElem;
606   ParentContextElem["usr"] = PC.USR;
607   ParentContextElem["name"] = PC.Name;
608   ParentContextElem["kind"] = serializeSymbolKind(PC.Kind, Lang)["identifier"];
609   return ParentContextElem;
610 }
611 
612 template <typename RecordTy>
613 Array generateParentContexts(const RecordTy &Record, const APISet &API,
614                              Language Lang) {
615   Array ParentContexts;
616   generatePathComponents(
617       Record, API, [Lang, &ParentContexts](const PathComponent &PC) {
618         ParentContexts.push_back(serializeParentContext(PC, Lang));
619       });
620 
621   return ParentContexts;
622 }
623 } // namespace
624 
625 /// Defines the format version emitted by SymbolGraphSerializer.
626 const VersionTuple SymbolGraphSerializer::FormatVersion{0, 5, 3};
627 
628 Object SymbolGraphSerializer::serializeMetadata() const {
629   Object Metadata;
630   serializeObject(Metadata, "formatVersion",
631                   serializeSemanticVersion(FormatVersion));
632   Metadata["generator"] = clang::getClangFullVersion();
633   return Metadata;
634 }
635 
636 Object SymbolGraphSerializer::serializeModule() const {
637   Object Module;
638   // The user is expected to always pass `--product-name=` on the command line
639   // to populate this field.
640   Module["name"] = API.ProductName;
641   serializeObject(Module, "platform", serializePlatform(API.getTarget()));
642   return Module;
643 }
644 
645 bool SymbolGraphSerializer::shouldSkip(const APIRecord &Record) const {
646   // Skip explicitly ignored symbols.
647   if (IgnoresList.shouldIgnore(Record.Name))
648     return true;
649 
650   // Skip unconditionally unavailable symbols
651   if (Record.Availabilities.isUnconditionallyUnavailable())
652     return true;
653 
654   // Filter out symbols prefixed with an underscored as they are understood to
655   // be symbols clients should not use.
656   if (Record.Name.startswith("_"))
657     return true;
658 
659   return false;
660 }
661 
662 template <typename RecordTy>
663 std::optional<Object>
664 SymbolGraphSerializer::serializeAPIRecord(const RecordTy &Record) const {
665   if (shouldSkip(Record))
666     return std::nullopt;
667 
668   Object Obj;
669   serializeObject(Obj, "identifier",
670                   serializeIdentifier(Record, API.getLanguage()));
671   serializeObject(Obj, "kind", serializeSymbolKind(Record, API.getLanguage()));
672   serializeObject(Obj, "names", serializeNames(Record));
673   serializeObject(
674       Obj, "location",
675       serializeSourceLocation(Record.Location, /*IncludeFileURI=*/true));
676   serializeArray(Obj, "availability",
677                  serializeAvailability(Record.Availabilities));
678   serializeObject(Obj, "docComment", serializeDocComment(Record.Comment));
679   serializeArray(Obj, "declarationFragments",
680                  serializeDeclarationFragments(Record.Declaration));
681   SmallVector<StringRef, 4> PathComponentsNames;
682   // If this returns true it indicates that we couldn't find a symbol in the
683   // hierarchy.
684   if (generatePathComponents(Record, API,
685                              [&PathComponentsNames](const PathComponent &PC) {
686                                PathComponentsNames.push_back(PC.Name);
687                              }))
688     return {};
689 
690   serializeArray(Obj, "pathComponents", Array(PathComponentsNames));
691 
692   serializeFunctionSignatureMixin(Obj, Record);
693   serializeAccessMixin(Obj, Record);
694 
695   return Obj;
696 }
697 
698 template <typename MemberTy>
699 void SymbolGraphSerializer::serializeMembers(
700     const APIRecord &Record,
701     const SmallVector<std::unique_ptr<MemberTy>> &Members) {
702   // Members should not be serialized if we aren't recursing.
703   if (!ShouldRecurse)
704     return;
705   for (const auto &Member : Members) {
706     auto MemberRecord = serializeAPIRecord(*Member);
707     if (!MemberRecord)
708       continue;
709 
710     Symbols.emplace_back(std::move(*MemberRecord));
711     serializeRelationship(RelationshipKind::MemberOf, *Member, Record);
712   }
713 }
714 
715 StringRef SymbolGraphSerializer::getRelationshipString(RelationshipKind Kind) {
716   switch (Kind) {
717   case RelationshipKind::MemberOf:
718     return "memberOf";
719   case RelationshipKind::InheritsFrom:
720     return "inheritsFrom";
721   case RelationshipKind::ConformsTo:
722     return "conformsTo";
723   case RelationshipKind::ExtensionTo:
724     return "extensionTo";
725   }
726   llvm_unreachable("Unhandled relationship kind");
727 }
728 
729 void SymbolGraphSerializer::serializeRelationship(RelationshipKind Kind,
730                                                   SymbolReference Source,
731                                                   SymbolReference Target) {
732   Object Relationship;
733   Relationship["source"] = Source.USR;
734   Relationship["target"] = Target.USR;
735   Relationship["targetFallback"] = Target.Name;
736   Relationship["kind"] = getRelationshipString(Kind);
737 
738   Relationships.emplace_back(std::move(Relationship));
739 }
740 
741 void SymbolGraphSerializer::visitGlobalFunctionRecord(
742     const GlobalFunctionRecord &Record) {
743   auto Obj = serializeAPIRecord(Record);
744   if (!Obj)
745     return;
746 
747   Symbols.emplace_back(std::move(*Obj));
748 }
749 
750 void SymbolGraphSerializer::visitGlobalVariableRecord(
751     const GlobalVariableRecord &Record) {
752   auto Obj = serializeAPIRecord(Record);
753   if (!Obj)
754     return;
755 
756   Symbols.emplace_back(std::move(*Obj));
757 }
758 
759 void SymbolGraphSerializer::visitEnumRecord(const EnumRecord &Record) {
760   auto Enum = serializeAPIRecord(Record);
761   if (!Enum)
762     return;
763 
764   Symbols.emplace_back(std::move(*Enum));
765   serializeMembers(Record, Record.Constants);
766 }
767 
768 void SymbolGraphSerializer::visitStructRecord(const StructRecord &Record) {
769   auto Struct = serializeAPIRecord(Record);
770   if (!Struct)
771     return;
772 
773   Symbols.emplace_back(std::move(*Struct));
774   serializeMembers(Record, Record.Fields);
775 }
776 
777 void SymbolGraphSerializer::visitStaticFieldRecord(
778     const StaticFieldRecord &Record) {
779   auto StaticField = serializeAPIRecord(Record);
780   if (!StaticField)
781     return;
782   Symbols.emplace_back(std::move(*StaticField));
783   serializeRelationship(RelationshipKind::MemberOf, Record, Record.Context);
784 }
785 
786 void SymbolGraphSerializer::visitCXXClassRecord(const CXXClassRecord &Record) {
787   auto Class = serializeAPIRecord(Record);
788   if (!Class)
789     return;
790 
791   Symbols.emplace_back(std::move(*Class));
792   serializeMembers(Record, Record.Fields);
793   serializeMembers(Record, Record.Methods);
794 
795   for (const auto Base : Record.Bases)
796     serializeRelationship(RelationshipKind::InheritsFrom, Record, Base);
797 }
798 
799 void SymbolGraphSerializer::visitObjCContainerRecord(
800     const ObjCContainerRecord &Record) {
801   auto ObjCContainer = serializeAPIRecord(Record);
802   if (!ObjCContainer)
803     return;
804 
805   Symbols.emplace_back(std::move(*ObjCContainer));
806 
807   serializeMembers(Record, Record.Ivars);
808   serializeMembers(Record, Record.Methods);
809   serializeMembers(Record, Record.Properties);
810 
811   for (const auto &Protocol : Record.Protocols)
812     // Record that Record conforms to Protocol.
813     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
814 
815   if (auto *ObjCInterface = dyn_cast<ObjCInterfaceRecord>(&Record)) {
816     if (!ObjCInterface->SuperClass.empty())
817       // If Record is an Objective-C interface record and it has a super class,
818       // record that Record is inherited from SuperClass.
819       serializeRelationship(RelationshipKind::InheritsFrom, Record,
820                             ObjCInterface->SuperClass);
821 
822     // Members of categories extending an interface are serialized as members of
823     // the interface.
824     for (const auto *Category : ObjCInterface->Categories) {
825       serializeMembers(Record, Category->Ivars);
826       serializeMembers(Record, Category->Methods);
827       serializeMembers(Record, Category->Properties);
828 
829       // Surface the protocols of the category to the interface.
830       for (const auto &Protocol : Category->Protocols)
831         serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
832     }
833   }
834 }
835 
836 void SymbolGraphSerializer::visitObjCCategoryRecord(
837     const ObjCCategoryRecord &Record) {
838   if (!Record.IsFromExternalModule)
839     return;
840 
841   // Check if the current Category' parent has been visited before, if so skip.
842   if (!visitedCategories.contains(Record.Interface.Name)) {
843     visitedCategories.insert(Record.Interface.Name);
844     Object Obj;
845     serializeObject(Obj, "identifier",
846                     serializeIdentifier(Record, API.getLanguage()));
847     serializeObject(Obj, "kind",
848                     serializeSymbolKind(APIRecord::RK_ObjCCategoryModule,
849                                         API.getLanguage()));
850     Obj["accessLevel"] = "public";
851     Symbols.emplace_back(std::move(Obj));
852   }
853 
854   Object Relationship;
855   Relationship["source"] = Record.USR;
856   Relationship["target"] = Record.Interface.USR;
857   Relationship["targetFallback"] = Record.Interface.Name;
858   Relationship["kind"] = getRelationshipString(RelationshipKind::ExtensionTo);
859   Relationships.emplace_back(std::move(Relationship));
860 
861   auto ObjCCategory = serializeAPIRecord(Record);
862 
863   if (!ObjCCategory)
864     return;
865 
866   Symbols.emplace_back(std::move(*ObjCCategory));
867   serializeMembers(Record, Record.Methods);
868   serializeMembers(Record, Record.Properties);
869 
870   // Surface the protocols of the category to the interface.
871   for (const auto &Protocol : Record.Protocols)
872     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
873 }
874 
875 void SymbolGraphSerializer::visitMacroDefinitionRecord(
876     const MacroDefinitionRecord &Record) {
877   auto Macro = serializeAPIRecord(Record);
878 
879   if (!Macro)
880     return;
881 
882   Symbols.emplace_back(std::move(*Macro));
883 }
884 
885 void SymbolGraphSerializer::serializeSingleRecord(const APIRecord *Record) {
886   switch (Record->getKind()) {
887   case APIRecord::RK_Unknown:
888     llvm_unreachable("Records should have a known kind!");
889   case APIRecord::RK_GlobalFunction:
890     visitGlobalFunctionRecord(*cast<GlobalFunctionRecord>(Record));
891     break;
892   case APIRecord::RK_GlobalVariable:
893     visitGlobalVariableRecord(*cast<GlobalVariableRecord>(Record));
894     break;
895   case APIRecord::RK_Enum:
896     visitEnumRecord(*cast<EnumRecord>(Record));
897     break;
898   case APIRecord::RK_Struct:
899     visitStructRecord(*cast<StructRecord>(Record));
900     break;
901   case APIRecord::RK_StaticField:
902     visitStaticFieldRecord(*cast<StaticFieldRecord>(Record));
903     break;
904   case APIRecord::RK_CXXClass:
905     visitCXXClassRecord(*cast<CXXClassRecord>(Record));
906     break;
907   case APIRecord::RK_ObjCInterface:
908     visitObjCContainerRecord(*cast<ObjCInterfaceRecord>(Record));
909     break;
910   case APIRecord::RK_ObjCProtocol:
911     visitObjCContainerRecord(*cast<ObjCProtocolRecord>(Record));
912     break;
913   case APIRecord::RK_ObjCCategory:
914     visitObjCCategoryRecord(*cast<ObjCCategoryRecord>(Record));
915     break;
916   case APIRecord::RK_MacroDefinition:
917     visitMacroDefinitionRecord(*cast<MacroDefinitionRecord>(Record));
918     break;
919   case APIRecord::RK_Typedef:
920     visitTypedefRecord(*cast<TypedefRecord>(Record));
921     break;
922   default:
923     if (auto Obj = serializeAPIRecord(*Record)) {
924       Symbols.emplace_back(std::move(*Obj));
925       auto &ParentInformation = Record->ParentInformation;
926       if (!ParentInformation.empty())
927         serializeRelationship(RelationshipKind::MemberOf, *Record,
928                               *ParentInformation.ParentRecord);
929     }
930     break;
931   }
932 }
933 
934 void SymbolGraphSerializer::visitTypedefRecord(const TypedefRecord &Record) {
935   // Typedefs of anonymous types have their entries unified with the underlying
936   // type.
937   bool ShouldDrop = Record.UnderlyingType.Name.empty();
938   // enums declared with `NS_OPTION` have a named enum and a named typedef, with
939   // the same name
940   ShouldDrop |= (Record.UnderlyingType.Name == Record.Name);
941   if (ShouldDrop)
942     return;
943 
944   auto Typedef = serializeAPIRecord(Record);
945   if (!Typedef)
946     return;
947 
948   (*Typedef)["type"] = Record.UnderlyingType.USR;
949 
950   Symbols.emplace_back(std::move(*Typedef));
951 }
952 
953 Object SymbolGraphSerializer::serialize() {
954   traverseAPISet();
955   return serializeCurrentGraph();
956 }
957 
958 Object SymbolGraphSerializer::serializeCurrentGraph() {
959   Object Root;
960   serializeObject(Root, "metadata", serializeMetadata());
961   serializeObject(Root, "module", serializeModule());
962 
963   Root["symbols"] = std::move(Symbols);
964   Root["relationships"] = std::move(Relationships);
965 
966   return Root;
967 }
968 
969 void SymbolGraphSerializer::serialize(raw_ostream &os) {
970   Object root = serialize();
971   if (Options.Compact)
972     os << formatv("{0}", Value(std::move(root))) << "\n";
973   else
974     os << formatv("{0:2}", Value(std::move(root))) << "\n";
975 }
976 
977 std::optional<Object>
978 SymbolGraphSerializer::serializeSingleSymbolSGF(StringRef USR,
979                                                 const APISet &API) {
980   APIRecord *Record = API.findRecordForUSR(USR);
981   if (!Record)
982     return {};
983 
984   Object Root;
985   APIIgnoresList EmptyIgnores;
986   SymbolGraphSerializer Serializer(API, EmptyIgnores,
987                                    /*Options.Compact*/ {true},
988                                    /*ShouldRecurse*/ false);
989   Serializer.serializeSingleRecord(Record);
990   serializeObject(Root, "symbolGraph", Serializer.serializeCurrentGraph());
991 
992   Language Lang = API.getLanguage();
993   serializeArray(Root, "parentContexts",
994                  generateParentContexts(*Record, API, Lang));
995 
996   Array RelatedSymbols;
997 
998   for (const auto &Fragment : Record->Declaration.getFragments()) {
999     // If we don't have a USR there isn't much we can do.
1000     if (Fragment.PreciseIdentifier.empty())
1001       continue;
1002 
1003     APIRecord *RelatedRecord = API.findRecordForUSR(Fragment.PreciseIdentifier);
1004 
1005     // If we can't find the record let's skip.
1006     if (!RelatedRecord)
1007       continue;
1008 
1009     Object RelatedSymbol;
1010     RelatedSymbol["usr"] = RelatedRecord->USR;
1011     RelatedSymbol["declarationLanguage"] = getLanguageName(Lang);
1012     // TODO: once we record this properly let's serialize it right.
1013     RelatedSymbol["accessLevel"] = "public";
1014     RelatedSymbol["filePath"] = RelatedRecord->Location.getFilename();
1015     RelatedSymbol["moduleName"] = API.ProductName;
1016     RelatedSymbol["isSystem"] = RelatedRecord->IsFromSystemHeader;
1017 
1018     serializeArray(RelatedSymbol, "parentContexts",
1019                    generateParentContexts(*RelatedRecord, API, Lang));
1020     RelatedSymbols.push_back(std::move(RelatedSymbol));
1021   }
1022 
1023   serializeArray(Root, "relatedSymbols", RelatedSymbols);
1024   return Root;
1025 }
1026