xref: /llvm-project/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp (revision d3fc779e4295b0bf726008580ddd99c8e86c2c0c)
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/Version.h"
16 #include "clang/ExtractAPI/API.h"
17 #include "clang/ExtractAPI/DeclarationFragments.h"
18 #include "llvm/Support/JSON.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/VersionTuple.h"
21 #include <type_traits>
22 
23 using namespace clang;
24 using namespace clang::extractapi;
25 using namespace llvm;
26 using namespace llvm::json;
27 
28 namespace {
29 
30 /// Helper function to inject a JSON object \p Obj into another object \p Paren
31 /// at position \p Key.
32 void serializeObject(Object &Paren, StringRef Key, Optional<Object> Obj) {
33   if (Obj)
34     Paren[Key] = std::move(Obj.value());
35 }
36 
37 /// Helper function to inject a JSON array \p Array into object \p Paren at
38 /// position \p Key.
39 void serializeArray(Object &Paren, StringRef Key, Optional<Array> Array) {
40   if (Array)
41     Paren[Key] = std::move(Array.value());
42 }
43 
44 /// Serialize a \c VersionTuple \p V with the Symbol Graph semantic version
45 /// format.
46 ///
47 /// A semantic version object contains three numeric fields, representing the
48 /// \c major, \c minor, and \c patch parts of the version tuple.
49 /// For example version tuple 1.0.3 is serialized as:
50 /// \code
51 ///   {
52 ///     "major" : 1,
53 ///     "minor" : 0,
54 ///     "patch" : 3
55 ///   }
56 /// \endcode
57 ///
58 /// \returns \c None if the version \p V is empty, or an \c Object containing
59 /// the semantic version representation of \p V.
60 Optional<Object> serializeSemanticVersion(const VersionTuple &V) {
61   if (V.empty())
62     return None;
63 
64   Object Version;
65   Version["major"] = V.getMajor();
66   Version["minor"] = V.getMinor().value_or(0);
67   Version["patch"] = V.getSubminor().value_or(0);
68   return Version;
69 }
70 
71 /// Serialize the OS information in the Symbol Graph platform property.
72 ///
73 /// The OS information in Symbol Graph contains the \c name of the OS, and an
74 /// optional \c minimumVersion semantic version field.
75 Object serializeOperatingSystem(const Triple &T) {
76   Object OS;
77   OS["name"] = T.getOSTypeName(T.getOS());
78   serializeObject(OS, "minimumVersion",
79                   serializeSemanticVersion(T.getMinimumSupportedOSVersion()));
80   return OS;
81 }
82 
83 /// Serialize the platform information in the Symbol Graph module section.
84 ///
85 /// The platform object describes a target platform triple in corresponding
86 /// three fields: \c architecture, \c vendor, and \c operatingSystem.
87 Object serializePlatform(const Triple &T) {
88   Object Platform;
89   Platform["architecture"] = T.getArchName();
90   Platform["vendor"] = T.getVendorName();
91   Platform["operatingSystem"] = serializeOperatingSystem(T);
92   return Platform;
93 }
94 
95 /// Serialize a source position.
96 Object serializeSourcePosition(const PresumedLoc &Loc) {
97   assert(Loc.isValid() && "invalid source position");
98 
99   Object SourcePosition;
100   SourcePosition["line"] = Loc.getLine();
101   SourcePosition["character"] = Loc.getColumn();
102 
103   return SourcePosition;
104 }
105 
106 /// Serialize a source location in file.
107 ///
108 /// \param Loc The presumed location to serialize.
109 /// \param IncludeFileURI If true, include the file path of \p Loc as a URI.
110 /// Defaults to false.
111 Object serializeSourceLocation(const PresumedLoc &Loc,
112                                bool IncludeFileURI = false) {
113   Object SourceLocation;
114   serializeObject(SourceLocation, "position", serializeSourcePosition(Loc));
115 
116   if (IncludeFileURI) {
117     std::string FileURI = "file://";
118     // Normalize file path to use forward slashes for the URI.
119     FileURI += sys::path::convert_to_slash(Loc.getFilename());
120     SourceLocation["uri"] = FileURI;
121   }
122 
123   return SourceLocation;
124 }
125 
126 /// Serialize a source range with begin and end locations.
127 Object serializeSourceRange(const PresumedLoc &BeginLoc,
128                             const PresumedLoc &EndLoc) {
129   Object SourceRange;
130   serializeObject(SourceRange, "start", serializeSourcePosition(BeginLoc));
131   serializeObject(SourceRange, "end", serializeSourcePosition(EndLoc));
132   return SourceRange;
133 }
134 
135 /// Serialize the availability attributes of a symbol.
136 ///
137 /// Availability information contains the introduced, deprecated, and obsoleted
138 /// versions of the symbol as semantic versions, if not default.
139 /// Availability information also contains flags to indicate if the symbol is
140 /// unconditionally unavailable or deprecated,
141 /// i.e. \c __attribute__((unavailable)) and \c __attribute__((deprecated)).
142 ///
143 /// \returns \c None if the symbol has default availability attributes, or
144 /// an \c Object containing the formatted availability information.
145 Optional<Object> serializeAvailability(const AvailabilityInfo &Avail) {
146   if (Avail.isDefault())
147     return None;
148 
149   Object Availbility;
150   serializeObject(Availbility, "introducedVersion",
151                   serializeSemanticVersion(Avail.Introduced));
152   serializeObject(Availbility, "deprecatedVersion",
153                   serializeSemanticVersion(Avail.Deprecated));
154   serializeObject(Availbility, "obsoletedVersion",
155                   serializeSemanticVersion(Avail.Obsoleted));
156   if (Avail.isUnavailable())
157     Availbility["isUnconditionallyUnavailable"] = true;
158   if (Avail.isUnconditionallyDeprecated())
159     Availbility["isUnconditionallyDeprecated"] = true;
160 
161   return Availbility;
162 }
163 
164 /// Get the language name string for interface language references.
165 StringRef getLanguageName(Language Lang) {
166   switch (Lang) {
167   case Language::C:
168     return "c";
169   case Language::ObjC:
170     return "objective-c";
171 
172   // Unsupported language currently
173   case Language::CXX:
174   case Language::ObjCXX:
175   case Language::OpenCL:
176   case Language::OpenCLCXX:
177   case Language::CUDA:
178   case Language::RenderScript:
179   case Language::HIP:
180   case Language::HLSL:
181 
182   // Languages that the frontend cannot parse and compile
183   case Language::Unknown:
184   case Language::Asm:
185   case Language::LLVM_IR:
186     llvm_unreachable("Unsupported language kind");
187   }
188 
189   llvm_unreachable("Unhandled language kind");
190 }
191 
192 /// Serialize the identifier object as specified by the Symbol Graph format.
193 ///
194 /// The identifier property of a symbol contains the USR for precise and unique
195 /// references, and the interface language name.
196 Object serializeIdentifier(const APIRecord &Record, Language Lang) {
197   Object Identifier;
198   Identifier["precise"] = Record.USR;
199   Identifier["interfaceLanguage"] = getLanguageName(Lang);
200 
201   return Identifier;
202 }
203 
204 /// Serialize the documentation comments attached to a symbol, as specified by
205 /// the Symbol Graph format.
206 ///
207 /// The Symbol Graph \c docComment object contains an array of lines. Each line
208 /// represents one line of striped documentation comment, with source range
209 /// information.
210 /// e.g.
211 /// \code
212 ///   /// This is a documentation comment
213 ///       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'  First line.
214 ///   ///     with multiple lines.
215 ///       ^~~~~~~~~~~~~~~~~~~~~~~'         Second line.
216 /// \endcode
217 ///
218 /// \returns \c None if \p Comment is empty, or an \c Object containing the
219 /// formatted lines.
220 Optional<Object> serializeDocComment(const DocComment &Comment) {
221   if (Comment.empty())
222     return None;
223 
224   Object DocComment;
225   Array LinesArray;
226   for (const auto &CommentLine : Comment) {
227     Object Line;
228     Line["text"] = CommentLine.Text;
229     serializeObject(Line, "range",
230                     serializeSourceRange(CommentLine.Begin, CommentLine.End));
231     LinesArray.emplace_back(std::move(Line));
232   }
233   serializeArray(DocComment, "lines", LinesArray);
234 
235   return DocComment;
236 }
237 
238 /// Serialize the declaration fragments of a symbol.
239 ///
240 /// The Symbol Graph declaration fragments is an array of tagged important
241 /// parts of a symbol's declaration. The fragments sequence can be joined to
242 /// form spans of declaration text, with attached information useful for
243 /// purposes like syntax-highlighting etc. For example:
244 /// \code
245 ///   const int pi; -> "declarationFragments" : [
246 ///                      {
247 ///                        "kind" : "keyword",
248 ///                        "spelling" : "const"
249 ///                      },
250 ///                      {
251 ///                        "kind" : "text",
252 ///                        "spelling" : " "
253 ///                      },
254 ///                      {
255 ///                        "kind" : "typeIdentifier",
256 ///                        "preciseIdentifier" : "c:I",
257 ///                        "spelling" : "int"
258 ///                      },
259 ///                      {
260 ///                        "kind" : "text",
261 ///                        "spelling" : " "
262 ///                      },
263 ///                      {
264 ///                        "kind" : "identifier",
265 ///                        "spelling" : "pi"
266 ///                      }
267 ///                    ]
268 /// \endcode
269 ///
270 /// \returns \c None if \p DF is empty, or an \c Array containing the formatted
271 /// declaration fragments array.
272 Optional<Array> serializeDeclarationFragments(const DeclarationFragments &DF) {
273   if (DF.getFragments().empty())
274     return None;
275 
276   Array Fragments;
277   for (const auto &F : DF.getFragments()) {
278     Object Fragment;
279     Fragment["spelling"] = F.Spelling;
280     Fragment["kind"] = DeclarationFragments::getFragmentKindString(F.Kind);
281     if (!F.PreciseIdentifier.empty())
282       Fragment["preciseIdentifier"] = F.PreciseIdentifier;
283     Fragments.emplace_back(std::move(Fragment));
284   }
285 
286   return Fragments;
287 }
288 
289 /// Serialize the \c names field of a symbol as specified by the Symbol Graph
290 /// format.
291 ///
292 /// The Symbol Graph names field contains multiple representations of a symbol
293 /// that can be used for different applications:
294 ///   - \c title : The simple declared name of the symbol;
295 ///   - \c subHeading : An array of declaration fragments that provides tags,
296 ///     and potentially more tokens (for example the \c +/- symbol for
297 ///     Objective-C methods). Can be used as sub-headings for documentation.
298 Object serializeNames(const APIRecord &Record) {
299   Object Names;
300   Names["title"] = Record.Name;
301   serializeArray(Names, "subHeading",
302                  serializeDeclarationFragments(Record.SubHeading));
303   DeclarationFragments NavigatorFragments;
304   NavigatorFragments.append(Record.Name,
305                             DeclarationFragments::FragmentKind::Identifier,
306                             /*PreciseIdentifier*/ "");
307   serializeArray(Names, "navigator",
308                  serializeDeclarationFragments(NavigatorFragments));
309 
310   return Names;
311 }
312 
313 /// Serialize the symbol kind information.
314 ///
315 /// The Symbol Graph symbol kind property contains a shorthand \c identifier
316 /// which is prefixed by the source language name, useful for tooling to parse
317 /// the kind, and a \c displayName for rendering human-readable names.
318 Object serializeSymbolKind(const APIRecord &Record, Language Lang) {
319   auto AddLangPrefix = [&Lang](StringRef S) -> std::string {
320     return (getLanguageName(Lang) + "." + S).str();
321   };
322 
323   Object Kind;
324   switch (Record.getKind()) {
325   case APIRecord::RK_GlobalFunction:
326     Kind["identifier"] = AddLangPrefix("func");
327     Kind["displayName"] = "Function";
328     break;
329   case APIRecord::RK_GlobalVariable:
330     Kind["identifier"] = AddLangPrefix("var");
331     Kind["displayName"] = "Global Variable";
332     break;
333   case APIRecord::RK_EnumConstant:
334     Kind["identifier"] = AddLangPrefix("enum.case");
335     Kind["displayName"] = "Enumeration Case";
336     break;
337   case APIRecord::RK_Enum:
338     Kind["identifier"] = AddLangPrefix("enum");
339     Kind["displayName"] = "Enumeration";
340     break;
341   case APIRecord::RK_StructField:
342     Kind["identifier"] = AddLangPrefix("property");
343     Kind["displayName"] = "Instance Property";
344     break;
345   case APIRecord::RK_Struct:
346     Kind["identifier"] = AddLangPrefix("struct");
347     Kind["displayName"] = "Structure";
348     break;
349   case APIRecord::RK_ObjCIvar:
350     Kind["identifier"] = AddLangPrefix("ivar");
351     Kind["displayName"] = "Instance Variable";
352     break;
353   case APIRecord::RK_ObjCMethod:
354     if (cast<ObjCMethodRecord>(&Record)->IsInstanceMethod) {
355       Kind["identifier"] = AddLangPrefix("method");
356       Kind["displayName"] = "Instance Method";
357     } else {
358       Kind["identifier"] = AddLangPrefix("type.method");
359       Kind["displayName"] = "Type Method";
360     }
361     break;
362   case APIRecord::RK_ObjCProperty:
363     if (cast<ObjCPropertyRecord>(&Record)->isClassProperty()) {
364       Kind["identifier"] = AddLangPrefix("type.property");
365       Kind["displayName"] = "Type Property";
366     } else {
367       Kind["identifier"] = AddLangPrefix("property");
368       Kind["displayName"] = "Instance Property";
369     }
370     break;
371   case APIRecord::RK_ObjCInterface:
372     Kind["identifier"] = AddLangPrefix("class");
373     Kind["displayName"] = "Class";
374     break;
375   case APIRecord::RK_ObjCCategory:
376     // We don't serialize out standalone Objective-C category symbols yet.
377     llvm_unreachable("Serializing standalone Objective-C category symbols is "
378                      "not supported.");
379     break;
380   case APIRecord::RK_ObjCProtocol:
381     Kind["identifier"] = AddLangPrefix("protocol");
382     Kind["displayName"] = "Protocol";
383     break;
384   case APIRecord::RK_MacroDefinition:
385     Kind["identifier"] = AddLangPrefix("macro");
386     Kind["displayName"] = "Macro";
387     break;
388   case APIRecord::RK_Typedef:
389     Kind["identifier"] = AddLangPrefix("typealias");
390     Kind["displayName"] = "Type Alias";
391     break;
392   }
393 
394   return Kind;
395 }
396 
397 template <typename RecordTy>
398 Optional<Object> serializeFunctionSignatureMixinImpl(const RecordTy &Record,
399                                                      std::true_type) {
400   const auto &FS = Record.Signature;
401   if (FS.empty())
402     return None;
403 
404   Object Signature;
405   serializeArray(Signature, "returns",
406                  serializeDeclarationFragments(FS.getReturnType()));
407 
408   Array Parameters;
409   for (const auto &P : FS.getParameters()) {
410     Object Parameter;
411     Parameter["name"] = P.Name;
412     serializeArray(Parameter, "declarationFragments",
413                    serializeDeclarationFragments(P.Fragments));
414     Parameters.emplace_back(std::move(Parameter));
415   }
416 
417   if (!Parameters.empty())
418     Signature["parameters"] = std::move(Parameters);
419 
420   return Signature;
421 }
422 
423 template <typename RecordTy>
424 Optional<Object> serializeFunctionSignatureMixinImpl(const RecordTy &Record,
425                                                      std::false_type) {
426   return None;
427 }
428 
429 /// Serialize the function signature field, as specified by the
430 /// Symbol Graph format.
431 ///
432 /// The Symbol Graph function signature property contains two arrays.
433 ///   - The \c returns array is the declaration fragments of the return type;
434 ///   - The \c parameters array contains names and declaration fragments of the
435 ///     parameters.
436 ///
437 /// \returns \c None if \p FS is empty, or an \c Object containing the
438 /// formatted function signature.
439 template <typename RecordTy>
440 void serializeFunctionSignatureMixin(Object &Paren, const RecordTy &Record) {
441   serializeObject(Paren, "functionSignature",
442                   serializeFunctionSignatureMixinImpl(
443                       Record, has_function_signature<RecordTy>()));
444 }
445 
446 } // namespace
447 
448 void SymbolGraphSerializer::anchor() {}
449 
450 /// Defines the format version emitted by SymbolGraphSerializer.
451 const VersionTuple SymbolGraphSerializer::FormatVersion{0, 5, 3};
452 
453 Object SymbolGraphSerializer::serializeMetadata() const {
454   Object Metadata;
455   serializeObject(Metadata, "formatVersion",
456                   serializeSemanticVersion(FormatVersion));
457   Metadata["generator"] = clang::getClangFullVersion();
458   return Metadata;
459 }
460 
461 Object SymbolGraphSerializer::serializeModule() const {
462   Object Module;
463   // The user is expected to always pass `--product-name=` on the command line
464   // to populate this field.
465   Module["name"] = ProductName;
466   serializeObject(Module, "platform", serializePlatform(API.getTarget()));
467   return Module;
468 }
469 
470 bool SymbolGraphSerializer::shouldSkip(const APIRecord &Record) const {
471   // Skip unconditionally unavailable symbols
472   if (Record.Availability.isUnconditionallyUnavailable())
473     return true;
474 
475   // Filter out symbols prefixed with an underscored as they are understood to
476   // be symbols clients should not use.
477   if (Record.Name.startswith("_"))
478     return true;
479 
480   return false;
481 }
482 
483 template <typename RecordTy>
484 Optional<Object>
485 SymbolGraphSerializer::serializeAPIRecord(const RecordTy &Record) const {
486   if (shouldSkip(Record))
487     return None;
488 
489   Object Obj;
490   serializeObject(Obj, "identifier",
491                   serializeIdentifier(Record, API.getLanguage()));
492   serializeObject(Obj, "kind", serializeSymbolKind(Record, API.getLanguage()));
493   serializeObject(Obj, "names", serializeNames(Record));
494   serializeObject(
495       Obj, "location",
496       serializeSourceLocation(Record.Location, /*IncludeFileURI=*/true));
497   serializeObject(Obj, "availbility",
498                   serializeAvailability(Record.Availability));
499   serializeObject(Obj, "docComment", serializeDocComment(Record.Comment));
500   serializeArray(Obj, "declarationFragments",
501                  serializeDeclarationFragments(Record.Declaration));
502   // TODO: Once we keep track of symbol access information serialize it
503   // correctly here.
504   Obj["accessLevel"] = "public";
505   serializeArray(Obj, "pathComponents", Array(PathComponents));
506 
507   serializeFunctionSignatureMixin(Obj, Record);
508 
509   return Obj;
510 }
511 
512 template <typename MemberTy>
513 void SymbolGraphSerializer::serializeMembers(
514     const APIRecord &Record,
515     const SmallVector<std::unique_ptr<MemberTy>> &Members) {
516   for (const auto &Member : Members) {
517     auto MemberPathComponentGuard = makePathComponentGuard(Member->Name);
518     auto MemberRecord = serializeAPIRecord(*Member);
519     if (!MemberRecord)
520       continue;
521 
522     Symbols.emplace_back(std::move(*MemberRecord));
523     serializeRelationship(RelationshipKind::MemberOf, *Member, Record);
524   }
525 }
526 
527 StringRef SymbolGraphSerializer::getRelationshipString(RelationshipKind Kind) {
528   switch (Kind) {
529   case RelationshipKind::MemberOf:
530     return "memberOf";
531   case RelationshipKind::InheritsFrom:
532     return "inheritsFrom";
533   case RelationshipKind::ConformsTo:
534     return "conformsTo";
535   }
536   llvm_unreachable("Unhandled relationship kind");
537 }
538 
539 void SymbolGraphSerializer::serializeRelationship(RelationshipKind Kind,
540                                                   SymbolReference Source,
541                                                   SymbolReference Target) {
542   Object Relationship;
543   Relationship["source"] = Source.USR;
544   Relationship["target"] = Target.USR;
545   Relationship["kind"] = getRelationshipString(Kind);
546 
547   Relationships.emplace_back(std::move(Relationship));
548 }
549 
550 void SymbolGraphSerializer::serializeGlobalFunctionRecord(
551     const GlobalFunctionRecord &Record) {
552   auto GlobalPathComponentGuard = makePathComponentGuard(Record.Name);
553 
554   auto Obj = serializeAPIRecord(Record);
555   if (!Obj)
556     return;
557 
558   Symbols.emplace_back(std::move(*Obj));
559 }
560 
561 void SymbolGraphSerializer::serializeGlobalVariableRecord(
562     const GlobalVariableRecord &Record) {
563   auto GlobalPathComponentGuard = makePathComponentGuard(Record.Name);
564 
565   auto Obj = serializeAPIRecord(Record);
566   if (!Obj)
567     return;
568 
569   Symbols.emplace_back(std::move(*Obj));
570 }
571 
572 void SymbolGraphSerializer::serializeEnumRecord(const EnumRecord &Record) {
573   auto EnumPathComponentGuard = makePathComponentGuard(Record.Name);
574   auto Enum = serializeAPIRecord(Record);
575   if (!Enum)
576     return;
577 
578   Symbols.emplace_back(std::move(*Enum));
579   serializeMembers(Record, Record.Constants);
580 }
581 
582 void SymbolGraphSerializer::serializeStructRecord(const StructRecord &Record) {
583   auto StructPathComponentGuard = makePathComponentGuard(Record.Name);
584   auto Struct = serializeAPIRecord(Record);
585   if (!Struct)
586     return;
587 
588   Symbols.emplace_back(std::move(*Struct));
589   serializeMembers(Record, Record.Fields);
590 }
591 
592 void SymbolGraphSerializer::serializeObjCContainerRecord(
593     const ObjCContainerRecord &Record) {
594   auto ObjCContainerPathComponentGuard = makePathComponentGuard(Record.Name);
595   auto ObjCContainer = serializeAPIRecord(Record);
596   if (!ObjCContainer)
597     return;
598 
599   Symbols.emplace_back(std::move(*ObjCContainer));
600 
601   serializeMembers(Record, Record.Ivars);
602   serializeMembers(Record, Record.Methods);
603   serializeMembers(Record, Record.Properties);
604 
605   for (const auto &Protocol : Record.Protocols)
606     // Record that Record conforms to Protocol.
607     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
608 
609   if (auto *ObjCInterface = dyn_cast<ObjCInterfaceRecord>(&Record)) {
610     if (!ObjCInterface->SuperClass.empty())
611       // If Record is an Objective-C interface record and it has a super class,
612       // record that Record is inherited from SuperClass.
613       serializeRelationship(RelationshipKind::InheritsFrom, Record,
614                             ObjCInterface->SuperClass);
615 
616     // Members of categories extending an interface are serialized as members of
617     // the interface.
618     for (const auto *Category : ObjCInterface->Categories) {
619       serializeMembers(Record, Category->Ivars);
620       serializeMembers(Record, Category->Methods);
621       serializeMembers(Record, Category->Properties);
622 
623       // Surface the protocols of the the category to the interface.
624       for (const auto &Protocol : Category->Protocols)
625         serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
626     }
627   }
628 }
629 
630 void SymbolGraphSerializer::serializeMacroDefinitionRecord(
631     const MacroDefinitionRecord &Record) {
632   auto MacroPathComponentGuard = makePathComponentGuard(Record.Name);
633   auto Macro = serializeAPIRecord(Record);
634 
635   if (!Macro)
636     return;
637 
638   Symbols.emplace_back(std::move(*Macro));
639 }
640 
641 void SymbolGraphSerializer::serializeTypedefRecord(
642     const TypedefRecord &Record) {
643   // Typedefs of anonymous types have their entries unified with the underlying
644   // type.
645   bool ShouldDrop = Record.UnderlyingType.Name.empty();
646   // enums declared with `NS_OPTION` have a named enum and a named typedef, with
647   // the same name
648   ShouldDrop |= (Record.UnderlyingType.Name == Record.Name);
649   if (ShouldDrop)
650     return;
651 
652   auto TypedefPathComponentGuard = makePathComponentGuard(Record.Name);
653   auto Typedef = serializeAPIRecord(Record);
654   if (!Typedef)
655     return;
656 
657   (*Typedef)["type"] = Record.UnderlyingType.USR;
658 
659   Symbols.emplace_back(std::move(*Typedef));
660 }
661 
662 SymbolGraphSerializer::PathComponentGuard
663 SymbolGraphSerializer::makePathComponentGuard(StringRef Component) {
664   return PathComponentGuard(PathComponents, Component);
665 }
666 
667 Object SymbolGraphSerializer::serialize() {
668   Object Root;
669   serializeObject(Root, "metadata", serializeMetadata());
670   serializeObject(Root, "module", serializeModule());
671 
672   // Serialize global variables in the API set.
673   for (const auto &GlobalVar : API.getGlobalVariables())
674     serializeGlobalVariableRecord(*GlobalVar.second);
675 
676   for (const auto &GlobalFunction : API.getGlobalFunctions())
677     serializeGlobalFunctionRecord(*GlobalFunction.second);
678 
679   // Serialize enum records in the API set.
680   for (const auto &Enum : API.getEnums())
681     serializeEnumRecord(*Enum.second);
682 
683   // Serialize struct records in the API set.
684   for (const auto &Struct : API.getStructs())
685     serializeStructRecord(*Struct.second);
686 
687   // Serialize Objective-C interface records in the API set.
688   for (const auto &ObjCInterface : API.getObjCInterfaces())
689     serializeObjCContainerRecord(*ObjCInterface.second);
690 
691   // Serialize Objective-C protocol records in the API set.
692   for (const auto &ObjCProtocol : API.getObjCProtocols())
693     serializeObjCContainerRecord(*ObjCProtocol.second);
694 
695   for (const auto &Macro : API.getMacros())
696     serializeMacroDefinitionRecord(*Macro.second);
697 
698   for (const auto &Typedef : API.getTypedefs())
699     serializeTypedefRecord(*Typedef.second);
700 
701   Root["symbols"] = std::move(Symbols);
702   Root["relationships"] = std::move(Relationships);
703 
704   return Root;
705 }
706 
707 void SymbolGraphSerializer::serialize(raw_ostream &os) {
708   Object root = serialize();
709   if (Options.Compact)
710     os << formatv("{0}", Value(std::move(root))) << "\n";
711   else
712     os << formatv("{0:2}", Value(std::move(root))) << "\n";
713 }
714