XmlAnalyzer.cpp 36 KB

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