2
0

XmlSourceData.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "Utils.h"
  4. #include "XmlAnalyzer.h"
  5. #include "XmlSourceData.h"
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <iostream>
  9. #include <memory>
  10. // For GetXmlFiles()
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #else
  14. #include <dirent.h>
  15. #include <sys/stat.h>
  16. #endif
  17. using namespace std;
  18. using namespace pugi;
  19. static void LoadXml(const string& fullPath)
  20. {
  21. // All loaded XMLs. Not used directly, just prevents destruction
  22. static vector<shared_ptr<xml_document>> xmlStorage;
  23. // Load and store XML file
  24. shared_ptr<xml_document> xmlDocument = make_shared<xml_document>();
  25. xml_parse_result parseResult = xmlDocument->load_file(fullPath.c_str());
  26. assert(parseResult);
  27. xmlStorage.push_back(xmlDocument);
  28. xml_node doxygenindex = xmlDocument->child("doxygenindex");
  29. if (doxygenindex) // Only index.xml has doxygenindex child
  30. {
  31. // Fill defines_
  32. for (xml_node compound : doxygenindex.children("compound"))
  33. {
  34. for (xml_node member : compound.children("member"))
  35. {
  36. string member_kind = ExtractKind(member);
  37. if (member_kind == "define")
  38. {
  39. string name = member.child("name").child_value();
  40. SourceData::defines_.push_back(name);
  41. }
  42. }
  43. }
  44. return;
  45. }
  46. xml_node compounddef = xmlDocument->child("doxygen").child("compounddef");
  47. if (!compounddef)
  48. return;
  49. string compounddef_kind = ExtractKind(compounddef);
  50. if (compounddef_kind == "struct" || compounddef_kind == "class")
  51. {
  52. // Fill classesByID_
  53. string id = ExtractID(compounddef);
  54. SourceData::classesByID_.insert({ id, compounddef });
  55. // Fill classesByName_
  56. string compoundname = ExtractCompoundname(compounddef);
  57. if (StartsWith(compoundname, "Urho3D::"))
  58. {
  59. string name = CutStart(compoundname, "Urho3D::");
  60. if (!Contains(name, "::"))
  61. SourceData::classesByName_.insert({ name, compounddef });
  62. }
  63. // Fill members_
  64. for (xml_node sectiondef : compounddef.children("sectiondef"))
  65. {
  66. for (xml_node memberdef : sectiondef.children("memberdef"))
  67. {
  68. string memberdef_kind = ExtractKind(memberdef);
  69. if (memberdef_kind != "variable" && memberdef_kind != "function")
  70. continue;
  71. string member_id = ExtractID(memberdef);
  72. SourceData::members_.insert({ member_id, memberdef });
  73. }
  74. }
  75. return;
  76. }
  77. if (compounddef_kind == "namespace")
  78. {
  79. string compoundname = compounddef.child("compoundname").child_value();
  80. if (compoundname != "Urho3D")
  81. return;
  82. // Init namespaceUrho3D_
  83. SourceData::namespaceUrho3D_ = compounddef;
  84. // Fill usings_
  85. //
  86. // <compounddef kind="namespace">
  87. // <sectiondef kind="typedef">
  88. // <memberdef kind="typedef">...</memberdef>
  89. // <memberdef kind="typedef">...</memberdef>
  90. xml_node typedefs = FindSectiondef(compounddef, "typedef");
  91. assert(typedefs);
  92. for (xml_node memberdef : typedefs.children("memberdef"))
  93. {
  94. string definition = ExtractDefinition(memberdef);
  95. // TODO: using can be typedef https://github.com/doxygen/doxygen/issues/9654
  96. if (StartsWith(definition, "using "))
  97. SourceData::usings_.push_back(memberdef);
  98. }
  99. // <compounddef kind="namespace">
  100. // <sectiondef kind="enum">
  101. // <memberdef kind="enum">...</memberdef>
  102. // <memberdef kind="enum">...</memberdef>
  103. xml_node enums = FindSectiondef(compounddef, "enum");
  104. assert(enums);
  105. for (xml_node memberdef : enums.children("memberdef"))
  106. {
  107. string name = ExtractName(memberdef);
  108. SourceData::enums_.insert({ name, memberdef });
  109. }
  110. return;
  111. }
  112. if (compounddef_kind == "file")
  113. {
  114. // Fill ignoredHeaders_
  115. string comment = ExtractComment(compounddef);
  116. if (Contains(comment, "NO_BIND_FILE"))
  117. SourceData::ignoredHeaders_.push_back(ExtractHeaderFile(compounddef));
  118. return;
  119. }
  120. }
  121. static string AddTrailingSlash(const string& pathName)
  122. {
  123. string ret = pathName;
  124. replace(ret.begin(), ret.end(), '\\', '/');
  125. if (!ret.empty() && ret.back() != '/')
  126. ret += '/';
  127. return ret;
  128. }
  129. static void GetXmlFiles(string dirPath, vector<string>& result)
  130. {
  131. result.clear();
  132. dirPath = AddTrailingSlash(dirPath);
  133. #ifdef _WIN32
  134. WIN32_FIND_DATAA findData;
  135. HANDLE handle = FindFirstFileA((dirPath + "*.xml").c_str(), &findData);
  136. if (handle == INVALID_HANDLE_VALUE)
  137. return;
  138. do
  139. {
  140. if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  141. result.push_back(dirPath + findData.cFileName);
  142. } while (FindNextFileA(handle, &findData));
  143. FindClose(handle);
  144. #else
  145. DIR* dir = opendir(dirPath.c_str());
  146. if (!dir)
  147. return;
  148. struct stat st;
  149. while (dirent* de = readdir(dir))
  150. {
  151. string filePath = dirPath + de->d_name;
  152. if (stat(filePath.c_str(), &st) != 0)
  153. continue;
  154. if (st.st_mode & S_IFDIR)
  155. continue;
  156. if (EndsWith(filePath, ".xml"))
  157. result.push_back(filePath);
  158. }
  159. closedir(dir);
  160. #endif
  161. }
  162. namespace SourceData
  163. {
  164. unordered_map<string, xml_node> classesByID_;
  165. unordered_map<string, xml_node> classesByName_;
  166. unordered_map<string, xml_node> members_;
  167. unordered_map<string, xml_node> enums_;
  168. xml_node namespaceUrho3D_;
  169. vector<xml_node> usings_;
  170. vector<string> defines_;
  171. vector<string> ignoredHeaders_;
  172. void LoadAllXmls(const string& dir)
  173. {
  174. cout << "Loading XMLs from " << dir << "\n";
  175. vector<string> fullPaths;
  176. GetXmlFiles(dir, fullPaths);
  177. cout << "Found " << fullPaths.size() << " xml files\n";
  178. for (string fullPath : fullPaths)
  179. LoadXml(fullPath);
  180. }
  181. }