Representation.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ///===-- Representation.cpp - ClangDoc Representation -----------*- 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. // This file defines the merging of different types of infos. The data in the
  10. // calling Info is preserved during a merge unless that field is empty or
  11. // default. In that case, the data from the parameter Info is used to replace
  12. // the empty or default data.
  13. //
  14. // For most fields, the first decl seen provides the data. Exceptions to this
  15. // include the location and description fields, which are collections of data on
  16. // all decls related to a given definition. All other fields are ignored in new
  17. // decls unless the first seen decl didn't, for whatever reason, incorporate
  18. // data on that field (e.g. a forward declared class wouldn't have information
  19. // on members on the forward declaration, but would have the class name).
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #include "Representation.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/Support/Path.h"
  25. namespace clang {
  26. namespace doc {
  27. namespace {
  28. const SymbolID EmptySID = SymbolID();
  29. template <typename T>
  30. llvm::Expected<std::unique_ptr<Info>>
  31. reduce(std::vector<std::unique_ptr<Info>> &Values) {
  32. if (Values.empty())
  33. return llvm::createStringError(llvm::inconvertibleErrorCode(),
  34. "no value to reduce");
  35. std::unique_ptr<Info> Merged = std::make_unique<T>(Values[0]->USR);
  36. T *Tmp = static_cast<T *>(Merged.get());
  37. for (auto &I : Values)
  38. Tmp->merge(std::move(*static_cast<T *>(I.get())));
  39. return std::move(Merged);
  40. }
  41. // Return the index of the matching child in the vector, or -1 if merge is not
  42. // necessary.
  43. template <typename T>
  44. int getChildIndexIfExists(std::vector<T> &Children, T &ChildToMerge) {
  45. for (unsigned long I = 0; I < Children.size(); I++) {
  46. if (ChildToMerge.USR == Children[I].USR)
  47. return I;
  48. }
  49. return -1;
  50. }
  51. void reduceChildren(std::vector<Reference> &Children,
  52. std::vector<Reference> &&ChildrenToMerge) {
  53. for (auto &ChildToMerge : ChildrenToMerge) {
  54. int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
  55. if (mergeIdx == -1) {
  56. Children.push_back(std::move(ChildToMerge));
  57. continue;
  58. }
  59. Children[mergeIdx].merge(std::move(ChildToMerge));
  60. }
  61. }
  62. void reduceChildren(std::vector<FunctionInfo> &Children,
  63. std::vector<FunctionInfo> &&ChildrenToMerge) {
  64. for (auto &ChildToMerge : ChildrenToMerge) {
  65. int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
  66. if (mergeIdx == -1) {
  67. Children.push_back(std::move(ChildToMerge));
  68. continue;
  69. }
  70. Children[mergeIdx].merge(std::move(ChildToMerge));
  71. }
  72. }
  73. void reduceChildren(std::vector<EnumInfo> &Children,
  74. std::vector<EnumInfo> &&ChildrenToMerge) {
  75. for (auto &ChildToMerge : ChildrenToMerge) {
  76. int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
  77. if (mergeIdx == -1) {
  78. Children.push_back(std::move(ChildToMerge));
  79. continue;
  80. }
  81. Children[mergeIdx].merge(std::move(ChildToMerge));
  82. }
  83. }
  84. } // namespace
  85. // Dispatch function.
  86. llvm::Expected<std::unique_ptr<Info>>
  87. mergeInfos(std::vector<std::unique_ptr<Info>> &Values) {
  88. if (Values.empty())
  89. return llvm::createStringError(llvm::inconvertibleErrorCode(),
  90. "no info values to merge");
  91. switch (Values[0]->IT) {
  92. case InfoType::IT_namespace:
  93. return reduce<NamespaceInfo>(Values);
  94. case InfoType::IT_record:
  95. return reduce<RecordInfo>(Values);
  96. case InfoType::IT_enum:
  97. return reduce<EnumInfo>(Values);
  98. case InfoType::IT_function:
  99. return reduce<FunctionInfo>(Values);
  100. default:
  101. return llvm::createStringError(llvm::inconvertibleErrorCode(),
  102. "unexpected info type");
  103. }
  104. }
  105. static llvm::SmallString<64>
  106. calculateRelativeFilePath(const InfoType &Type, const StringRef &Path,
  107. const StringRef &Name, const StringRef &CurrentPath) {
  108. llvm::SmallString<64> FilePath;
  109. if (CurrentPath != Path) {
  110. // iterate back to the top
  111. for (llvm::sys::path::const_iterator I =
  112. llvm::sys::path::begin(CurrentPath);
  113. I != llvm::sys::path::end(CurrentPath); ++I)
  114. llvm::sys::path::append(FilePath, "..");
  115. llvm::sys::path::append(FilePath, Path);
  116. }
  117. // Namespace references have a Path to the parent namespace, but
  118. // the file is actually in the subdirectory for the namespace.
  119. if (Type == doc::InfoType::IT_namespace)
  120. llvm::sys::path::append(FilePath, Name);
  121. return llvm::sys::path::relative_path(FilePath);
  122. }
  123. llvm::SmallString<64>
  124. Reference::getRelativeFilePath(const StringRef &CurrentPath) const {
  125. return calculateRelativeFilePath(RefType, Path, Name, CurrentPath);
  126. }
  127. llvm::SmallString<16> Reference::getFileBaseName() const {
  128. if (RefType == InfoType::IT_namespace)
  129. return llvm::SmallString<16>("index");
  130. return Name;
  131. }
  132. llvm::SmallString<64>
  133. Info::getRelativeFilePath(const StringRef &CurrentPath) const {
  134. return calculateRelativeFilePath(IT, Path, extractName(), CurrentPath);
  135. }
  136. llvm::SmallString<16> Info::getFileBaseName() const {
  137. if (IT == InfoType::IT_namespace)
  138. return llvm::SmallString<16>("index");
  139. return extractName();
  140. }
  141. bool Reference::mergeable(const Reference &Other) {
  142. return RefType == Other.RefType && USR == Other.USR;
  143. }
  144. void Reference::merge(Reference &&Other) {
  145. assert(mergeable(Other));
  146. if (Name.empty())
  147. Name = Other.Name;
  148. if (Path.empty())
  149. Path = Other.Path;
  150. if (!IsInGlobalNamespace)
  151. IsInGlobalNamespace = Other.IsInGlobalNamespace;
  152. }
  153. void Info::mergeBase(Info &&Other) {
  154. assert(mergeable(Other));
  155. if (USR == EmptySID)
  156. USR = Other.USR;
  157. if (Name == "")
  158. Name = Other.Name;
  159. if (Path == "")
  160. Path = Other.Path;
  161. if (Namespace.empty())
  162. Namespace = std::move(Other.Namespace);
  163. // Unconditionally extend the description, since each decl may have a comment.
  164. std::move(Other.Description.begin(), Other.Description.end(),
  165. std::back_inserter(Description));
  166. llvm::sort(Description);
  167. auto Last = std::unique(Description.begin(), Description.end());
  168. Description.erase(Last, Description.end());
  169. }
  170. bool Info::mergeable(const Info &Other) {
  171. return IT == Other.IT && USR == Other.USR;
  172. }
  173. void SymbolInfo::merge(SymbolInfo &&Other) {
  174. assert(mergeable(Other));
  175. if (!DefLoc)
  176. DefLoc = std::move(Other.DefLoc);
  177. // Unconditionally extend the list of locations, since we want all of them.
  178. std::move(Other.Loc.begin(), Other.Loc.end(), std::back_inserter(Loc));
  179. llvm::sort(Loc);
  180. auto Last = std::unique(Loc.begin(), Loc.end());
  181. Loc.erase(Last, Loc.end());
  182. mergeBase(std::move(Other));
  183. }
  184. void NamespaceInfo::merge(NamespaceInfo &&Other) {
  185. assert(mergeable(Other));
  186. // Reduce children if necessary.
  187. reduceChildren(ChildNamespaces, std::move(Other.ChildNamespaces));
  188. reduceChildren(ChildRecords, std::move(Other.ChildRecords));
  189. reduceChildren(ChildFunctions, std::move(Other.ChildFunctions));
  190. reduceChildren(ChildEnums, std::move(Other.ChildEnums));
  191. mergeBase(std::move(Other));
  192. }
  193. NamespaceInfo::NamespaceInfo() : Info(InfoType::IT_namespace) {}
  194. NamespaceInfo::NamespaceInfo(SymbolID USR) : Info(InfoType::IT_namespace, USR) {}
  195. NamespaceInfo::NamespaceInfo(SymbolID USR, StringRef Name)
  196. : Info(InfoType::IT_namespace, USR, Name) {}
  197. NamespaceInfo::NamespaceInfo(SymbolID USR, StringRef Name, StringRef Path)
  198. : Info(InfoType::IT_namespace, USR, Name, Path) {}
  199. void RecordInfo::merge(RecordInfo &&Other) {
  200. assert(mergeable(Other));
  201. if (!TagType)
  202. TagType = Other.TagType;
  203. if (Members.empty())
  204. Members = std::move(Other.Members);
  205. if (Bases.empty())
  206. Bases = std::move(Other.Bases);
  207. if (Parents.empty())
  208. Parents = std::move(Other.Parents);
  209. if (VirtualParents.empty())
  210. VirtualParents = std::move(Other.VirtualParents);
  211. // Reduce children if necessary.
  212. reduceChildren(ChildRecords, std::move(Other.ChildRecords));
  213. reduceChildren(ChildFunctions, std::move(Other.ChildFunctions));
  214. reduceChildren(ChildEnums, std::move(Other.ChildEnums));
  215. SymbolInfo::merge(std::move(Other));
  216. }
  217. RecordInfo::RecordInfo() : SymbolInfo(InfoType::IT_record) {}
  218. RecordInfo::RecordInfo(SymbolID USR) : SymbolInfo(InfoType::IT_record, USR) {}
  219. RecordInfo::RecordInfo(SymbolID USR, StringRef Name)
  220. : SymbolInfo(InfoType::IT_record, USR, Name) {}
  221. RecordInfo::RecordInfo(SymbolID USR, StringRef Name, StringRef Path)
  222. : SymbolInfo(InfoType::IT_record, USR, Name, Path) {}
  223. BaseRecordInfo::BaseRecordInfo() : RecordInfo() {}
  224. BaseRecordInfo::BaseRecordInfo(SymbolID USR, StringRef Name, StringRef Path, bool IsVirtual,
  225. AccessSpecifier Access, bool IsParent)
  226. : RecordInfo(USR, Name, Path), IsVirtual(IsVirtual), Access(Access),
  227. IsParent(IsParent) {}
  228. void EnumInfo::merge(EnumInfo &&Other) {
  229. assert(mergeable(Other));
  230. if (!Scoped)
  231. Scoped = Other.Scoped;
  232. if (Members.empty())
  233. Members = std::move(Other.Members);
  234. SymbolInfo::merge(std::move(Other));
  235. }
  236. void FunctionInfo::merge(FunctionInfo &&Other) {
  237. assert(mergeable(Other));
  238. if (!IsMethod)
  239. IsMethod = Other.IsMethod;
  240. if (!Access)
  241. Access = Other.Access;
  242. if (ReturnType.Type.USR == EmptySID && ReturnType.Type.Name == "")
  243. ReturnType = std::move(Other.ReturnType);
  244. if (Parent.USR == EmptySID && Parent.Name == "")
  245. Parent = std::move(Other.Parent);
  246. if (Params.empty())
  247. Params = std::move(Other.Params);
  248. SymbolInfo::merge(std::move(Other));
  249. }
  250. llvm::SmallString<16> Info::extractName() const {
  251. if (!Name.empty())
  252. return Name;
  253. switch (IT) {
  254. case InfoType::IT_namespace:
  255. // Cover the case where the project contains a base namespace called
  256. // 'GlobalNamespace' (i.e. a namespace at the same level as the global
  257. // namespace, which would conflict with the hard-coded global namespace name
  258. // below.)
  259. if (Name == "GlobalNamespace" && Namespace.empty())
  260. return llvm::SmallString<16>("@GlobalNamespace");
  261. // The case of anonymous namespaces is taken care of in serialization,
  262. // so here we can safely assume an unnamed namespace is the global
  263. // one.
  264. return llvm::SmallString<16>("GlobalNamespace");
  265. case InfoType::IT_record:
  266. return llvm::SmallString<16>("@nonymous_record_" +
  267. toHex(llvm::toStringRef(USR)));
  268. case InfoType::IT_enum:
  269. return llvm::SmallString<16>("@nonymous_enum_" +
  270. toHex(llvm::toStringRef(USR)));
  271. case InfoType::IT_function:
  272. return llvm::SmallString<16>("@nonymous_function_" +
  273. toHex(llvm::toStringRef(USR)));
  274. case InfoType::IT_default:
  275. return llvm::SmallString<16>("@nonymous_" + toHex(llvm::toStringRef(USR)));
  276. }
  277. llvm_unreachable("Invalid InfoType.");
  278. return llvm::SmallString<16>("");
  279. }
  280. // Order is based on the Name attribute: case insensitive order
  281. bool Index::operator<(const Index &Other) const {
  282. // Loop through each character of both strings
  283. for (unsigned I = 0; I < Name.size() && I < Other.Name.size(); ++I) {
  284. // Compare them after converting both to lower case
  285. int D = tolower(Name[I]) - tolower(Other.Name[I]);
  286. if (D == 0)
  287. continue;
  288. return D < 0;
  289. }
  290. // If both strings have the size it means they would be equal if changed to
  291. // lower case. In here, lower case will be smaller than upper case
  292. // Example: string < stRing = true
  293. // This is the opposite of how operator < handles strings
  294. if (Name.size() == Other.Name.size())
  295. return Name > Other.Name;
  296. // If they are not the same size; the shorter string is smaller
  297. return Name.size() < Other.Name.size();
  298. }
  299. void Index::sort() {
  300. llvm::sort(Children);
  301. for (auto &C : Children)
  302. C.sort();
  303. }
  304. ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
  305. StringRef ProjectName, bool PublicOnly,
  306. StringRef OutDirectory, StringRef SourceRoot,
  307. StringRef RepositoryUrl,
  308. std::vector<std::string> UserStylesheets,
  309. std::vector<std::string> JsScripts)
  310. : ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly),
  311. OutDirectory(OutDirectory), UserStylesheets(UserStylesheets),
  312. JsScripts(JsScripts) {
  313. llvm::SmallString<128> SourceRootDir(SourceRoot);
  314. if (SourceRoot.empty())
  315. // If no SourceRoot was provided the current path is used as the default
  316. llvm::sys::fs::current_path(SourceRootDir);
  317. this->SourceRoot = std::string(SourceRootDir.str());
  318. if (!RepositoryUrl.empty()) {
  319. this->RepositoryUrl = std::string(RepositoryUrl);
  320. if (!RepositoryUrl.empty() && RepositoryUrl.find("http://") != 0 &&
  321. RepositoryUrl.find("https://") != 0)
  322. this->RepositoryUrl->insert(0, "https://");
  323. }
  324. }
  325. } // namespace doc
  326. } // namespace clang