XmlSourceData.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // Copyright (c) 2008-2020 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. static void LoadXml(const string& fullPath)
  37. {
  38. // All loaded XMLs. Not used directly, just prevents destruction
  39. static vector<shared_ptr<xml_document> > xmlStorage;
  40. // Load and store XML file
  41. shared_ptr<xml_document> xmlDocument = make_shared<xml_document>();
  42. xml_parse_result parseResult = xmlDocument->load_file(fullPath.c_str());
  43. assert(parseResult);
  44. xmlStorage.push_back(xmlDocument);
  45. xml_node doxygenindex = xmlDocument->child("doxygenindex");
  46. if (doxygenindex) // index.xml
  47. {
  48. // Fill defines_
  49. for (xml_node compound : doxygenindex.children("compound"))
  50. {
  51. for (xml_node member : compound.children("member"))
  52. {
  53. string member_kind = ExtractKind(member);
  54. if (member_kind == "define")
  55. {
  56. string name = member.child("name").child_value();
  57. SourceData::defines_.push_back(name);
  58. }
  59. }
  60. }
  61. return;
  62. }
  63. xml_node compounddef = xmlDocument->child("doxygen").child("compounddef");
  64. if (!compounddef)
  65. return;
  66. string compounddef_kind = ExtractKind(compounddef);
  67. if (compounddef_kind == "struct" || compounddef_kind == "class")
  68. {
  69. // Fill classes_
  70. string id = compounddef.attribute("id").value();
  71. assert(!id.empty());
  72. SourceData::classesByID_.insert({ id, compounddef });
  73. // Fill classesByName_
  74. string compoundname = ExtractCompoundname(compounddef);
  75. if (StartsWith(compoundname, "Urho3D::"))
  76. {
  77. string name = CutStart(compoundname, "Urho3D::");
  78. if (!Contains(name, "::"))
  79. SourceData::classesByName_.insert({ name, compounddef });
  80. }
  81. // Fill members_
  82. for (xml_node sectiondef : compounddef.children("sectiondef"))
  83. {
  84. for (xml_node memberdef : sectiondef.children("memberdef"))
  85. {
  86. string memberdef_kind = ExtractKind(memberdef);
  87. if (memberdef_kind != "variable" && memberdef_kind != "function")
  88. continue;
  89. string member_id = ExtractID(memberdef);
  90. SourceData::members_.insert({ member_id, memberdef });
  91. }
  92. }
  93. return;
  94. }
  95. if (compounddef_kind == "namespace")
  96. {
  97. string compoundname = compounddef.child("compoundname").child_value();
  98. if (compoundname != "Urho3D")
  99. return;
  100. // Init namespaceUrho3D_
  101. SourceData::namespaceUrho3D_ = compounddef;
  102. // Fill usings_
  103. //
  104. // <compounddef kind="namespace">
  105. // <sectiondef kind="typedef">
  106. // <memberdef kind="typedef">...</memberdef>
  107. // <memberdef kind="typedef">...</memberdef>
  108. xml_node typedefs = FindSectiondef(compounddef, "typedef");
  109. assert(typedefs);
  110. for (xml_node memberdef : typedefs.children("memberdef"))
  111. {
  112. string definition = ExtractDefinition(memberdef);
  113. if (StartsWith(definition, "using "))
  114. SourceData::usings_.push_back(memberdef);
  115. }
  116. // <compounddef kind="namespace">
  117. // <sectiondef kind="enum">
  118. // <memberdef kind="enum">...</memberdef>
  119. // <memberdef kind="enum">...</memberdef>
  120. xml_node enums = FindSectiondef(compounddef, "enum");
  121. assert(enums);
  122. for (xml_node memberdef : enums.children("memberdef"))
  123. {
  124. string name = ExtractName(memberdef);
  125. SourceData::enums_.insert({ name, memberdef });
  126. }
  127. return;
  128. }
  129. if (compounddef_kind == "file")
  130. {
  131. // Fill ignoredHeaders_
  132. string comment = ExtractComment(compounddef);
  133. if (Contains(comment, "NO_BIND_FILE"))
  134. SourceData::ignoredHeaders_.push_back(ExtractHeaderFile(compounddef));
  135. return;
  136. }
  137. }
  138. static string AddTrailingSlash(const string& pathName)
  139. {
  140. string ret = pathName;
  141. replace(ret.begin(), ret.end(), '\\', '/');
  142. if (!ret.empty() && ret.back() != '/')
  143. ret += '/';
  144. return ret;
  145. }
  146. static void GetXmlFiles(string dirPath, vector<string>& result)
  147. {
  148. result.clear();
  149. dirPath = AddTrailingSlash(dirPath);
  150. #ifdef _WIN32
  151. WIN32_FIND_DATAA findData;
  152. HANDLE handle = FindFirstFileA((dirPath + "*.xml").c_str(), &findData);
  153. if (handle == INVALID_HANDLE_VALUE)
  154. return;
  155. do
  156. {
  157. if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  158. result.push_back(dirPath + findData.cFileName);
  159. } while (FindNextFileA(handle, &findData));
  160. FindClose(handle);
  161. #else
  162. DIR* dir = opendir(dirPath.c_str());
  163. if (!dir)
  164. return;
  165. struct stat st;
  166. while (dirent* de = readdir(dir))
  167. {
  168. string filePath = dirPath + de->d_name;
  169. if (stat(filePath.c_str(), &st) != 0)
  170. continue;
  171. if (st.st_mode & S_IFDIR)
  172. continue;
  173. if (EndsWith(filePath, ".xml"))
  174. result.push_back(filePath);
  175. }
  176. closedir(dir);
  177. #endif
  178. }
  179. namespace SourceData
  180. {
  181. unordered_map<string, xml_node> classesByID_;
  182. unordered_map<string, xml_node> classesByName_;
  183. unordered_map<string, xml_node> members_;
  184. unordered_map<string, xml_node> enums_;
  185. xml_node namespaceUrho3D_;
  186. vector<xml_node> usings_;
  187. vector<string> defines_;
  188. vector<string> ignoredHeaders_;
  189. void LoadAllXmls(const string& dir)
  190. {
  191. cout << "Loading XMLs from " << dir << "\n";
  192. vector<string> fullPaths;
  193. GetXmlFiles(dir, fullPaths);
  194. cout << "Found " << fullPaths.size() << " xml files\n";
  195. for (string fullPath : fullPaths)
  196. LoadXml(fullPath);
  197. }
  198. }