XmlSourceData.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Copyright (c) 2008-2022 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Utils.h"
  23. #include "XmlAnalyzer.h"
  24. #include "XmlSourceData.h"
  25. #include <algorithm>
  26. #include <cassert>
  27. #include <iostream>
  28. #include <memory>
  29. // For GetXmlFiles()
  30. #ifdef _WIN32
  31. #include <windows.h>
  32. #else
  33. #include <dirent.h>
  34. #include <sys/stat.h>
  35. #endif
  36. using namespace std;
  37. using namespace pugi;
  38. static void LoadXml(const string& fullPath)
  39. {
  40. // All loaded XMLs. Not used directly, just prevents destruction
  41. static vector<shared_ptr<xml_document>> xmlStorage;
  42. // Load and store XML file
  43. shared_ptr<xml_document> xmlDocument = make_shared<xml_document>();
  44. xml_parse_result parseResult = xmlDocument->load_file(fullPath.c_str());
  45. assert(parseResult);
  46. xmlStorage.push_back(xmlDocument);
  47. xml_node doxygenindex = xmlDocument->child("doxygenindex");
  48. if (doxygenindex) // Only index.xml has doxygenindex child
  49. {
  50. // Fill defines_
  51. for (xml_node compound : doxygenindex.children("compound"))
  52. {
  53. for (xml_node member : compound.children("member"))
  54. {
  55. string member_kind = ExtractKind(member);
  56. if (member_kind == "define")
  57. {
  58. string name = member.child("name").child_value();
  59. SourceData::defines_.push_back(name);
  60. }
  61. }
  62. }
  63. return;
  64. }
  65. xml_node compounddef = xmlDocument->child("doxygen").child("compounddef");
  66. if (!compounddef)
  67. return;
  68. string compounddef_kind = ExtractKind(compounddef);
  69. if (compounddef_kind == "struct" || compounddef_kind == "class")
  70. {
  71. // Fill classesByID_
  72. string id = ExtractID(compounddef);
  73. SourceData::classesByID_.insert({ id, compounddef });
  74. // Fill classesByName_
  75. string compoundname = ExtractCompoundname(compounddef);
  76. if (StartsWith(compoundname, "Urho3D::"))
  77. {
  78. string name = CutStart(compoundname, "Urho3D::");
  79. if (!Contains(name, "::"))
  80. SourceData::classesByName_.insert({ name, compounddef });
  81. }
  82. // Fill members_
  83. for (xml_node sectiondef : compounddef.children("sectiondef"))
  84. {
  85. for (xml_node memberdef : sectiondef.children("memberdef"))
  86. {
  87. string memberdef_kind = ExtractKind(memberdef);
  88. if (memberdef_kind != "variable" && memberdef_kind != "function")
  89. continue;
  90. string member_id = ExtractID(memberdef);
  91. SourceData::members_.insert({ member_id, memberdef });
  92. }
  93. }
  94. return;
  95. }
  96. if (compounddef_kind == "namespace")
  97. {
  98. string compoundname = compounddef.child("compoundname").child_value();
  99. if (compoundname != "Urho3D")
  100. return;
  101. // Init namespaceUrho3D_
  102. SourceData::namespaceUrho3D_ = compounddef;
  103. // Fill usings_
  104. //
  105. // <compounddef kind="namespace">
  106. // <sectiondef kind="typedef">
  107. // <memberdef kind="typedef">...</memberdef>
  108. // <memberdef kind="typedef">...</memberdef>
  109. xml_node typedefs = FindSectiondef(compounddef, "typedef");
  110. assert(typedefs);
  111. for (xml_node memberdef : typedefs.children("memberdef"))
  112. {
  113. string definition = ExtractDefinition(memberdef);
  114. if (StartsWith(definition, "using "))
  115. SourceData::usings_.push_back(memberdef);
  116. }
  117. // <compounddef kind="namespace">
  118. // <sectiondef kind="enum">
  119. // <memberdef kind="enum">...</memberdef>
  120. // <memberdef kind="enum">...</memberdef>
  121. xml_node enums = FindSectiondef(compounddef, "enum");
  122. assert(enums);
  123. for (xml_node memberdef : enums.children("memberdef"))
  124. {
  125. string name = ExtractName(memberdef);
  126. SourceData::enums_.insert({ name, memberdef });
  127. }
  128. return;
  129. }
  130. if (compounddef_kind == "file")
  131. {
  132. // Fill ignoredHeaders_
  133. string comment = ExtractComment(compounddef);
  134. if (Contains(comment, "NO_BIND_FILE"))
  135. SourceData::ignoredHeaders_.push_back(ExtractHeaderFile(compounddef));
  136. return;
  137. }
  138. }
  139. static string AddTrailingSlash(const string& pathName)
  140. {
  141. string ret = pathName;
  142. replace(ret.begin(), ret.end(), '\\', '/');
  143. if (!ret.empty() && ret.back() != '/')
  144. ret += '/';
  145. return ret;
  146. }
  147. static void GetXmlFiles(string dirPath, vector<string>& result)
  148. {
  149. result.clear();
  150. dirPath = AddTrailingSlash(dirPath);
  151. #ifdef _WIN32
  152. WIN32_FIND_DATAA findData;
  153. HANDLE handle = FindFirstFileA((dirPath + "*.xml").c_str(), &findData);
  154. if (handle == INVALID_HANDLE_VALUE)
  155. return;
  156. do
  157. {
  158. if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  159. result.push_back(dirPath + findData.cFileName);
  160. } while (FindNextFileA(handle, &findData));
  161. FindClose(handle);
  162. #else
  163. DIR* dir = opendir(dirPath.c_str());
  164. if (!dir)
  165. return;
  166. struct stat st;
  167. while (dirent* de = readdir(dir))
  168. {
  169. string filePath = dirPath + de->d_name;
  170. if (stat(filePath.c_str(), &st) != 0)
  171. continue;
  172. if (st.st_mode & S_IFDIR)
  173. continue;
  174. if (EndsWith(filePath, ".xml"))
  175. result.push_back(filePath);
  176. }
  177. closedir(dir);
  178. #endif
  179. }
  180. namespace SourceData
  181. {
  182. unordered_map<string, xml_node> classesByID_;
  183. unordered_map<string, xml_node> classesByName_;
  184. unordered_map<string, xml_node> members_;
  185. unordered_map<string, xml_node> enums_;
  186. xml_node namespaceUrho3D_;
  187. vector<xml_node> usings_;
  188. vector<string> defines_;
  189. vector<string> ignoredHeaders_;
  190. void LoadAllXmls(const string& dir)
  191. {
  192. cout << "Loading XMLs from " << dir << "\n";
  193. vector<string> fullPaths;
  194. GetXmlFiles(dir, fullPaths);
  195. cout << "Found " << fullPaths.size() << " xml files\n";
  196. for (string fullPath : fullPaths)
  197. LoadXml(fullPath);
  198. }
  199. }