XmlAnalyzer.cpp 35 KB

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