XmlSourceData.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (StartsWith(definition, "using "))
  96. SourceData::usings_.push_back(memberdef);
  97. }
  98. // <compounddef kind="namespace">
  99. // <sectiondef kind="enum">
  100. // <memberdef kind="enum">...</memberdef>
  101. // <memberdef kind="enum">...</memberdef>
  102. xml_node enums = FindSectiondef(compounddef, "enum");
  103. assert(enums);
  104. for (xml_node memberdef : enums.children("memberdef"))
  105. {
  106. string name = ExtractName(memberdef);
  107. SourceData::enums_.insert({ name, memberdef });
  108. }
  109. return;
  110. }
  111. if (compounddef_kind == "file")
  112. {
  113. // Fill ignoredHeaders_
  114. string comment = ExtractComment(compounddef);
  115. if (Contains(comment, "NO_BIND_FILE"))
  116. SourceData::ignoredHeaders_.push_back(ExtractHeaderFile(compounddef));
  117. return;
  118. }
  119. }
  120. static string AddTrailingSlash(const string& pathName)
  121. {
  122. string ret = pathName;
  123. replace(ret.begin(), ret.end(), '\\', '/');
  124. if (!ret.empty() && ret.back() != '/')
  125. ret += '/';
  126. return ret;
  127. }
  128. static void GetXmlFiles(string dirPath, vector<string>& result)
  129. {
  130. result.clear();
  131. dirPath = AddTrailingSlash(dirPath);
  132. #ifdef _WIN32
  133. WIN32_FIND_DATAA findData;
  134. HANDLE handle = FindFirstFileA((dirPath + "*.xml").c_str(), &findData);
  135. if (handle == INVALID_HANDLE_VALUE)
  136. return;
  137. do
  138. {
  139. if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  140. result.push_back(dirPath + findData.cFileName);
  141. } while (FindNextFileA(handle, &findData));
  142. FindClose(handle);
  143. #else
  144. DIR* dir = opendir(dirPath.c_str());
  145. if (!dir)
  146. return;
  147. struct stat st;
  148. while (dirent* de = readdir(dir))
  149. {
  150. string filePath = dirPath + de->d_name;
  151. if (stat(filePath.c_str(), &st) != 0)
  152. continue;
  153. if (st.st_mode & S_IFDIR)
  154. continue;
  155. if (EndsWith(filePath, ".xml"))
  156. result.push_back(filePath);
  157. }
  158. closedir(dir);
  159. #endif
  160. }
  161. namespace SourceData
  162. {
  163. unordered_map<string, xml_node> classesByID_;
  164. unordered_map<string, xml_node> classesByName_;
  165. unordered_map<string, xml_node> members_;
  166. unordered_map<string, xml_node> enums_;
  167. xml_node namespaceUrho3D_;
  168. vector<xml_node> usings_;
  169. vector<string> defines_;
  170. vector<string> ignoredHeaders_;
  171. void LoadAllXmls(const string& dir)
  172. {
  173. cout << "Loading XMLs from " << dir << "\n";
  174. vector<string> fullPaths;
  175. GetXmlFiles(dir, fullPaths);
  176. cout << "Found " << fullPaths.size() << " xml files\n";
  177. for (string fullPath : fullPaths)
  178. LoadXml(fullPath);
  179. }
  180. }