Generators.cpp 3.9 KB

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