XmlSourceData.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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) // index.xml
  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 classes_
  72. string id = compounddef.attribute("id").value();
  73. assert(!id.empty());
  74. SourceData::classesByID_.insert({ id, compounddef });
  75. // Fill classesByName_
  76. string compoundname = ExtractCompoundname(compounddef);
  77. if (StartsWith(compoundname, "Urho3D::"))
  78. {
  79. string name = CutStart(compoundname, "Urho3D::");
  80. if (!Contains(name, "::"))
  81. SourceData::classesByName_.insert({ name, compounddef });
  82. }
  83. // Fill members_
  84. for (xml_node sectiondef : compounddef.children("sectiondef"))
  85. {
  86. for (xml_node memberdef : sectiondef.children("memberdef"))
  87. {
  88. string memberdef_kind = ExtractKind(memberdef);
  89. if (memberdef_kind != "variable" && memberdef_kind != "function")
  90. continue;
  91. string member_id = ExtractID(memberdef);
  92. SourceData::members_.insert({ member_id, memberdef });
  93. }
  94. }
  95. return;
  96. }
  97. if (compounddef_kind == "namespace")
  98. {
  99. string compoundname = compounddef.child("compoundname").child_value();
  100. if (compoundname != "Urho3D")
  101. return;
  102. // Init namespaceUrho3D_
  103. SourceData::namespaceUrho3D_ = compounddef;
  104. // Fill usings_
  105. //
  106. // <compounddef kind="namespace">
  107. // <sectiondef kind="typedef">
  108. // <memberdef kind="typedef">...</memberdef>
  109. // <memberdef kind="typedef">...</memberdef>
  110. xml_node typedefs = FindSectiondef(compounddef, "typedef");
  111. assert(typedefs);
  112. for (xml_node memberdef : typedefs.children("memberdef"))
  113. {
  114. string definition = ExtractDefinition(memberdef);
  115. if (StartsWith(definition, "using "))
  116. SourceData::usings_.push_back(memberdef);
  117. }
  118. // <compounddef kind="namespace">
  119. // <sectiondef kind="enum">
  120. // <memberdef kind="enum">...</memberdef>
  121. // <memberdef kind="enum">...</memberdef>
  122. xml_node enums = FindSectiondef(compounddef, "enum");
  123. assert(enums);
  124. for (xml_node memberdef : enums.children("memberdef"))
  125. {
  126. string name = ExtractName(memberdef);
  127. SourceData::enums_.insert({ name, memberdef });
  128. }
  129. return;
  130. }
  131. if (compounddef_kind == "file")
  132. {
  133. // Fill ignoredHeaders_
  134. string comment = ExtractComment(compounddef);
  135. if (Contains(comment, "NO_BIND_FILE"))
  136. SourceData::ignoredHeaders_.push_back(ExtractHeaderFile(compounddef));
  137. return;
  138. }
  139. }
  140. static string AddTrailingSlash(const string& pathName)
  141. {
  142. string ret = pathName;
  143. replace(ret.begin(), ret.end(), '\\', '/');
  144. if (!ret.empty() && ret.back() != '/')
  145. ret += '/';
  146. return ret;
  147. }
  148. static void GetXmlFiles(string dirPath, vector<string>& result)
  149. {
  150. result.clear();
  151. dirPath = AddTrailingSlash(dirPath);
  152. #ifdef _WIN32
  153. WIN32_FIND_DATAA findData;
  154. HANDLE handle = FindFirstFileA((dirPath + "*.xml").c_str(), &findData);
  155. if (handle == INVALID_HANDLE_VALUE)
  156. return;
  157. do
  158. {
  159. if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  160. result.push_back(dirPath + findData.cFileName);
  161. } while (FindNextFileA(handle, &findData));
  162. FindClose(handle);
  163. #else
  164. DIR* dir = opendir(dirPath.c_str());
  165. if (!dir)
  166. return;
  167. struct stat st;
  168. while (dirent* de = readdir(dir))
  169. {
  170. string filePath = dirPath + de->d_name;
  171. if (stat(filePath.c_str(), &st) != 0)
  172. continue;
  173. if (st.st_mode & S_IFDIR)
  174. continue;
  175. if (EndsWith(filePath, ".xml"))
  176. result.push_back(filePath);
  177. }
  178. closedir(dir);
  179. #endif
  180. }
  181. namespace SourceData
  182. {
  183. unordered_map<string, xml_node> classesByID_;
  184. unordered_map<string, xml_node> classesByName_;
  185. unordered_map<string, xml_node> members_;
  186. unordered_map<string, xml_node> enums_;
  187. xml_node namespaceUrho3D_;
  188. vector<xml_node> usings_;
  189. vector<string> defines_;
  190. vector<string> ignoredHeaders_;
  191. void LoadAllXmls(const string& dir)
  192. {
  193. cout << "Loading XMLs from " << dir << "\n";
  194. vector<string> fullPaths;
  195. GetXmlFiles(dir, fullPaths);
  196. cout << "Found " << fullPaths.size() << " xml files\n";
  197. for (string fullPath : fullPaths)
  198. LoadXml(fullPath);
  199. }
  200. }