XmlAnalyzer.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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 <cassert>
  26. #include <regex>
  27. using namespace std;
  28. using namespace pugi;
  29. extern string _sourceDir;
  30. string RemoveRefs(xml_node node)
  31. {
  32. assert(node.name() == string("type") || node.name() == string("defval") || node.name() == string("para"));
  33. string result;
  34. for (xml_node part : node.children())
  35. {
  36. if (part.name() == string("ref"))
  37. result += part.child_value();
  38. else
  39. result += part.value();
  40. }
  41. return result;
  42. }
  43. TypeAnalyzer::TypeAnalyzer(xml_node type, const TemplateSpecialization& specialization)
  44. {
  45. assert(type.name() == string("type"));
  46. fullType_ = RemoveRefs(type);
  47. fullType_ = RemoveFirst(fullType_, "URHO3D_API ");
  48. fullType_ = RemoveFirst(fullType_, " URHO3D_API");
  49. fullType_ = CutStart(fullType_, "volatile ");
  50. fullType_ = CutStart(fullType_, "constexpr ");
  51. fullType_ = ReplaceAll(fullType_, " *", "*");
  52. fullType_ = ReplaceAll(fullType_, " &", "&");
  53. fullType_ = ReplaceAll(fullType_, "< ", "<");
  54. fullType_ = ReplaceAll(fullType_, " >", ">");
  55. for (pair<const string, string> it : specialization)
  56. {
  57. regex rgx("\\b" + it.first + "\\b");
  58. fullType_ = regex_replace(fullType_, rgx, it.second);
  59. }
  60. isConst_ = StartsWith(fullType_, "const ");
  61. name_ = CutStart(fullType_, "const ");
  62. isRvalueReference_ = EndsWith(name_, "&&");
  63. name_ = CutEnd(name_, "&&");
  64. isRefToPoiner_ = EndsWith(name_, "*&");
  65. name_ = CutEnd(name_, "*&");
  66. isDoublePointer_ = EndsWith(name_, "**");
  67. name_ = CutEnd(name_, "**");
  68. isPointer_ = EndsWith(name_, "*");
  69. name_ = CutEnd(name_, "*");
  70. isReference_ = EndsWith(name_, "&");
  71. name_ = CutEnd(name_, "&");
  72. smatch match;
  73. if (regex_match(name_, match, regex("(.+?)<(.+)>")))
  74. {
  75. templateParams_ = match[2].str();
  76. name_ = match[1].str();
  77. }
  78. }
  79. TypeAnalyzer::TypeAnalyzer(const string& typeName)
  80. {
  81. fullType_ = typeName;
  82. name_ = typeName;
  83. isConst_ = false;
  84. isPointer_ = false;
  85. isReference_ = false;
  86. isRvalueReference_ = false;
  87. isDoublePointer_ = false;
  88. isRefToPoiner_ = false;
  89. }
  90. // ============================================================================
  91. ParamAnalyzer::ParamAnalyzer(xml_node param, const TemplateSpecialization& specialization)
  92. : node_(param)
  93. , specialization_(specialization)
  94. {
  95. assert(node_.name() == string("param"));
  96. }
  97. string ParamAnalyzer::ToString() const
  98. {
  99. string type = GetType().ToString();
  100. assert(!type.empty());
  101. string name = GetDeclname();
  102. assert(!name.empty());
  103. return type + " " + name;
  104. }
  105. TypeAnalyzer ParamAnalyzer::GetType() const
  106. {
  107. xml_node type = node_.child("type");
  108. assert(type);
  109. return TypeAnalyzer(type, specialization_);
  110. }
  111. string ParamAnalyzer::GetDeclname() const
  112. {
  113. string result = node_.child("declname").child_value();
  114. //assert(!result.empty()); TODO
  115. return result;
  116. }
  117. string ParamAnalyzer::GetDefval() const
  118. {
  119. xml_node defval = node_.child("defval");
  120. if (defval)
  121. return RemoveRefs(defval);
  122. return "";
  123. }
  124. // ============================================================================
  125. string ExtractCompoundname(xml_node compounddef)
  126. {
  127. assert(IsCompounddef(compounddef));
  128. string result = compounddef.child("compoundname").child_value();
  129. assert(!result.empty());
  130. return result;
  131. }
  132. xml_node FindSectiondef(xml_node compounddef, const string& kind)
  133. {
  134. assert(IsCompounddef(compounddef));
  135. assert(!kind.empty());
  136. for (xml_node sectiondef : compounddef.children("sectiondef"))
  137. {
  138. if (ExtractKind(sectiondef) == kind)
  139. return sectiondef;
  140. }
  141. return xml_node();
  142. }
  143. bool IsStatic(xml_node memberdef)
  144. {
  145. assert(IsMemberdef(memberdef));
  146. string staticAttr = memberdef.attribute("static").value();
  147. assert(!staticAttr.empty());
  148. return staticAttr == "yes";
  149. }
  150. bool IsExplicit(xml_node memberdef)
  151. {
  152. assert(IsMemberdef(memberdef));
  153. assert(ExtractKind(memberdef) == "function");
  154. string explicitAttr = memberdef.attribute("explicit").value();
  155. assert(!explicitAttr.empty());
  156. return explicitAttr == "yes";
  157. }
  158. string ExtractDefinition(xml_node memberdef)
  159. {
  160. assert(IsMemberdef(memberdef));
  161. string result = memberdef.child("definition").child_value();
  162. assert(!result.empty());
  163. return result;
  164. }
  165. static string BeautifyDefinition(const string& definition)
  166. {
  167. string result = definition;
  168. result = ReplaceAll(result, " **", "** ");
  169. result = ReplaceAll(result, " &&", "&& ");
  170. result = ReplaceAll(result, " *&", "*& ");
  171. result = ReplaceAll(result, " *", "* ");
  172. result = ReplaceAll(result, " &", "& ");
  173. result = ReplaceAll(result, "=", " = ");
  174. result = ReplaceAll(result, " = = ", "==");
  175. result = ReplaceAll(result, "! = ", "!=");
  176. result = ReplaceAll(result, "+ = ", "+=");
  177. result = ReplaceAll(result, "- = ", "-=");
  178. result = ReplaceAll(result, "* = ", "*=");
  179. result = ReplaceAll(result, "/ = ", "/=");
  180. result = ReplaceAll(result, "operator = ", "operator=");
  181. result = ReplaceAll(result, "operator", "operator ");
  182. while (Contains(result, " "))
  183. result = ReplaceAll(result, " ", " ");
  184. result = ReplaceAll(result, " )", ")");
  185. result = ReplaceAll(result, "< ", "<");
  186. result = ReplaceAll(result, " >", ">");
  187. result = ReplaceAll(result, "template<", "template <");
  188. return result;
  189. }
  190. string ExtractArgsstring(xml_node memberdef)
  191. {
  192. assert(IsMemberdef(memberdef));
  193. xml_node argsstring = memberdef.child("argsstring");
  194. assert(argsstring);
  195. return argsstring.child_value();
  196. }
  197. string ExtractCleanedFunctionArgsstring(xml_node memberdef)
  198. {
  199. assert(ExtractKind(memberdef) == "function");
  200. string argsstring = ExtractArgsstring(memberdef);
  201. assert(StartsWith(argsstring, "("));
  202. size_t endPos = argsstring.find_last_of(')');
  203. assert(endPos != string::npos);
  204. return argsstring.substr(1, endPos - 1);
  205. }
  206. string ExtractProt(xml_node memberdef)
  207. {
  208. assert(IsMemberdef(memberdef));
  209. string result = memberdef.attribute("prot").value();
  210. assert(!result.empty());
  211. return result;
  212. }
  213. TypeAnalyzer ExtractType(xml_node memberdef, const TemplateSpecialization& specialization)
  214. {
  215. assert(IsMemberdef(memberdef));
  216. xml_node type = memberdef.child("type");
  217. assert(type);
  218. // Doxygen bug workaround https://github.com/doxygen/doxygen/issues/7732
  219. // For user-defined conversion operator exract return type from function name
  220. if (RemoveRefs(type).empty() && ExtractKind(memberdef) == "function")
  221. {
  222. string functionName = ExtractName(memberdef);
  223. if (StartsWith(functionName, "operator "))
  224. return TypeAnalyzer(CutStart(functionName, "operator "));
  225. }
  226. return TypeAnalyzer(type, specialization);
  227. }
  228. vector<ParamAnalyzer> ExtractParams(xml_node memberdef, const TemplateSpecialization& specialization)
  229. {
  230. assert(IsMemberdef(memberdef));
  231. assert(ExtractKind(memberdef) == "function");
  232. vector<ParamAnalyzer> result;
  233. for (xml_node param : memberdef.children("param"))
  234. result.push_back(ParamAnalyzer(param, specialization));
  235. return result;
  236. }
  237. string JoinParamsTypes(xml_node memberdef, const TemplateSpecialization& specialization)
  238. {
  239. assert(IsMemberdef(memberdef));
  240. assert(ExtractKind(memberdef) == "function");
  241. string result;
  242. vector<ParamAnalyzer> params = ExtractParams(memberdef, specialization);
  243. for (const ParamAnalyzer& param : params)
  244. {
  245. if (!result.empty())
  246. result += ", ";
  247. result += param.GetType().ToString();
  248. }
  249. return result;
  250. }
  251. string JoinParamsNames(xml_node memberdef, bool skipContext)
  252. {
  253. assert(IsMemberdef(memberdef));
  254. assert(ExtractKind(memberdef) == "function");
  255. string result;
  256. vector<ParamAnalyzer> params = ExtractParams(memberdef);
  257. for (size_t i = 0; i < params.size(); i++)
  258. {
  259. ParamAnalyzer param = params[i];
  260. if (skipContext && i == 0)
  261. {
  262. assert(param.GetType().ToString() == "Context*");
  263. continue;
  264. }
  265. if (!result.empty())
  266. result += ", ";
  267. result += param.GetDeclname();
  268. }
  269. return result;
  270. }
  271. string ExtractID(xml_node node)
  272. {
  273. assert(IsMemberdef(node) || IsCompounddef(node));
  274. string result = node.attribute("id").value();
  275. assert(!result.empty());
  276. return result;
  277. }
  278. string ExtractKind(xml_node node)
  279. {
  280. assert(IsMemberdef(node) || IsCompounddef(node) || IsMember(node) || IsSectiondef(node));
  281. string result = node.attribute("kind").value();
  282. assert(!result.empty());
  283. return result;
  284. }
  285. string ExtractName(xml_node node)
  286. {
  287. assert(IsMemberdef(node) || IsEnumvalue(node));
  288. string result = node.child("name").child_value();
  289. assert(!result.empty());
  290. return result;
  291. }
  292. string ExtractLine(xml_node node)
  293. {
  294. assert(IsMemberdef(node) || IsCompounddef(node));
  295. string result = node.child("location").attribute("line").value();
  296. assert(!result.empty());
  297. return result;
  298. }
  299. string ExtractColumn(xml_node node)
  300. {
  301. assert(IsMemberdef(node) || IsCompounddef(node));
  302. string result = node.child("location").attribute("column").value();
  303. assert(!result.empty());
  304. return result;
  305. }
  306. static string DescriptionToString(xml_node description)
  307. {
  308. string result;
  309. for (xml_node para : description.children("para"))
  310. {
  311. result += RemoveRefs(para);
  312. result += " "; // To avoid gluing words from different paragraphs
  313. }
  314. return result;
  315. }
  316. string ExtractComment(xml_node node)
  317. {
  318. assert(IsMemberdef(node) || IsCompounddef(node));
  319. xml_node brief = node.child("briefdescription");
  320. assert(brief);
  321. xml_node detailed = node.child("detaileddescription");
  322. assert(detailed);
  323. return DescriptionToString(brief) + DescriptionToString(detailed);
  324. }
  325. static string HeaderFullPathToRelative(const string& fullPath)
  326. {
  327. assert(StartsWith(fullPath, _sourceDir + "/Source/Urho3D"));
  328. return ".." + CutStart(fullPath, _sourceDir + "/Source/Urho3D");
  329. }
  330. string ExtractHeaderFile(xml_node node)
  331. {
  332. assert(IsMemberdef(node) || IsCompounddef(node));
  333. xml_node location = node.child("location");
  334. assert(!location.empty());
  335. string declfile = location.attribute("declfile").value();
  336. if (EndsWith(declfile, ".h"))
  337. return HeaderFullPathToRelative(declfile);
  338. string file = location.attribute("file").value();
  339. if (EndsWith(file, ".h"))
  340. return HeaderFullPathToRelative(file);
  341. return string();
  342. }
  343. bool IsTemplate(xml_node node)
  344. {
  345. assert(IsMemberdef(node) || IsCompounddef(node));
  346. return node.child("templateparamlist");
  347. }
  348. vector<string> ExtractTemplateParams(xml_node node)
  349. {
  350. assert(IsMemberdef(node) || IsCompounddef(node));
  351. vector<string> result;
  352. xml_node templateparamlist = node.child("templateparamlist");
  353. for (xml_node param : templateparamlist.children("param"))
  354. {
  355. string type = param.child_value("type");
  356. type = CutStart(type, "class ");
  357. type = CutStart(type, "typename ");
  358. result.push_back(type);
  359. }
  360. return result;
  361. }
  362. // ============================================================================
  363. EnumAnalyzer::EnumAnalyzer(xml_node memberdef)
  364. : memberdef_(memberdef)
  365. {
  366. assert(IsMemberdef(memberdef));
  367. assert(ExtractKind(memberdef) == "enum");
  368. }
  369. string EnumAnalyzer::GetBaseType() const
  370. {
  371. string result = memberdef_.child("type").child_value();
  372. if (result.empty())
  373. return "int";
  374. return result;
  375. }
  376. string EnumAnalyzer::GetLocation() const
  377. {
  378. string baseType = GetBaseType();
  379. if (baseType == "int")
  380. return "enum " + GetTypeName() + " | File: " + GetHeaderFile();
  381. return "enum " + GetTypeName() + " : " + baseType + " | File: " + GetHeaderFile();
  382. }
  383. vector<string> EnumAnalyzer::GetEnumerators() const
  384. {
  385. vector<string> result;
  386. for (xml_node enumvalue : memberdef_.children("enumvalue"))
  387. result.push_back(ExtractName(enumvalue));
  388. return result;
  389. }
  390. // ============================================================================
  391. GlobalVariableAnalyzer::GlobalVariableAnalyzer(xml_node memberdef)
  392. : memberdef_(memberdef)
  393. {
  394. assert(IsMemberdef(memberdef));
  395. assert(ExtractKind(memberdef) == "variable");
  396. }
  397. // ============================================================================
  398. ClassAnalyzer::ClassAnalyzer(xml_node compounddef, const TemplateSpecialization& specialization)
  399. : compounddef_(compounddef)
  400. , specialization_(specialization)
  401. {
  402. assert(IsCompounddef(compounddef));
  403. }
  404. string ClassAnalyzer::GetClassName() const
  405. {
  406. string compoundname = ExtractCompoundname(compounddef_);
  407. assert(StartsWith(compoundname, "Urho3D::"));
  408. return CutStart(compoundname, "Urho3D::");
  409. }
  410. string ClassAnalyzer::GetDirName() const
  411. {
  412. string str = CutStart(GetHeaderFile(), "../");
  413. size_t pos = str.find('/');
  414. assert(pos != string::npos);
  415. return str.substr(0, pos);
  416. }
  417. bool ClassAnalyzer::IsInternal() const
  418. {
  419. if (GetHeaderFile().empty()) // Defined in *.cpp
  420. return true;
  421. if (Contains(GetClassName(), "::")) // Defined inside another class
  422. return true;
  423. return false;
  424. }
  425. vector<xml_node> ClassAnalyzer::GetMemberdefs() const
  426. {
  427. vector<xml_node> result;
  428. xml_node listofallmembers = compounddef_.child("listofallmembers");
  429. assert(listofallmembers);
  430. for (xml_node member : listofallmembers.children("member"))
  431. {
  432. xml_attribute ambiguityscope = member.attribute("ambiguityscope");
  433. if (!ambiguityscope.empty())
  434. continue;
  435. string refid = member.attribute("refid").value();
  436. assert(!refid.empty());
  437. auto it = SourceData::members_.find(refid);
  438. if (it == SourceData::members_.end())
  439. continue;
  440. xml_node memberdef = it->second;
  441. result.push_back(memberdef);
  442. }
  443. return result;
  444. }
  445. vector<string> ClassAnalyzer::GetAllPublicMembersRefids() const
  446. {
  447. vector<string> result;
  448. xml_node listofallmembers = compounddef_.child("listofallmembers");
  449. assert(listofallmembers);
  450. for (xml_node member : listofallmembers.children("member"))
  451. {
  452. string prot = member.attribute("prot").value();
  453. assert(!prot.empty());
  454. if (prot != "public")
  455. continue;
  456. xml_attribute ambiguityscope = member.attribute("ambiguityscope");
  457. if (!ambiguityscope.empty())
  458. continue;
  459. string refid = member.attribute("refid").value();
  460. assert(!refid.empty());
  461. result.push_back(refid);
  462. }
  463. return result;
  464. }
  465. static vector<xml_node> GetHiddenMemberdefs(const ClassAnalyzer& classAnalyzer)
  466. {
  467. vector<string> thisRefids = classAnalyzer.GetAllPublicMembersRefids();
  468. vector<ClassAnalyzer> baseClasses = classAnalyzer.GetBaseClasses();
  469. vector<xml_node> result;
  470. for (const ClassAnalyzer& baseClass : baseClasses)
  471. {
  472. vector<string> baseRefids = baseClass.GetAllPublicMembersRefids();
  473. for (const string& baseRefid : baseRefids)
  474. {
  475. if (CONTAINS(thisRefids, baseRefid))
  476. continue;
  477. auto it = SourceData::members_.find(baseRefid);
  478. if (it == SourceData::members_.end())
  479. continue;
  480. xml_node memberdef = it->second;
  481. result.push_back(memberdef);
  482. }
  483. }
  484. return result;
  485. }
  486. vector<string> ClassAnalyzer::GetHiddenMethods() const
  487. {
  488. vector<xml_node> hiddenMemberdefs = GetHiddenMemberdefs(*this);
  489. vector<string> result;
  490. for (xml_node memberdef : hiddenMemberdefs)
  491. {
  492. if (ExtractKind(memberdef) == "function" && !IsStatic(memberdef))
  493. result.push_back(GetFunctionDeclaration(memberdef));
  494. }
  495. sort(result.begin(), result.end());
  496. return result;
  497. }
  498. vector<string> ClassAnalyzer::GetHiddenStaticMethods() const
  499. {
  500. vector<xml_node> hiddenMemberdefs = GetHiddenMemberdefs(*this);
  501. vector<string> result;
  502. for (xml_node memberdef : hiddenMemberdefs)
  503. {
  504. if (ExtractKind(memberdef) == "function" && IsStatic(memberdef))
  505. result.push_back(GetFunctionDeclaration(memberdef));
  506. }
  507. sort(result.begin(), result.end());
  508. return result;
  509. }
  510. vector<string> ClassAnalyzer::GetHiddenFields() const
  511. {
  512. vector<xml_node> hiddenMemberdefs = GetHiddenMemberdefs(*this);
  513. vector<string> result;
  514. for (xml_node memberdef : hiddenMemberdefs)
  515. {
  516. if (ExtractKind(memberdef) == "variable" && !IsStatic(memberdef))
  517. result.push_back(GetVariableDeclaration(memberdef));
  518. }
  519. sort(result.begin(), result.end());
  520. return result;
  521. }
  522. vector<string> ClassAnalyzer::GetHiddenStaticFields() const
  523. {
  524. vector<xml_node> hiddenMemberdefs = GetHiddenMemberdefs(*this);
  525. vector<string> result;
  526. for (xml_node memberdef : hiddenMemberdefs)
  527. {
  528. if (ExtractKind(memberdef) == "variable" && IsStatic(memberdef))
  529. result.push_back(GetVariableDeclaration(memberdef));
  530. }
  531. sort(result.begin(), result.end());
  532. return result;
  533. }
  534. int ClassAnalyzer::GetInherianceDeep(int counter) const
  535. {
  536. vector<ClassAnalyzer> baseClasses = GetBaseClasses();
  537. int result = counter;
  538. for (const ClassAnalyzer& baseClass : baseClasses)
  539. {
  540. int ret = baseClass.GetInherianceDeep(counter + 1);
  541. if (ret > result)
  542. result = ret;
  543. }
  544. return result;
  545. }
  546. vector<MethodAnalyzer> ClassAnalyzer::GetAllMethods() const
  547. {
  548. vector<MethodAnalyzer> result;
  549. vector<xml_node> memberdefs = GetMemberdefs();
  550. for (xml_node memberdef : memberdefs)
  551. {
  552. if (ExtractKind(memberdef) == "function")
  553. result.push_back(MethodAnalyzer(*this, memberdef));
  554. }
  555. return result;
  556. }
  557. vector<MethodAnalyzer> ClassAnalyzer::GetAllPublicMethods() const
  558. {
  559. vector<MethodAnalyzer> result;
  560. vector<xml_node> memberdefs = GetMemberdefs();
  561. for (xml_node memberdef : memberdefs)
  562. {
  563. if (ExtractKind(memberdef) == "function" && ExtractProt(memberdef) == "public")
  564. result.push_back(MethodAnalyzer(*this, memberdef));
  565. }
  566. return result;
  567. }
  568. vector<MethodAnalyzer> ClassAnalyzer::GetThisPublicMethods() const
  569. {
  570. vector<MethodAnalyzer> result;
  571. xml_node sectiondef = FindSectiondef(compounddef_, "public-func");
  572. for (xml_node memberdef : sectiondef.children("memberdef"))
  573. {
  574. MethodAnalyzer methodAnalyzer(*this, memberdef);
  575. result.push_back(methodAnalyzer);
  576. }
  577. return result;
  578. }
  579. vector<MethodAnalyzer> ClassAnalyzer::GetThisPublicStaticMethods() const
  580. {
  581. vector<MethodAnalyzer> result;
  582. xml_node sectiondef = FindSectiondef(compounddef_, "public-static-func");
  583. for (xml_node memberdef : sectiondef.children("memberdef"))
  584. {
  585. MethodAnalyzer methodAnalyzer(*this, memberdef);
  586. result.push_back(methodAnalyzer);
  587. }
  588. return result;
  589. }
  590. vector<FieldAnalyzer> ClassAnalyzer::GetAllFields() const
  591. {
  592. vector<FieldAnalyzer> result;
  593. vector<xml_node> memberdefs = GetMemberdefs();
  594. for (xml_node memberdef : memberdefs)
  595. {
  596. if (ExtractKind(memberdef) == "variable")
  597. result.push_back(FieldAnalyzer(*this, memberdef));
  598. }
  599. return result;
  600. }
  601. vector<FieldAnalyzer> ClassAnalyzer::GetThisPublicFields() const
  602. {
  603. vector<FieldAnalyzer> result;
  604. xml_node sectiondef = FindSectiondef(compounddef_, "public-attrib");
  605. for (xml_node memberdef : sectiondef.children("memberdef"))
  606. {
  607. FieldAnalyzer fieldAnalyzer(*this, memberdef);
  608. result.push_back(fieldAnalyzer);
  609. }
  610. return result;
  611. }
  612. vector<FieldAnalyzer> ClassAnalyzer::GetThisPublicStaticFields() const
  613. {
  614. vector<FieldAnalyzer> result;
  615. xml_node sectiondef = FindSectiondef(compounddef_, "public-static-attrib");
  616. for (xml_node memberdef : sectiondef.children("memberdef"))
  617. {
  618. FieldAnalyzer fieldAnalyzer(*this, memberdef);
  619. result.push_back(fieldAnalyzer);
  620. }
  621. return result;
  622. }
  623. bool ClassAnalyzer::ContainsMethod(const string& name) const
  624. {
  625. vector<MethodAnalyzer> methods = GetAllMethods();
  626. for (const MethodAnalyzer& method : methods)
  627. {
  628. if (method.GetName() == name)
  629. return true;
  630. }
  631. return false;
  632. }
  633. shared_ptr<MethodAnalyzer> ClassAnalyzer::GetMethod(const string& name) const
  634. {
  635. vector<MethodAnalyzer> methods = GetAllMethods();
  636. for (const MethodAnalyzer& method : methods)
  637. {
  638. if (method.GetName() == name)
  639. return make_shared<MethodAnalyzer>(method);
  640. }
  641. return nullptr;
  642. }
  643. int ClassAnalyzer::NumMethods(const string& name) const
  644. {
  645. int result = 0;
  646. vector<MethodAnalyzer> methods = GetAllMethods();
  647. for (const MethodAnalyzer& method : methods)
  648. {
  649. if (method.GetName() == name)
  650. result++;
  651. }
  652. return result;
  653. }
  654. bool ClassAnalyzer::IsAbstract() const
  655. {
  656. vector<MethodAnalyzer> methods = GetAllMethods();
  657. for (const MethodAnalyzer& method : methods)
  658. {
  659. if (method.IsPureVirtual())
  660. {
  661. if (!IsRefCounted())
  662. return true;
  663. // Some pure virtual functions is implemented by URHO3D_OBJECT
  664. string name = method.GetName();
  665. if (name == "GetType" || name == "GetTypeInfo" || name == "GetTypeName")
  666. {
  667. if (ContainsMethod("URHO3D_OBJECT"))
  668. continue;
  669. }
  670. return true;
  671. }
  672. }
  673. return false;
  674. }
  675. bool ClassAnalyzer::AllFloats() const
  676. {
  677. if (Contains(GetComment(), "ALL_FLOATS")) // TODO: remove
  678. return true;
  679. vector<FieldAnalyzer> fields = GetAllFields();
  680. for (const FieldAnalyzer& field : fields)
  681. {
  682. if (field.IsStatic())
  683. continue;
  684. string type = field.GetType().ToString();
  685. if (type != "float" && type != "double")
  686. return false;
  687. }
  688. return true;
  689. }
  690. bool ClassAnalyzer::AllInts() const
  691. {
  692. if (Contains(GetComment(), "ALL_INTS")) // TODO: remove
  693. return true;
  694. vector<FieldAnalyzer> fields = GetAllFields();
  695. for (const FieldAnalyzer& field : fields)
  696. {
  697. if (field.IsStatic())
  698. continue;
  699. string type = field.GetType().ToString();
  700. if (type != "int" && type != "unsigned")
  701. return false;
  702. }
  703. return true;
  704. }
  705. bool ClassAnalyzer::IsPod() const
  706. {
  707. bool result = Contains(GetComment(), "IS_POD");
  708. if (AllFloats() || AllInts())
  709. result = true;
  710. return result;
  711. }
  712. shared_ptr<ClassAnalyzer> ClassAnalyzer::GetBaseClass() const
  713. {
  714. xml_node basecompoundref = compounddef_.child("basecompoundref");
  715. if (!basecompoundref)
  716. return shared_ptr<ClassAnalyzer>();
  717. string refid = basecompoundref.attribute("refid").value();
  718. assert(!refid.empty());
  719. auto it = SourceData::classesByID_.find(refid);
  720. if (it == SourceData::classesByID_.end())
  721. return shared_ptr<ClassAnalyzer>();
  722. xml_node compounddef = it->second;
  723. return make_shared<ClassAnalyzer>(compounddef);
  724. }
  725. vector<ClassAnalyzer> ClassAnalyzer::GetBaseClasses() const
  726. {
  727. // Workaround for bug in doxygen 1.9+
  728. // https://github.com/doxygen/doxygen/issues/8805
  729. // This can be removed in future
  730. vector<string> pushed_refids;
  731. vector<ClassAnalyzer> result;
  732. for (xml_node basecompoundref : compounddef_.children("basecompoundref"))
  733. {
  734. string refid = basecompoundref.attribute("refid").value();
  735. if (refid.empty()) // Type from ThirdParty lib
  736. continue;
  737. auto it = SourceData::classesByID_.find(refid);
  738. if (it == SourceData::classesByID_.end())
  739. continue;
  740. // This can be removed in future
  741. if (CONTAINS(pushed_refids, refid))
  742. continue;
  743. pushed_refids.push_back(refid);
  744. xml_node compounddef = it->second;
  745. result.push_back(ClassAnalyzer(compounddef));
  746. }
  747. return result;
  748. }
  749. inline bool Contains(const vector<ClassAnalyzer>& classes, const ClassAnalyzer& cl)
  750. {
  751. for (const ClassAnalyzer& c : classes)
  752. {
  753. // Comparison without template specialization
  754. if (c.GetCompounddef() == cl.GetCompounddef())
  755. return true;
  756. }
  757. return false;
  758. }
  759. static void RecursivelyGetBaseClasses(const ClassAnalyzer& analyzer, vector<ClassAnalyzer>& result)
  760. {
  761. for (const ClassAnalyzer& baseClass : analyzer.GetBaseClasses())
  762. {
  763. if (!Contains(result, baseClass))
  764. result.push_back(baseClass);
  765. RecursivelyGetBaseClasses(baseClass, result);
  766. }
  767. }
  768. vector<ClassAnalyzer> ClassAnalyzer::GetAllBaseClasses() const
  769. {
  770. vector<ClassAnalyzer> result;
  771. RecursivelyGetBaseClasses(*this, result);
  772. return result;
  773. }
  774. vector<ClassAnalyzer> ClassAnalyzer::GetDerivedClasses() const
  775. {
  776. vector<ClassAnalyzer> result;
  777. for (xml_node basecompoundref : compounddef_.children("derivedcompoundref"))
  778. {
  779. string refid = basecompoundref.attribute("refid").value();
  780. if (refid.empty()) // Type from ThirdParty lib
  781. continue;
  782. auto it = SourceData::classesByID_.find(refid);
  783. if (it == SourceData::classesByID_.end())
  784. continue;
  785. xml_node compounddef = it->second;
  786. result.push_back(ClassAnalyzer(compounddef));
  787. }
  788. return result;
  789. }
  790. static void RecursivelyGetDerivedClasses(const ClassAnalyzer& analyzer, vector<ClassAnalyzer>& result)
  791. {
  792. for (const ClassAnalyzer& derivedClass : analyzer.GetDerivedClasses())
  793. {
  794. if (!Contains(result, derivedClass))
  795. result.push_back(derivedClass);
  796. RecursivelyGetDerivedClasses(derivedClass, result);
  797. }
  798. }
  799. vector<ClassAnalyzer> ClassAnalyzer::GetAllDerivedClasses() const
  800. {
  801. vector<ClassAnalyzer> result;
  802. RecursivelyGetDerivedClasses(*this, result);
  803. return result;
  804. }
  805. bool ClassAnalyzer::HasThisConstructor() const
  806. {
  807. vector<MethodAnalyzer> methods = GetAllMethods();
  808. for (const MethodAnalyzer& method : methods)
  809. {
  810. if (method.IsThisConstructor())
  811. return true;
  812. }
  813. return false;
  814. }
  815. bool ClassAnalyzer::IsRefCounted() const
  816. {
  817. if (GetClassName() == "RefCounted")
  818. return true;
  819. vector<ClassAnalyzer> baseClasses = GetAllBaseClasses();
  820. for (const ClassAnalyzer& classAnalyzer : baseClasses)
  821. {
  822. if (classAnalyzer.GetClassName() == "RefCounted")
  823. return true;
  824. }
  825. return false;
  826. }
  827. shared_ptr<MethodAnalyzer> ClassAnalyzer::GetDefinedThisDefaultConstructor() const
  828. {
  829. vector<MethodAnalyzer> methods = GetAllMethods();
  830. for (const MethodAnalyzer& method : methods)
  831. {
  832. if (method.IsThisDefaultConstructor())
  833. return make_shared<MethodAnalyzer>(method);
  834. }
  835. return nullptr;
  836. }
  837. vector<MethodAnalyzer> ClassAnalyzer::GetThisNonDefaultConstructors() const
  838. {
  839. vector<MethodAnalyzer> result;
  840. vector<MethodAnalyzer> methods = GetAllMethods();
  841. for (const MethodAnalyzer& method : methods)
  842. {
  843. if (method.IsThisNonDefaultConstructor())
  844. result.push_back(method);
  845. }
  846. return result;
  847. }
  848. // ============================================================================
  849. FunctionAnalyzer::FunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  850. : memberdef_(memberdef)
  851. , specialization_(specialization)
  852. {
  853. assert(IsMemberdef(memberdef));
  854. assert(ExtractKind(memberdef) == "function");
  855. }
  856. // ============================================================================
  857. MethodAnalyzer::MethodAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  858. : FunctionAnalyzer(memberdef, specialization)
  859. , classAnalyzer_(classAnalyzer)
  860. {
  861. // Append template specialization from class
  862. specialization_.insert(classAnalyzer.GetSpecialization().begin(), classAnalyzer.GetSpecialization().end());
  863. }
  864. string MethodAnalyzer::GetVirt() const
  865. {
  866. string result = memberdef_.attribute("virt").value();
  867. assert(!result.empty());
  868. return result;
  869. }
  870. string MethodAnalyzer::GetContainsClassName() const
  871. {
  872. string argsstring = ExtractArgsstring(memberdef_);
  873. assert(!argsstring.empty());
  874. string prototype = ExtractDefinition(memberdef_) + argsstring;
  875. smatch match;
  876. regex_match(prototype, match, regex(".*Urho3D::(.+?)::.*"));
  877. assert(match.size());
  878. string result = match[1].str();
  879. return result;
  880. }
  881. /*
  882. string FieldAnalyzer::GetDeclaration() const
  883. {
  884. string definition = ExtractDefinition(memberdef_);
  885. assert(!definition.empty());
  886. // Remove Urho3D::
  887. smatch match;
  888. regex_match(definition, match, regex("(.*)Urho3D::(.+)"));
  889. assert(match.size() == 3);
  890. string result = match[1].str() + match[2].str();
  891. result = BeautifyDefinition(result);
  892. return result;
  893. }
  894. */
  895. string GetVariableDeclaration(xml_node memberdef)
  896. {
  897. assert(IsMemberdef(memberdef));
  898. assert(ExtractKind(memberdef) == "variable");
  899. string result = ExtractDefinition(memberdef);
  900. result = RemoveFirst(result, "URHO3D_API ");
  901. result = RemoveFirst(result, " URHO3D_API");
  902. assert(Contains(result, " Urho3D::"));
  903. result = ReplaceFirst(result, " Urho3D::", " ");
  904. if (IsStatic(memberdef))
  905. result = "static " + result;
  906. result = BeautifyDefinition(result);
  907. return result;
  908. }
  909. string GetFunctionDeclaration(xml_node memberdef)
  910. {
  911. assert(IsMemberdef(memberdef));
  912. assert(ExtractKind(memberdef) == "function");
  913. string argsstring = ExtractArgsstring(memberdef);
  914. assert(!argsstring.empty());
  915. string prototype = ExtractDefinition(memberdef) + argsstring;
  916. smatch match;
  917. regex_match(prototype, match, regex("([^(]*)Urho3D::(.+?)"));
  918. assert(match.size());
  919. string result = match[1].str() + match[2].str();
  920. if (IsExplicit(memberdef))
  921. result = "explicit " + result;
  922. if (IsTemplate(memberdef))
  923. {
  924. string t = "";
  925. xml_node templateparamlist = memberdef.child("templateparamlist");
  926. for (xml_node param : templateparamlist.children("param"))
  927. {
  928. if (t.length() > 0)
  929. t += ", ";
  930. xml_node type = param.child("type");
  931. assert(type);
  932. t += type.child_value();
  933. xml_node declname = param.child("declname");
  934. if (!declname.empty())
  935. t += " " + string(declname.child_value());
  936. }
  937. result = "template<" + t + "> " + result;
  938. }
  939. result = RemoveFirst(result, "URHO3D_API ");
  940. result = RemoveFirst(result, " URHO3D_API");
  941. result = BeautifyDefinition(result);
  942. return result;
  943. }
  944. string GetFunctionLocation(xml_node memberdef)
  945. {
  946. return GetFunctionDeclaration(memberdef) + " | File: " + ExtractHeaderFile(memberdef);
  947. }
  948. bool MethodAnalyzer::IsConst() const
  949. {
  950. string constAttr = memberdef_.attribute("const").value();
  951. assert(!constAttr.empty());
  952. return constAttr == "yes";
  953. }
  954. bool MethodAnalyzer::CanBeGetProperty() const
  955. {
  956. string returnType = GetReturnType().ToString();
  957. if (returnType == "void" || returnType.empty())
  958. return false;
  959. if (GetParams().size() != 0 && GetParams().size() != 1)
  960. return false;
  961. return true;
  962. }
  963. bool MethodAnalyzer::CanBeSetProperty() const
  964. {
  965. string returnType = GetReturnType().ToString();
  966. if (returnType != "void")
  967. return false;
  968. if (GetParams().size() != 1 && GetParams().size() != 2)
  969. return false;
  970. return true;
  971. }
  972. bool MethodAnalyzer::IsParentDestructor() const
  973. {
  974. string functionName = GetName();
  975. if (!StartsWith(functionName, "~"))
  976. return false;
  977. return !IsThisDestructor();
  978. }
  979. bool MethodAnalyzer::IsConstructor() const
  980. {
  981. string name = GetName();
  982. return ExtractDefinition(memberdef_) == "Urho3D::" + name + "::" + name;
  983. }
  984. bool MethodAnalyzer::IsDestructor() const
  985. {
  986. string name = GetName();
  987. return StartsWith(name, "~");
  988. }
  989. bool MethodAnalyzer::IsParentConstructor() const
  990. {
  991. if (IsThisConstructor())
  992. return false;
  993. string name = GetName();
  994. return ExtractDefinition(memberdef_) == "Urho3D::" + name + "::" + name;
  995. }
  996. shared_ptr<MethodAnalyzer> MethodAnalyzer::Reimplements() const
  997. {
  998. xml_node reimplements = memberdef_.child("reimplements");
  999. if (!reimplements)
  1000. return shared_ptr<MethodAnalyzer>();
  1001. string refid = reimplements.attribute("refid").value();
  1002. assert(!refid.empty());
  1003. auto it = SourceData::members_.find(refid);
  1004. if (it == SourceData::members_.end())
  1005. return shared_ptr<MethodAnalyzer>();
  1006. xml_node memberdef = it->second;
  1007. return make_shared<MethodAnalyzer>(classAnalyzer_, memberdef);
  1008. }
  1009. // ============================================================================
  1010. FieldAnalyzer::FieldAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  1011. : classAnalyzer_(classAnalyzer)
  1012. , memberdef_(memberdef)
  1013. {
  1014. assert(IsMemberdef(memberdef));
  1015. assert(ExtractKind(memberdef) == "variable");
  1016. }
  1017. string FieldAnalyzer::GetLocation() const
  1018. {
  1019. string result = GetDeclaration();
  1020. result += " | File: " + GetHeaderFile();
  1021. if (!classAnalyzer_.usingLocation_.empty())
  1022. result = classAnalyzer_.usingLocation_ + " | " + result;
  1023. return result;
  1024. }
  1025. // ============================================================================
  1026. NamespaceAnalyzer::NamespaceAnalyzer(xml_node compounddef)
  1027. : compounddef_(compounddef)
  1028. {
  1029. assert(IsCompounddef(compounddef));
  1030. assert(ExtractKind(compounddef) == "namespace");
  1031. }
  1032. vector<EnumAnalyzer> NamespaceAnalyzer::GetEnums()
  1033. {
  1034. xml_node sectiondef = FindSectiondef(compounddef_, "enum");
  1035. assert(sectiondef);
  1036. vector<EnumAnalyzer> result;
  1037. for (xml_node memberdef : sectiondef.children("memberdef"))
  1038. {
  1039. EnumAnalyzer analyzer(memberdef);
  1040. result.push_back(analyzer);
  1041. }
  1042. return result;
  1043. }
  1044. vector<GlobalVariableAnalyzer> NamespaceAnalyzer::GetVariables()
  1045. {
  1046. xml_node sectiondef = FindSectiondef(compounddef_, "var");
  1047. assert(sectiondef);
  1048. vector<GlobalVariableAnalyzer> result;
  1049. for (xml_node memberdef : sectiondef.children("memberdef"))
  1050. result.push_back(GlobalVariableAnalyzer(memberdef));
  1051. return result;
  1052. }
  1053. vector<GlobalFunctionAnalyzer> NamespaceAnalyzer::GetFunctions()
  1054. {
  1055. xml_node sectiondef = FindSectiondef(compounddef_, "func");
  1056. assert(sectiondef);
  1057. vector<GlobalFunctionAnalyzer> result;
  1058. for (xml_node memberdef : sectiondef.children("memberdef"))
  1059. result.push_back(GlobalFunctionAnalyzer(memberdef));
  1060. return result;
  1061. }
  1062. // ============================================================================
  1063. UsingAnalyzer::UsingAnalyzer(xml_node memberdef)
  1064. : memberdef_(memberdef)
  1065. {
  1066. assert(IsMemberdef(memberdef));
  1067. assert(ExtractKind(memberdef) == "typedef");
  1068. }
  1069. // ============================================================================
  1070. GlobalFunctionAnalyzer::GlobalFunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  1071. : FunctionAnalyzer(memberdef, specialization)
  1072. {
  1073. }
  1074. // ============================================================================
  1075. ClassStaticFunctionAnalyzer::ClassStaticFunctionAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  1076. : FunctionAnalyzer(memberdef, specialization)
  1077. , classAnalyzer_(classAnalyzer)
  1078. {
  1079. assert(IsStatic(memberdef));
  1080. }