189f6b26fSZixu Wang //===- ExtractAPI/Serialization/SymbolGraphSerializer.cpp -------*- C++ -*-===// 289f6b26fSZixu Wang // 389f6b26fSZixu Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 489f6b26fSZixu Wang // See https://llvm.org/LICENSE.txt for license information. 589f6b26fSZixu Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 689f6b26fSZixu Wang // 789f6b26fSZixu Wang //===----------------------------------------------------------------------===// 889f6b26fSZixu Wang /// 989f6b26fSZixu Wang /// \file 1089f6b26fSZixu Wang /// This file implements the SymbolGraphSerializer. 1189f6b26fSZixu Wang /// 1289f6b26fSZixu Wang //===----------------------------------------------------------------------===// 1389f6b26fSZixu Wang 1489f6b26fSZixu Wang #include "clang/ExtractAPI/Serialization/SymbolGraphSerializer.h" 1589f6b26fSZixu Wang #include "clang/Basic/Version.h" 1689f6b26fSZixu Wang #include "clang/ExtractAPI/API.h" 1789f6b26fSZixu Wang #include "llvm/Support/JSON.h" 1889f6b26fSZixu Wang #include "llvm/Support/Path.h" 1989f6b26fSZixu Wang #include "llvm/Support/VersionTuple.h" 2089f6b26fSZixu Wang 2189f6b26fSZixu Wang using namespace clang; 2289f6b26fSZixu Wang using namespace clang::extractapi; 2389f6b26fSZixu Wang using namespace llvm; 2489f6b26fSZixu Wang using namespace llvm::json; 2589f6b26fSZixu Wang 2689f6b26fSZixu Wang namespace { 2789f6b26fSZixu Wang 2889f6b26fSZixu Wang /// Helper function to inject a JSON object \p Obj into another object \p Paren 2989f6b26fSZixu Wang /// at position \p Key. 3089f6b26fSZixu Wang void serializeObject(Object &Paren, StringRef Key, Optional<Object> Obj) { 3189f6b26fSZixu Wang if (Obj) 3289f6b26fSZixu Wang Paren[Key] = std::move(Obj.getValue()); 3389f6b26fSZixu Wang } 3489f6b26fSZixu Wang 3589f6b26fSZixu Wang /// Helper function to inject a JSON array \p Array into object \p Paren at 3689f6b26fSZixu Wang /// position \p Key. 3789f6b26fSZixu Wang void serializeArray(Object &Paren, StringRef Key, Optional<Array> Array) { 3889f6b26fSZixu Wang if (Array) 3989f6b26fSZixu Wang Paren[Key] = std::move(Array.getValue()); 4089f6b26fSZixu Wang } 4189f6b26fSZixu Wang 4289f6b26fSZixu Wang /// Serialize a \c VersionTuple \p V with the Symbol Graph semantic version 4389f6b26fSZixu Wang /// format. 4489f6b26fSZixu Wang /// 4589f6b26fSZixu Wang /// A semantic version object contains three numeric fields, representing the 4689f6b26fSZixu Wang /// \c major, \c minor, and \c patch parts of the version tuple. 4789f6b26fSZixu Wang /// For example version tuple 1.0.3 is serialized as: 4889f6b26fSZixu Wang /// \code 4989f6b26fSZixu Wang /// { 5089f6b26fSZixu Wang /// "major" : 1, 5189f6b26fSZixu Wang /// "minor" : 0, 5289f6b26fSZixu Wang /// "patch" : 3 5389f6b26fSZixu Wang /// } 5489f6b26fSZixu Wang /// \endcode 5589f6b26fSZixu Wang /// 5689f6b26fSZixu Wang /// \returns \c None if the version \p V is empty, or an \c Object containing 5789f6b26fSZixu Wang /// the semantic version representation of \p V. 5889f6b26fSZixu Wang Optional<Object> serializeSemanticVersion(const VersionTuple &V) { 5989f6b26fSZixu Wang if (V.empty()) 6089f6b26fSZixu Wang return None; 6189f6b26fSZixu Wang 6289f6b26fSZixu Wang Object Version; 6389f6b26fSZixu Wang Version["major"] = V.getMajor(); 6489f6b26fSZixu Wang Version["minor"] = V.getMinor().getValueOr(0); 6589f6b26fSZixu Wang Version["patch"] = V.getSubminor().getValueOr(0); 6689f6b26fSZixu Wang return Version; 6789f6b26fSZixu Wang } 6889f6b26fSZixu Wang 6989f6b26fSZixu Wang /// Serialize the OS information in the Symbol Graph platform property. 7089f6b26fSZixu Wang /// 7189f6b26fSZixu Wang /// The OS information in Symbol Graph contains the \c name of the OS, and an 7289f6b26fSZixu Wang /// optional \c minimumVersion semantic version field. 7389f6b26fSZixu Wang Object serializeOperatingSystem(const Triple &T) { 7489f6b26fSZixu Wang Object OS; 7589f6b26fSZixu Wang OS["name"] = T.getOSTypeName(T.getOS()); 7689f6b26fSZixu Wang serializeObject(OS, "minimumVersion", 7789f6b26fSZixu Wang serializeSemanticVersion(T.getMinimumSupportedOSVersion())); 7889f6b26fSZixu Wang return OS; 7989f6b26fSZixu Wang } 8089f6b26fSZixu Wang 8189f6b26fSZixu Wang /// Serialize the platform information in the Symbol Graph module section. 8289f6b26fSZixu Wang /// 8389f6b26fSZixu Wang /// The platform object describes a target platform triple in corresponding 8489f6b26fSZixu Wang /// three fields: \c architecture, \c vendor, and \c operatingSystem. 8589f6b26fSZixu Wang Object serializePlatform(const Triple &T) { 8689f6b26fSZixu Wang Object Platform; 8789f6b26fSZixu Wang Platform["architecture"] = T.getArchName(); 8889f6b26fSZixu Wang Platform["vendor"] = T.getVendorName(); 8989f6b26fSZixu Wang Platform["operatingSystem"] = serializeOperatingSystem(T); 9089f6b26fSZixu Wang return Platform; 9189f6b26fSZixu Wang } 9289f6b26fSZixu Wang 9389f6b26fSZixu Wang /// Serialize a source location in file. 9489f6b26fSZixu Wang /// 9589f6b26fSZixu Wang /// \param Loc The presumed location to serialize. 9689f6b26fSZixu Wang /// \param IncludeFileURI If true, include the file path of \p Loc as a URI. 9789f6b26fSZixu Wang /// Defaults to false. 9889f6b26fSZixu Wang Object serializeSourcePosition(const PresumedLoc &Loc, 9989f6b26fSZixu Wang bool IncludeFileURI = false) { 10089f6b26fSZixu Wang assert(Loc.isValid() && "invalid source position"); 10189f6b26fSZixu Wang 10289f6b26fSZixu Wang Object SourcePosition; 10389f6b26fSZixu Wang SourcePosition["line"] = Loc.getLine(); 10489f6b26fSZixu Wang SourcePosition["character"] = Loc.getColumn(); 10589f6b26fSZixu Wang 10689f6b26fSZixu Wang if (IncludeFileURI) { 10789f6b26fSZixu Wang std::string FileURI = "file://"; 10889f6b26fSZixu Wang // Normalize file path to use forward slashes for the URI. 10989f6b26fSZixu Wang FileURI += sys::path::convert_to_slash(Loc.getFilename()); 11089f6b26fSZixu Wang SourcePosition["uri"] = FileURI; 11189f6b26fSZixu Wang } 11289f6b26fSZixu Wang 11389f6b26fSZixu Wang return SourcePosition; 11489f6b26fSZixu Wang } 11589f6b26fSZixu Wang 11689f6b26fSZixu Wang /// Serialize a source range with begin and end locations. 11789f6b26fSZixu Wang Object serializeSourceRange(const PresumedLoc &BeginLoc, 11889f6b26fSZixu Wang const PresumedLoc &EndLoc) { 11989f6b26fSZixu Wang Object SourceRange; 12089f6b26fSZixu Wang serializeObject(SourceRange, "start", serializeSourcePosition(BeginLoc)); 12189f6b26fSZixu Wang serializeObject(SourceRange, "end", serializeSourcePosition(EndLoc)); 12289f6b26fSZixu Wang return SourceRange; 12389f6b26fSZixu Wang } 12489f6b26fSZixu Wang 12589f6b26fSZixu Wang /// Serialize the availability attributes of a symbol. 12689f6b26fSZixu Wang /// 12789f6b26fSZixu Wang /// Availability information contains the introduced, deprecated, and obsoleted 12889f6b26fSZixu Wang /// versions of the symbol as semantic versions, if not default. 12989f6b26fSZixu Wang /// Availability information also contains flags to indicate if the symbol is 13089f6b26fSZixu Wang /// unconditionally unavailable or deprecated, 13189f6b26fSZixu Wang /// i.e. \c __attribute__((unavailable)) and \c __attribute__((deprecated)). 13289f6b26fSZixu Wang /// 13389f6b26fSZixu Wang /// \returns \c None if the symbol has default availability attributes, or 13489f6b26fSZixu Wang /// an \c Object containing the formatted availability information. 13589f6b26fSZixu Wang Optional<Object> serializeAvailability(const AvailabilityInfo &Avail) { 13689f6b26fSZixu Wang if (Avail.isDefault()) 13789f6b26fSZixu Wang return None; 13889f6b26fSZixu Wang 13989f6b26fSZixu Wang Object Availbility; 14089f6b26fSZixu Wang serializeObject(Availbility, "introducedVersion", 14189f6b26fSZixu Wang serializeSemanticVersion(Avail.Introduced)); 14289f6b26fSZixu Wang serializeObject(Availbility, "deprecatedVersion", 14389f6b26fSZixu Wang serializeSemanticVersion(Avail.Deprecated)); 14489f6b26fSZixu Wang serializeObject(Availbility, "obsoletedVersion", 14589f6b26fSZixu Wang serializeSemanticVersion(Avail.Obsoleted)); 14689f6b26fSZixu Wang if (Avail.isUnavailable()) 14789f6b26fSZixu Wang Availbility["isUnconditionallyUnavailable"] = true; 14889f6b26fSZixu Wang if (Avail.isUnconditionallyDeprecated()) 14989f6b26fSZixu Wang Availbility["isUnconditionallyDeprecated"] = true; 15089f6b26fSZixu Wang 15189f6b26fSZixu Wang return Availbility; 15289f6b26fSZixu Wang } 15389f6b26fSZixu Wang 15489f6b26fSZixu Wang /// Get the short language name string for interface language references. 15589f6b26fSZixu Wang StringRef getLanguageName(const LangOptions &LangOpts) { 15689f6b26fSZixu Wang auto Language = 15789f6b26fSZixu Wang LangStandard::getLangStandardForKind(LangOpts.LangStd).getLanguage(); 15889f6b26fSZixu Wang switch (Language) { 15989f6b26fSZixu Wang case Language::C: 16089f6b26fSZixu Wang return "c"; 16189f6b26fSZixu Wang case Language::ObjC: 162b62d4021SZixu Wang return "objective-c"; 16389f6b26fSZixu Wang 16489f6b26fSZixu Wang // Unsupported language currently 16589f6b26fSZixu Wang case Language::CXX: 16689f6b26fSZixu Wang case Language::ObjCXX: 16789f6b26fSZixu Wang case Language::OpenCL: 16889f6b26fSZixu Wang case Language::OpenCLCXX: 16989f6b26fSZixu Wang case Language::CUDA: 17089f6b26fSZixu Wang case Language::RenderScript: 17189f6b26fSZixu Wang case Language::HIP: 17289f6b26fSZixu Wang 17389f6b26fSZixu Wang // Languages that the frontend cannot parse and compile 17489f6b26fSZixu Wang case Language::Unknown: 17589f6b26fSZixu Wang case Language::Asm: 17689f6b26fSZixu Wang case Language::LLVM_IR: 17789f6b26fSZixu Wang llvm_unreachable("Unsupported language kind"); 17889f6b26fSZixu Wang } 17989f6b26fSZixu Wang 18089f6b26fSZixu Wang llvm_unreachable("Unhandled language kind"); 18189f6b26fSZixu Wang } 18289f6b26fSZixu Wang 18389f6b26fSZixu Wang /// Serialize the identifier object as specified by the Symbol Graph format. 18489f6b26fSZixu Wang /// 18589f6b26fSZixu Wang /// The identifier property of a symbol contains the USR for precise and unique 18689f6b26fSZixu Wang /// references, and the interface language name. 18789f6b26fSZixu Wang Object serializeIdentifier(const APIRecord &Record, 18889f6b26fSZixu Wang const LangOptions &LangOpts) { 18989f6b26fSZixu Wang Object Identifier; 19089f6b26fSZixu Wang Identifier["precise"] = Record.USR; 19189f6b26fSZixu Wang Identifier["interfaceLanguage"] = getLanguageName(LangOpts); 19289f6b26fSZixu Wang 19389f6b26fSZixu Wang return Identifier; 19489f6b26fSZixu Wang } 19589f6b26fSZixu Wang 19689f6b26fSZixu Wang /// Serialize the documentation comments attached to a symbol, as specified by 19789f6b26fSZixu Wang /// the Symbol Graph format. 19889f6b26fSZixu Wang /// 19989f6b26fSZixu Wang /// The Symbol Graph \c docComment object contains an array of lines. Each line 20089f6b26fSZixu Wang /// represents one line of striped documentation comment, with source range 20189f6b26fSZixu Wang /// information. 20289f6b26fSZixu Wang /// e.g. 20389f6b26fSZixu Wang /// \code 20489f6b26fSZixu Wang /// /// This is a documentation comment 20589f6b26fSZixu Wang /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' First line. 20689f6b26fSZixu Wang /// /// with multiple lines. 20789f6b26fSZixu Wang /// ^~~~~~~~~~~~~~~~~~~~~~~' Second line. 20889f6b26fSZixu Wang /// \endcode 20989f6b26fSZixu Wang /// 21089f6b26fSZixu Wang /// \returns \c None if \p Comment is empty, or an \c Object containing the 21189f6b26fSZixu Wang /// formatted lines. 21289f6b26fSZixu Wang Optional<Object> serializeDocComment(const DocComment &Comment) { 21389f6b26fSZixu Wang if (Comment.empty()) 21489f6b26fSZixu Wang return None; 21589f6b26fSZixu Wang 21689f6b26fSZixu Wang Object DocComment; 21789f6b26fSZixu Wang Array LinesArray; 21889f6b26fSZixu Wang for (const auto &CommentLine : Comment) { 21989f6b26fSZixu Wang Object Line; 22089f6b26fSZixu Wang Line["text"] = CommentLine.Text; 22189f6b26fSZixu Wang serializeObject(Line, "range", 22289f6b26fSZixu Wang serializeSourceRange(CommentLine.Begin, CommentLine.End)); 22389f6b26fSZixu Wang LinesArray.emplace_back(std::move(Line)); 22489f6b26fSZixu Wang } 22589f6b26fSZixu Wang serializeArray(DocComment, "lines", LinesArray); 22689f6b26fSZixu Wang 22789f6b26fSZixu Wang return DocComment; 22889f6b26fSZixu Wang } 22989f6b26fSZixu Wang 23089f6b26fSZixu Wang /// Serialize the declaration fragments of a symbol. 23189f6b26fSZixu Wang /// 23289f6b26fSZixu Wang /// The Symbol Graph declaration fragments is an array of tagged important 23389f6b26fSZixu Wang /// parts of a symbol's declaration. The fragments sequence can be joined to 23489f6b26fSZixu Wang /// form spans of declaration text, with attached information useful for 23589f6b26fSZixu Wang /// purposes like syntax-highlighting etc. For example: 23689f6b26fSZixu Wang /// \code 23789f6b26fSZixu Wang /// const int pi; -> "declarationFragments" : [ 23889f6b26fSZixu Wang /// { 23989f6b26fSZixu Wang /// "kind" : "keyword", 24089f6b26fSZixu Wang /// "spelling" : "const" 24189f6b26fSZixu Wang /// }, 24289f6b26fSZixu Wang /// { 24389f6b26fSZixu Wang /// "kind" : "text", 24489f6b26fSZixu Wang /// "spelling" : " " 24589f6b26fSZixu Wang /// }, 24689f6b26fSZixu Wang /// { 24789f6b26fSZixu Wang /// "kind" : "typeIdentifier", 24889f6b26fSZixu Wang /// "preciseIdentifier" : "c:I", 24989f6b26fSZixu Wang /// "spelling" : "int" 25089f6b26fSZixu Wang /// }, 25189f6b26fSZixu Wang /// { 25289f6b26fSZixu Wang /// "kind" : "text", 25389f6b26fSZixu Wang /// "spelling" : " " 25489f6b26fSZixu Wang /// }, 25589f6b26fSZixu Wang /// { 25689f6b26fSZixu Wang /// "kind" : "identifier", 25789f6b26fSZixu Wang /// "spelling" : "pi" 25889f6b26fSZixu Wang /// } 25989f6b26fSZixu Wang /// ] 26089f6b26fSZixu Wang /// \endcode 26189f6b26fSZixu Wang /// 26289f6b26fSZixu Wang /// \returns \c None if \p DF is empty, or an \c Array containing the formatted 26389f6b26fSZixu Wang /// declaration fragments array. 26489f6b26fSZixu Wang Optional<Array> serializeDeclarationFragments(const DeclarationFragments &DF) { 26589f6b26fSZixu Wang if (DF.getFragments().empty()) 26689f6b26fSZixu Wang return None; 26789f6b26fSZixu Wang 26889f6b26fSZixu Wang Array Fragments; 26989f6b26fSZixu Wang for (const auto &F : DF.getFragments()) { 27089f6b26fSZixu Wang Object Fragment; 27189f6b26fSZixu Wang Fragment["spelling"] = F.Spelling; 27289f6b26fSZixu Wang Fragment["kind"] = DeclarationFragments::getFragmentKindString(F.Kind); 27389f6b26fSZixu Wang if (!F.PreciseIdentifier.empty()) 27489f6b26fSZixu Wang Fragment["preciseIdentifier"] = F.PreciseIdentifier; 27589f6b26fSZixu Wang Fragments.emplace_back(std::move(Fragment)); 27689f6b26fSZixu Wang } 27789f6b26fSZixu Wang 27889f6b26fSZixu Wang return Fragments; 27989f6b26fSZixu Wang } 28089f6b26fSZixu Wang 28189f6b26fSZixu Wang /// Serialize the function signature field of a function, as specified by the 28289f6b26fSZixu Wang /// Symbol Graph format. 28389f6b26fSZixu Wang /// 28489f6b26fSZixu Wang /// The Symbol Graph function signature property contains two arrays. 28589f6b26fSZixu Wang /// - The \c returns array is the declaration fragments of the return type; 28689f6b26fSZixu Wang /// - The \c parameters array contains names and declaration fragments of the 28789f6b26fSZixu Wang /// parameters. 28889f6b26fSZixu Wang /// 28989f6b26fSZixu Wang /// \returns \c None if \p FS is empty, or an \c Object containing the 29089f6b26fSZixu Wang /// formatted function signature. 29189f6b26fSZixu Wang Optional<Object> serializeFunctionSignature(const FunctionSignature &FS) { 29289f6b26fSZixu Wang if (FS.empty()) 29389f6b26fSZixu Wang return None; 29489f6b26fSZixu Wang 29589f6b26fSZixu Wang Object Signature; 29689f6b26fSZixu Wang serializeArray(Signature, "returns", 29789f6b26fSZixu Wang serializeDeclarationFragments(FS.getReturnType())); 29889f6b26fSZixu Wang 29989f6b26fSZixu Wang Array Parameters; 30089f6b26fSZixu Wang for (const auto &P : FS.getParameters()) { 30189f6b26fSZixu Wang Object Parameter; 30289f6b26fSZixu Wang Parameter["name"] = P.Name; 30389f6b26fSZixu Wang serializeArray(Parameter, "declarationFragments", 30489f6b26fSZixu Wang serializeDeclarationFragments(P.Fragments)); 30589f6b26fSZixu Wang Parameters.emplace_back(std::move(Parameter)); 30689f6b26fSZixu Wang } 30789f6b26fSZixu Wang 30889f6b26fSZixu Wang if (!Parameters.empty()) 30989f6b26fSZixu Wang Signature["parameters"] = std::move(Parameters); 31089f6b26fSZixu Wang 31189f6b26fSZixu Wang return Signature; 31289f6b26fSZixu Wang } 31389f6b26fSZixu Wang 31489f6b26fSZixu Wang /// Serialize the \c names field of a symbol as specified by the Symbol Graph 31589f6b26fSZixu Wang /// format. 31689f6b26fSZixu Wang /// 31789f6b26fSZixu Wang /// The Symbol Graph names field contains multiple representations of a symbol 31889f6b26fSZixu Wang /// that can be used for different applications: 31989f6b26fSZixu Wang /// - \c title : The simple declared name of the symbol; 32089f6b26fSZixu Wang /// - \c subHeading : An array of declaration fragments that provides tags, 32189f6b26fSZixu Wang /// and potentially more tokens (for example the \c +/- symbol for 32289f6b26fSZixu Wang /// Objective-C methods). Can be used as sub-headings for documentation. 32389f6b26fSZixu Wang Object serializeNames(const APIRecord &Record) { 32489f6b26fSZixu Wang Object Names; 32589f6b26fSZixu Wang Names["title"] = Record.Name; 32689f6b26fSZixu Wang serializeArray(Names, "subHeading", 32789f6b26fSZixu Wang serializeDeclarationFragments(Record.SubHeading)); 32889f6b26fSZixu Wang 32989f6b26fSZixu Wang return Names; 33089f6b26fSZixu Wang } 33189f6b26fSZixu Wang 33289f6b26fSZixu Wang /// Serialize the symbol kind information. 33389f6b26fSZixu Wang /// 33489f6b26fSZixu Wang /// The Symbol Graph symbol kind property contains a shorthand \c identifier 33589f6b26fSZixu Wang /// which is prefixed by the source language name, useful for tooling to parse 33689f6b26fSZixu Wang /// the kind, and a \c displayName for rendering human-readable names. 33789f6b26fSZixu Wang Object serializeSymbolKind(const APIRecord &Record, 33889f6b26fSZixu Wang const LangOptions &LangOpts) { 33989f6b26fSZixu Wang Object Kind; 34089f6b26fSZixu Wang switch (Record.getKind()) { 34189f6b26fSZixu Wang case APIRecord::RK_Global: 34289f6b26fSZixu Wang auto *GR = dyn_cast<GlobalRecord>(&Record); 34389f6b26fSZixu Wang switch (GR->GlobalKind) { 34489f6b26fSZixu Wang case GVKind::Function: 34589f6b26fSZixu Wang Kind["identifier"] = (getLanguageName(LangOpts) + ".func").str(); 34689f6b26fSZixu Wang Kind["displayName"] = "Function"; 34789f6b26fSZixu Wang break; 34889f6b26fSZixu Wang case GVKind::Variable: 34989f6b26fSZixu Wang Kind["identifier"] = (getLanguageName(LangOpts) + ".var").str(); 35089f6b26fSZixu Wang Kind["displayName"] = "Global Variable"; 35189f6b26fSZixu Wang break; 35289f6b26fSZixu Wang case GVKind::Unknown: 35389f6b26fSZixu Wang // Unknown global kind 35489f6b26fSZixu Wang break; 35589f6b26fSZixu Wang } 35689f6b26fSZixu Wang break; 35789f6b26fSZixu Wang } 35889f6b26fSZixu Wang 35989f6b26fSZixu Wang return Kind; 36089f6b26fSZixu Wang } 36189f6b26fSZixu Wang 36289f6b26fSZixu Wang } // namespace 36389f6b26fSZixu Wang 36489f6b26fSZixu Wang void SymbolGraphSerializer::anchor() {} 36589f6b26fSZixu Wang 36689f6b26fSZixu Wang /// Defines the format version emitted by SymbolGraphSerializer. 36789f6b26fSZixu Wang const VersionTuple SymbolGraphSerializer::FormatVersion{0, 5, 3}; 36889f6b26fSZixu Wang 36989f6b26fSZixu Wang Object SymbolGraphSerializer::serializeMetadata() const { 37089f6b26fSZixu Wang Object Metadata; 37189f6b26fSZixu Wang serializeObject(Metadata, "formatVersion", 37289f6b26fSZixu Wang serializeSemanticVersion(FormatVersion)); 37389f6b26fSZixu Wang Metadata["generator"] = clang::getClangFullVersion(); 37489f6b26fSZixu Wang return Metadata; 37589f6b26fSZixu Wang } 37689f6b26fSZixu Wang 37789f6b26fSZixu Wang Object SymbolGraphSerializer::serializeModule() const { 37889f6b26fSZixu Wang Object Module; 379*5ef2ec7eSDaniel Grumberg // The user is expected to always pass `--product-name=` on the command line 380*5ef2ec7eSDaniel Grumberg // to populate this field. 381*5ef2ec7eSDaniel Grumberg Module["name"] = ProductName; 38289f6b26fSZixu Wang serializeObject(Module, "platform", serializePlatform(API.getTarget())); 38389f6b26fSZixu Wang return Module; 38489f6b26fSZixu Wang } 38589f6b26fSZixu Wang 38689f6b26fSZixu Wang bool SymbolGraphSerializer::shouldSkip(const APIRecord &Record) const { 38789f6b26fSZixu Wang // Skip unconditionally unavailable symbols 38889f6b26fSZixu Wang if (Record.Availability.isUnconditionallyUnavailable()) 38989f6b26fSZixu Wang return true; 39089f6b26fSZixu Wang 39189f6b26fSZixu Wang return false; 39289f6b26fSZixu Wang } 39389f6b26fSZixu Wang 39489f6b26fSZixu Wang Optional<Object> 39589f6b26fSZixu Wang SymbolGraphSerializer::serializeAPIRecord(const APIRecord &Record) const { 39689f6b26fSZixu Wang if (shouldSkip(Record)) 39789f6b26fSZixu Wang return None; 39889f6b26fSZixu Wang 39989f6b26fSZixu Wang Object Obj; 40089f6b26fSZixu Wang serializeObject(Obj, "identifier", 40189f6b26fSZixu Wang serializeIdentifier(Record, API.getLangOpts())); 40289f6b26fSZixu Wang serializeObject(Obj, "kind", serializeSymbolKind(Record, API.getLangOpts())); 40389f6b26fSZixu Wang serializeObject(Obj, "names", serializeNames(Record)); 40489f6b26fSZixu Wang serializeObject( 40589f6b26fSZixu Wang Obj, "location", 40689f6b26fSZixu Wang serializeSourcePosition(Record.Location, /*IncludeFileURI=*/true)); 40789f6b26fSZixu Wang serializeObject(Obj, "availbility", 40889f6b26fSZixu Wang serializeAvailability(Record.Availability)); 40989f6b26fSZixu Wang serializeObject(Obj, "docComment", serializeDocComment(Record.Comment)); 41089f6b26fSZixu Wang serializeArray(Obj, "declarationFragments", 41189f6b26fSZixu Wang serializeDeclarationFragments(Record.Declaration)); 41289f6b26fSZixu Wang 41389f6b26fSZixu Wang return Obj; 41489f6b26fSZixu Wang } 41589f6b26fSZixu Wang 41689f6b26fSZixu Wang void SymbolGraphSerializer::serializeGlobalRecord(const GlobalRecord &Record) { 41789f6b26fSZixu Wang auto Obj = serializeAPIRecord(Record); 41889f6b26fSZixu Wang if (!Obj) 41989f6b26fSZixu Wang return; 42089f6b26fSZixu Wang 42189f6b26fSZixu Wang if (Record.GlobalKind == GVKind::Function) 42289f6b26fSZixu Wang serializeObject(*Obj, "parameters", 42389f6b26fSZixu Wang serializeFunctionSignature(Record.Signature)); 42489f6b26fSZixu Wang 42589f6b26fSZixu Wang Symbols.emplace_back(std::move(*Obj)); 42689f6b26fSZixu Wang } 42789f6b26fSZixu Wang 42889f6b26fSZixu Wang Object SymbolGraphSerializer::serialize() { 42989f6b26fSZixu Wang Object Root; 43089f6b26fSZixu Wang serializeObject(Root, "metadata", serializeMetadata()); 43189f6b26fSZixu Wang serializeObject(Root, "module", serializeModule()); 43289f6b26fSZixu Wang 43389f6b26fSZixu Wang // Serialize global records in the API set. 43489f6b26fSZixu Wang for (const auto &Global : API.getGlobals()) 43589f6b26fSZixu Wang serializeGlobalRecord(*Global.second); 43689f6b26fSZixu Wang 43789f6b26fSZixu Wang Root["symbols"] = std::move(Symbols); 43889f6b26fSZixu Wang Root["relationhips"] = std::move(Relationships); 43989f6b26fSZixu Wang 44089f6b26fSZixu Wang return Root; 44189f6b26fSZixu Wang } 44289f6b26fSZixu Wang 44389f6b26fSZixu Wang void SymbolGraphSerializer::serialize(raw_ostream &os) { 44489f6b26fSZixu Wang Object root = serialize(); 44589f6b26fSZixu Wang if (Options.Compact) 44689f6b26fSZixu Wang os << formatv("{0}", Value(std::move(root))) << "\n"; 44789f6b26fSZixu Wang else 44889f6b26fSZixu Wang os << formatv("{0:2}", Value(std::move(root))) << "\n"; 44989f6b26fSZixu Wang } 450