xref: /llvm-project/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp (revision 236b6a0eb41a163510e65664e2160f599287326b)
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.getValue());
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.getValue());
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().getValueOr(0);
67   Version["patch"] = V.getSubminor().getValueOr(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 (dyn_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     Kind["identifier"] = AddLangPrefix("property");
364     Kind["displayName"] = "Instance Property";
365     break;
366   case APIRecord::RK_ObjCInterface:
367     Kind["identifier"] = AddLangPrefix("class");
368     Kind["displayName"] = "Class";
369     break;
370   case APIRecord::RK_ObjCCategory:
371     // We don't serialize out standalone Objective-C category symbols yet.
372     llvm_unreachable("Serializing standalone Objective-C category symbols is "
373                      "not supported.");
374     break;
375   case APIRecord::RK_ObjCProtocol:
376     Kind["identifier"] = AddLangPrefix("protocol");
377     Kind["displayName"] = "Protocol";
378     break;
379   case APIRecord::RK_MacroDefinition:
380     Kind["identifier"] = AddLangPrefix("macro");
381     Kind["displayName"] = "Macro";
382     break;
383   case APIRecord::RK_Typedef:
384     Kind["identifier"] = AddLangPrefix("typealias");
385     Kind["displayName"] = "Type Alias";
386     break;
387   }
388 
389   return Kind;
390 }
391 
392 template <typename RecordTy>
393 Optional<Object> serializeFunctionSignatureMixinImpl(const RecordTy &Record,
394                                                      std::true_type) {
395   const auto &FS = Record.Signature;
396   if (FS.empty())
397     return None;
398 
399   Object Signature;
400   serializeArray(Signature, "returns",
401                  serializeDeclarationFragments(FS.getReturnType()));
402 
403   Array Parameters;
404   for (const auto &P : FS.getParameters()) {
405     Object Parameter;
406     Parameter["name"] = P.Name;
407     serializeArray(Parameter, "declarationFragments",
408                    serializeDeclarationFragments(P.Fragments));
409     Parameters.emplace_back(std::move(Parameter));
410   }
411 
412   if (!Parameters.empty())
413     Signature["parameters"] = std::move(Parameters);
414 
415   return Signature;
416 }
417 
418 template <typename RecordTy>
419 Optional<Object> serializeFunctionSignatureMixinImpl(const RecordTy &Record,
420                                                      std::false_type) {
421   return None;
422 }
423 
424 /// Serialize the function signature field, as specified by the
425 /// Symbol Graph format.
426 ///
427 /// The Symbol Graph function signature property contains two arrays.
428 ///   - The \c returns array is the declaration fragments of the return type;
429 ///   - The \c parameters array contains names and declaration fragments of the
430 ///     parameters.
431 ///
432 /// \returns \c None if \p FS is empty, or an \c Object containing the
433 /// formatted function signature.
434 template <typename RecordTy>
435 void serializeFunctionSignatureMixin(Object &Paren, const RecordTy &Record) {
436   serializeObject(Paren, "functionSignature",
437                   serializeFunctionSignatureMixinImpl(
438                       Record, has_function_signature<RecordTy>()));
439 }
440 
441 } // namespace
442 
443 void SymbolGraphSerializer::anchor() {}
444 
445 /// Defines the format version emitted by SymbolGraphSerializer.
446 const VersionTuple SymbolGraphSerializer::FormatVersion{0, 5, 3};
447 
448 Object SymbolGraphSerializer::serializeMetadata() const {
449   Object Metadata;
450   serializeObject(Metadata, "formatVersion",
451                   serializeSemanticVersion(FormatVersion));
452   Metadata["generator"] = clang::getClangFullVersion();
453   return Metadata;
454 }
455 
456 Object SymbolGraphSerializer::serializeModule() const {
457   Object Module;
458   // The user is expected to always pass `--product-name=` on the command line
459   // to populate this field.
460   Module["name"] = ProductName;
461   serializeObject(Module, "platform", serializePlatform(API.getTarget()));
462   return Module;
463 }
464 
465 bool SymbolGraphSerializer::shouldSkip(const APIRecord &Record) const {
466   // Skip unconditionally unavailable symbols
467   if (Record.Availability.isUnconditionallyUnavailable())
468     return true;
469 
470   return false;
471 }
472 
473 template <typename RecordTy>
474 Optional<Object>
475 SymbolGraphSerializer::serializeAPIRecord(const RecordTy &Record) const {
476   if (shouldSkip(Record))
477     return None;
478 
479   Object Obj;
480   serializeObject(Obj, "identifier",
481                   serializeIdentifier(Record, API.getLanguage()));
482   serializeObject(Obj, "kind", serializeSymbolKind(Record, API.getLanguage()));
483   serializeObject(Obj, "names", serializeNames(Record));
484   serializeObject(
485       Obj, "location",
486       serializeSourceLocation(Record.Location, /*IncludeFileURI=*/true));
487   serializeObject(Obj, "availbility",
488                   serializeAvailability(Record.Availability));
489   serializeObject(Obj, "docComment", serializeDocComment(Record.Comment));
490   serializeArray(Obj, "declarationFragments",
491                  serializeDeclarationFragments(Record.Declaration));
492   // TODO: Once we keep track of symbol access information serialize it
493   // correctly here.
494   Obj["accessLevel"] = "public";
495   serializeArray(Obj, "pathComponents", Array(PathComponents));
496 
497   serializeFunctionSignatureMixin(Obj, Record);
498 
499   return Obj;
500 }
501 
502 template <typename MemberTy>
503 void SymbolGraphSerializer::serializeMembers(
504     const APIRecord &Record,
505     const SmallVector<std::unique_ptr<MemberTy>> &Members) {
506   for (const auto &Member : Members) {
507     auto MemberPathComponentGuard = makePathComponentGuard(Member->Name);
508     auto MemberRecord = serializeAPIRecord(*Member);
509     if (!MemberRecord)
510       continue;
511 
512     Symbols.emplace_back(std::move(*MemberRecord));
513     serializeRelationship(RelationshipKind::MemberOf, *Member, Record);
514   }
515 }
516 
517 StringRef SymbolGraphSerializer::getRelationshipString(RelationshipKind Kind) {
518   switch (Kind) {
519   case RelationshipKind::MemberOf:
520     return "memberOf";
521   case RelationshipKind::InheritsFrom:
522     return "inheritsFrom";
523   case RelationshipKind::ConformsTo:
524     return "conformsTo";
525   }
526   llvm_unreachable("Unhandled relationship kind");
527 }
528 
529 void SymbolGraphSerializer::serializeRelationship(RelationshipKind Kind,
530                                                   SymbolReference Source,
531                                                   SymbolReference Target) {
532   Object Relationship;
533   Relationship["source"] = Source.USR;
534   Relationship["target"] = Target.USR;
535   Relationship["kind"] = getRelationshipString(Kind);
536 
537   Relationships.emplace_back(std::move(Relationship));
538 }
539 
540 void SymbolGraphSerializer::serializeGlobalFunctionRecord(
541     const GlobalFunctionRecord &Record) {
542   auto GlobalPathComponentGuard = makePathComponentGuard(Record.Name);
543 
544   auto Obj = serializeAPIRecord(Record);
545   if (!Obj)
546     return;
547 
548   Symbols.emplace_back(std::move(*Obj));
549 }
550 
551 void SymbolGraphSerializer::serializeGlobalVariableRecord(
552     const GlobalVariableRecord &Record) {
553   auto GlobalPathComponentGuard = makePathComponentGuard(Record.Name);
554 
555   auto Obj = serializeAPIRecord(Record);
556   if (!Obj)
557     return;
558 
559   Symbols.emplace_back(std::move(*Obj));
560 }
561 
562 void SymbolGraphSerializer::serializeEnumRecord(const EnumRecord &Record) {
563   auto EnumPathComponentGuard = makePathComponentGuard(Record.Name);
564   auto Enum = serializeAPIRecord(Record);
565   if (!Enum)
566     return;
567 
568   Symbols.emplace_back(std::move(*Enum));
569   serializeMembers(Record, Record.Constants);
570 }
571 
572 void SymbolGraphSerializer::serializeStructRecord(const StructRecord &Record) {
573   auto StructPathComponentGuard = makePathComponentGuard(Record.Name);
574   auto Struct = serializeAPIRecord(Record);
575   if (!Struct)
576     return;
577 
578   Symbols.emplace_back(std::move(*Struct));
579   serializeMembers(Record, Record.Fields);
580 }
581 
582 void SymbolGraphSerializer::serializeObjCContainerRecord(
583     const ObjCContainerRecord &Record) {
584   auto ObjCContainerPathComponentGuard = makePathComponentGuard(Record.Name);
585   auto ObjCContainer = serializeAPIRecord(Record);
586   if (!ObjCContainer)
587     return;
588 
589   Symbols.emplace_back(std::move(*ObjCContainer));
590 
591   serializeMembers(Record, Record.Ivars);
592   serializeMembers(Record, Record.Methods);
593   serializeMembers(Record, Record.Properties);
594 
595   for (const auto &Protocol : Record.Protocols)
596     // Record that Record conforms to Protocol.
597     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
598 
599   if (auto *ObjCInterface = dyn_cast<ObjCInterfaceRecord>(&Record)) {
600     if (!ObjCInterface->SuperClass.empty())
601       // If Record is an Objective-C interface record and it has a super class,
602       // record that Record is inherited from SuperClass.
603       serializeRelationship(RelationshipKind::InheritsFrom, Record,
604                             ObjCInterface->SuperClass);
605 
606     // Members of categories extending an interface are serialized as members of
607     // the interface.
608     for (const auto *Category : ObjCInterface->Categories) {
609       serializeMembers(Record, Category->Ivars);
610       serializeMembers(Record, Category->Methods);
611       serializeMembers(Record, Category->Properties);
612 
613       // Surface the protocols of the the category to the interface.
614       for (const auto &Protocol : Category->Protocols)
615         serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
616     }
617   }
618 }
619 
620 void SymbolGraphSerializer::serializeMacroDefinitionRecord(
621     const MacroDefinitionRecord &Record) {
622   auto MacroPathComponentGuard = makePathComponentGuard(Record.Name);
623   auto Macro = serializeAPIRecord(Record);
624 
625   if (!Macro)
626     return;
627 
628   Symbols.emplace_back(std::move(*Macro));
629 }
630 
631 void SymbolGraphSerializer::serializeTypedefRecord(
632     const TypedefRecord &Record) {
633   // Typedefs of anonymous types have their entries unified with the underlying
634   // type.
635   bool ShouldDrop = Record.UnderlyingType.Name.empty();
636   // enums declared with `NS_OPTION` have a named enum and a named typedef, with
637   // the same name
638   ShouldDrop |= (Record.UnderlyingType.Name == Record.Name);
639   if (ShouldDrop)
640     return;
641 
642   auto TypedefPathComponentGuard = makePathComponentGuard(Record.Name);
643   auto Typedef = serializeAPIRecord(Record);
644   if (!Typedef)
645     return;
646 
647   (*Typedef)["type"] = Record.UnderlyingType.USR;
648 
649   Symbols.emplace_back(std::move(*Typedef));
650 }
651 
652 SymbolGraphSerializer::PathComponentGuard
653 SymbolGraphSerializer::makePathComponentGuard(StringRef Component) {
654   return PathComponentGuard(PathComponents, Component);
655 }
656 
657 Object SymbolGraphSerializer::serialize() {
658   Object Root;
659   serializeObject(Root, "metadata", serializeMetadata());
660   serializeObject(Root, "module", serializeModule());
661 
662   // Serialize global variables in the API set.
663   for (const auto &GlobalVar : API.getGlobalVariables())
664     serializeGlobalVariableRecord(*GlobalVar.second);
665 
666   for (const auto &GlobalFunction : API.getGlobalFunctions())
667     serializeGlobalFunctionRecord(*GlobalFunction.second);
668 
669   // Serialize enum records in the API set.
670   for (const auto &Enum : API.getEnums())
671     serializeEnumRecord(*Enum.second);
672 
673   // Serialize struct records in the API set.
674   for (const auto &Struct : API.getStructs())
675     serializeStructRecord(*Struct.second);
676 
677   // Serialize Objective-C interface records in the API set.
678   for (const auto &ObjCInterface : API.getObjCInterfaces())
679     serializeObjCContainerRecord(*ObjCInterface.second);
680 
681   // Serialize Objective-C protocol records in the API set.
682   for (const auto &ObjCProtocol : API.getObjCProtocols())
683     serializeObjCContainerRecord(*ObjCProtocol.second);
684 
685   for (const auto &Macro : API.getMacros())
686     serializeMacroDefinitionRecord(*Macro.second);
687 
688   for (const auto &Typedef : API.getTypedefs())
689     serializeTypedefRecord(*Typedef.second);
690 
691   Root["symbols"] = std::move(Symbols);
692   Root["relationships"] = std::move(Relationships);
693 
694   return Root;
695 }
696 
697 void SymbolGraphSerializer::serialize(raw_ostream &os) {
698   Object root = serialize();
699   if (Options.Compact)
700     os << formatv("{0}", Value(std::move(root))) << "\n";
701   else
702     os << formatv("{0:2}", Value(std::move(root))) << "\n";
703 }
704