1 //===-- Generators.cpp - Generator Registry ----------------------*- 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 #include "Generators.h" 10 11 LLVM_INSTANTIATE_REGISTRY(clang::doc::GeneratorRegistry) 12 13 namespace clang { 14 namespace doc { 15 16 llvm::Expected<std::unique_ptr<Generator>> 17 findGeneratorByName(llvm::StringRef Format) { 18 for (auto I = GeneratorRegistry::begin(), E = GeneratorRegistry::end(); 19 I != E; ++I) { 20 if (I->getName() != Format) 21 continue; 22 return I->instantiate(); 23 } 24 return createStringError(llvm::inconvertibleErrorCode(), 25 "can't find generator: " + Format); 26 } 27 28 // Enum conversion 29 30 std::string getAccess(AccessSpecifier AS) { 31 switch (AS) { 32 case AccessSpecifier::AS_public: 33 return "public"; 34 case AccessSpecifier::AS_protected: 35 return "protected"; 36 case AccessSpecifier::AS_private: 37 return "private"; 38 case AccessSpecifier::AS_none: 39 return {}; 40 } 41 llvm_unreachable("Unknown AccessSpecifier"); 42 } 43 44 std::string getTagType(TagTypeKind AS) { 45 switch (AS) { 46 case TagTypeKind::TTK_Class: 47 return "class"; 48 case TagTypeKind::TTK_Union: 49 return "union"; 50 case TagTypeKind::TTK_Interface: 51 return "interface"; 52 case TagTypeKind::TTK_Struct: 53 return "struct"; 54 case TagTypeKind::TTK_Enum: 55 return "enum"; 56 } 57 llvm_unreachable("Unknown TagTypeKind"); 58 } 59 60 llvm::Error Generator::createResources(ClangDocContext &CDCtx) { 61 return llvm::Error::success(); 62 } 63 64 // A function to add a reference to Info in Idx. 65 // Given an Info X with the following namespaces: [B,A]; a reference to X will 66 // be added in the children of a reference to B, which should be also a child of 67 // a reference to A, where A is a child of Idx. 68 // Idx 69 // |-- A 70 // |--B 71 // |--X 72 // If the references to the namespaces do not exist, they will be created. If 73 // the references already exist, the same one will be used. 74 void Generator::addInfoToIndex(Index &Idx, const doc::Info *Info) { 75 // Index pointer that will be moving through Idx until the first parent 76 // namespace of Info (where the reference has to be inserted) is found. 77 Index *I = &Idx; 78 // The Namespace vector includes the upper-most namespace at the end so the 79 // loop will start from the end to find each of the namespaces. 80 for (const auto &R : llvm::reverse(Info->Namespace)) { 81 // Look for the current namespace in the children of the index I is 82 // pointing. 83 auto It = std::find(I->Children.begin(), I->Children.end(), R.USR); 84 if (It != I->Children.end()) { 85 // If it is found, just change I to point the namespace reference found. 86 I = &*It; 87 } else { 88 // If it is not found a new reference is created 89 I->Children.emplace_back(R.USR, R.Name, R.RefType, R.Path); 90 // I is updated with the reference of the new namespace reference 91 I = &I->Children.back(); 92 } 93 } 94 // Look for Info in the vector where it is supposed to be; it could already 95 // exist if it is a parent namespace of an Info already passed to this 96 // function. 97 auto It = std::find(I->Children.begin(), I->Children.end(), Info->USR); 98 if (It == I->Children.end()) { 99 // If it is not in the vector it is inserted 100 I->Children.emplace_back(Info->USR, Info->extractName(), Info->IT, 101 Info->Path); 102 } else { 103 // If it not in the vector we only check if Path and Name are not empty 104 // because if the Info was included by a namespace it may not have those 105 // values. 106 if (It->Path.empty()) 107 It->Path = Info->Path; 108 if (It->Name.empty()) 109 It->Name = Info->extractName(); 110 } 111 } 112 113 // This anchor is used to force the linker to link in the generated object file 114 // and thus register the generators. 115 extern volatile int YAMLGeneratorAnchorSource; 116 extern volatile int MDGeneratorAnchorSource; 117 extern volatile int HTMLGeneratorAnchorSource; 118 static int LLVM_ATTRIBUTE_UNUSED YAMLGeneratorAnchorDest = 119 YAMLGeneratorAnchorSource; 120 static int LLVM_ATTRIBUTE_UNUSED MDGeneratorAnchorDest = 121 MDGeneratorAnchorSource; 122 static int LLVM_ATTRIBUTE_UNUSED HTMLGeneratorAnchorDest = 123 HTMLGeneratorAnchorSource; 124 125 } // namespace doc 126 } // namespace clang 127