XmlAnalyzer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Utils.h"
  23. #include "XmlAnalyzer.h"
  24. #include "XmlSourceData.h"
  25. #include <cassert>
  26. #include <regex>
  27. string RemoveRefs(xml_node node)
  28. {
  29. assert(node.name() == string("type") || node.name() == string("defval") || node.name() == string("para"));
  30. string result;
  31. for (xml_node part : node.children())
  32. {
  33. if (part.name() == string("ref"))
  34. result += part.child_value();
  35. else
  36. result += part.value();
  37. }
  38. return result;
  39. }
  40. TypeAnalyzer::TypeAnalyzer(xml_node type, const TemplateSpecialization& specialization)
  41. {
  42. assert(type.name() == string("type"));
  43. fullType_ = RemoveRefs(type);
  44. fullType_ = RemoveFirst(fullType_, "URHO3D_API ");
  45. fullType_ = RemoveFirst(fullType_, " URHO3D_API");
  46. fullType_ = CutStart(fullType_, "constexpr ");
  47. fullType_ = ReplaceAll(fullType_, " *", "*");
  48. fullType_ = ReplaceAll(fullType_, " &", "&");
  49. fullType_ = ReplaceAll(fullType_, "< ", "<");
  50. fullType_ = ReplaceAll(fullType_, " >", ">");
  51. for (pair<const string, string> it : specialization)
  52. {
  53. regex rgx("\\b" + it.first + "\\b");
  54. fullType_ = regex_replace(fullType_, rgx, it.second);
  55. }
  56. isConst_ = StartsWith(fullType_, "const ");
  57. name_ = CutStart(fullType_, "const ");
  58. isRvalueReference_ = EndsWith(name_, "&&");
  59. name_ = CutEnd(name_, "&&");
  60. isRefToPoiner_ = EndsWith(name_, "*&");
  61. name_ = CutEnd(name_, "*&");
  62. isDoublePointer_ = EndsWith(name_, "**");
  63. name_ = CutEnd(name_, "**");
  64. isPointer_ = EndsWith(name_, "*");
  65. name_ = CutEnd(name_, "*");
  66. isReference_ = EndsWith(name_, "&");
  67. name_ = CutEnd(name_, "&");
  68. smatch match;
  69. if (regex_match(name_, match, regex("(.+?)<(.+)>")))
  70. {
  71. templateParams_ = match[2].str();
  72. name_ = match[1].str();
  73. }
  74. }
  75. TypeAnalyzer::TypeAnalyzer(const string& typeName)
  76. {
  77. fullType_ = typeName;
  78. name_ = typeName;
  79. isConst_ = false;
  80. isPointer_ = false;
  81. isReference_ = false;
  82. isRvalueReference_ = false;
  83. isDoublePointer_ = false;
  84. isRefToPoiner_ = false;
  85. }
  86. // ============================================================================
  87. ParamAnalyzer::ParamAnalyzer(xml_node param, const TemplateSpecialization& specialization)
  88. : node_(param)
  89. , specialization_(specialization)
  90. {
  91. assert(node_.name() == string("param"));
  92. }
  93. string ParamAnalyzer::ToString() const
  94. {
  95. string type = GetType().ToString();
  96. assert(!type.empty());
  97. string name = GetDeclname();
  98. assert(!name.empty());
  99. return type + " " + name;
  100. }
  101. TypeAnalyzer ParamAnalyzer::GetType() const
  102. {
  103. xml_node type = node_.child("type");
  104. assert(type);
  105. return TypeAnalyzer(type, specialization_);
  106. }
  107. string ParamAnalyzer::GetDeclname() const
  108. {
  109. string result = node_.child("declname").child_value();
  110. assert(!result.empty());
  111. return result;
  112. }
  113. string ParamAnalyzer::GetDefval() const
  114. {
  115. xml_node defval = node_.child("defval");
  116. if (defval)
  117. return RemoveRefs(defval);
  118. return "";
  119. }
  120. // ============================================================================
  121. string ExtractCompoundname(xml_node compounddef)
  122. {
  123. assert(IsCompounddef(compounddef));
  124. string result = compounddef.child("compoundname").child_value();
  125. assert(!result.empty());
  126. return result;
  127. }
  128. xml_node FindSectiondef(xml_node compounddef, const string& kind)
  129. {
  130. assert(IsCompounddef(compounddef));
  131. assert(!kind.empty());
  132. for (xml_node sectiondef : compounddef.children("sectiondef"))
  133. {
  134. if (ExtractKind(sectiondef) == kind)
  135. return sectiondef;
  136. }
  137. return xml_node();
  138. }
  139. bool IsStatic(xml_node memberdef)
  140. {
  141. assert(IsMemberdef(memberdef));
  142. string staticAttr = memberdef.attribute("static").value();
  143. assert(!staticAttr.empty());
  144. return staticAttr == "yes";
  145. }
  146. bool IsExplicit(xml_node memberdef)
  147. {
  148. assert(IsMemberdef(memberdef));
  149. assert(ExtractKind(memberdef) == "function");
  150. string explicitAttr = memberdef.attribute("explicit").value();
  151. assert(!explicitAttr.empty());
  152. return explicitAttr == "yes";
  153. }
  154. string ExtractDefinition(xml_node memberdef)
  155. {
  156. assert(IsMemberdef(memberdef));
  157. string result = memberdef.child("definition").child_value();
  158. assert(!result.empty());
  159. return result;
  160. }
  161. static string BeautifyDefinition(const string& definition)
  162. {
  163. string result = definition;
  164. result = ReplaceAll(result, " **", "** ");
  165. result = ReplaceAll(result, " &&", "&& ");
  166. result = ReplaceAll(result, " *&", "*& ");
  167. result = ReplaceAll(result, " *", "* ");
  168. result = ReplaceAll(result, " &", "& ");
  169. while (Contains(result, " "))
  170. result = ReplaceAll(result, " ", " ");
  171. result = ReplaceAll(result, " )", ")");
  172. result = ReplaceAll(result, "< ", "<");
  173. result = ReplaceAll(result, " >", ">");
  174. return result;
  175. }
  176. string ExtractArgsstring(xml_node memberdef)
  177. {
  178. assert(IsMemberdef(memberdef));
  179. xml_node argsstring = memberdef.child("argsstring");
  180. assert(argsstring);
  181. return argsstring.child_value();
  182. }
  183. string ExtractCleanedFunctionArgsstring(xml_node memberdef)
  184. {
  185. assert(ExtractKind(memberdef) == "function");
  186. string argsstring = ExtractArgsstring(memberdef);
  187. assert(StartsWith(argsstring, "("));
  188. size_t endPos = argsstring.find_last_of(')');
  189. assert(endPos != string::npos);
  190. return argsstring.substr(1, endPos - 1);
  191. }
  192. string ExtractProt(xml_node memberdef)
  193. {
  194. assert(IsMemberdef(memberdef));
  195. string result = memberdef.attribute("prot").value();
  196. assert(!result.empty());
  197. return result;
  198. }
  199. TypeAnalyzer ExtractType(xml_node memberdef, const TemplateSpecialization& specialization)
  200. {
  201. assert(IsMemberdef(memberdef));
  202. xml_node type = memberdef.child("type");
  203. assert(type);
  204. // Doxygen bug workaround https://github.com/doxygen/doxygen/issues/7732
  205. // For user-defined conversion operator exract return type from function name
  206. if (RemoveRefs(type).empty() && ExtractKind(memberdef) == "function")
  207. {
  208. string functionName = ExtractName(memberdef);
  209. if (StartsWith(functionName, "operator "))
  210. return TypeAnalyzer(CutStart(functionName, "operator "));
  211. }
  212. return TypeAnalyzer(type, specialization);
  213. }
  214. vector<ParamAnalyzer> ExtractParams(xml_node memberdef, const TemplateSpecialization& specialization)
  215. {
  216. assert(IsMemberdef(memberdef));
  217. assert(ExtractKind(memberdef) == "function");
  218. vector<ParamAnalyzer> result;
  219. for (xml_node param : memberdef.children("param"))
  220. result.push_back(ParamAnalyzer(param, specialization));
  221. return result;
  222. }
  223. string JoinParamsTypes(xml_node memberdef, const TemplateSpecialization& specialization)
  224. {
  225. assert(IsMemberdef(memberdef));
  226. assert(ExtractKind(memberdef) == "function");
  227. string result;
  228. vector<ParamAnalyzer> params = ExtractParams(memberdef, specialization);
  229. for (const ParamAnalyzer& param : params)
  230. {
  231. if (!result.empty())
  232. result += ", ";
  233. result += param.GetType().ToString();
  234. }
  235. return result;
  236. }
  237. string JoinParamsNames(xml_node memberdef, bool skipContext)
  238. {
  239. assert(IsMemberdef(memberdef));
  240. assert(ExtractKind(memberdef) == "function");
  241. string result;
  242. vector<ParamAnalyzer> params = ExtractParams(memberdef);
  243. for (size_t i = 0; i < params.size(); i++)
  244. {
  245. ParamAnalyzer param = params[i];
  246. if (skipContext && i == 0)
  247. {
  248. assert(param.GetType().ToString() == "Context*");
  249. continue;
  250. }
  251. if (!result.empty())
  252. result += ", ";
  253. result += param.GetDeclname();
  254. }
  255. return result;
  256. }
  257. string ExtractID(xml_node node)
  258. {
  259. assert(IsMemberdef(node) || IsCompounddef(node));
  260. string result = node.attribute("id").value();
  261. assert(!result.empty());
  262. return result;
  263. }
  264. string ExtractKind(xml_node node)
  265. {
  266. assert(IsMemberdef(node) || IsCompounddef(node) || IsMember(node) || IsSectiondef(node));
  267. string result = node.attribute("kind").value();
  268. assert(!result.empty());
  269. return result;
  270. }
  271. string ExtractName(xml_node node)
  272. {
  273. assert(IsMemberdef(node) || IsEnumvalue(node));
  274. string result = node.child("name").child_value();
  275. assert(!result.empty());
  276. return result;
  277. }
  278. string ExtractLine(xml_node node)
  279. {
  280. assert(IsMemberdef(node) || IsCompounddef(node));
  281. string result = node.child("location").attribute("line").value();
  282. assert(!result.empty());
  283. return result;
  284. }
  285. string ExtractColumn(xml_node node)
  286. {
  287. assert(IsMemberdef(node) || IsCompounddef(node));
  288. string result = node.child("location").attribute("column").value();
  289. assert(!result.empty());
  290. return result;
  291. }
  292. static string DescriptionToString(xml_node description)
  293. {
  294. string result;
  295. for (xml_node para : description.children("para"))
  296. {
  297. result += RemoveRefs(para);
  298. result += " "; // To avoid gluing words from different paragraphs
  299. }
  300. return result;
  301. }
  302. string ExtractComment(xml_node node)
  303. {
  304. assert(IsMemberdef(node) || IsCompounddef(node));
  305. xml_node brief = node.child("briefdescription");
  306. assert(brief);
  307. xml_node detailed = node.child("detaileddescription");
  308. assert(detailed);
  309. return DescriptionToString(brief) + DescriptionToString(detailed);
  310. }
  311. static string HeaderFullPathToRelative(const string& fullPath)
  312. {
  313. size_t pos = fullPath.rfind("Source/Urho3D");
  314. assert(pos != string::npos);
  315. return ".." + fullPath.substr(pos + strlen("Source/Urho3D"));
  316. }
  317. string ExtractHeaderFile(xml_node node)
  318. {
  319. assert(IsMemberdef(node) || IsCompounddef(node));
  320. xml_node location = node.child("location");
  321. assert(!location.empty());
  322. string declfile = location.attribute("declfile").value();
  323. if (EndsWith(declfile, ".h"))
  324. return HeaderFullPathToRelative(declfile);
  325. string file = location.attribute("file").value();
  326. if (EndsWith(file, ".h"))
  327. return HeaderFullPathToRelative(file);
  328. return string();
  329. }
  330. bool IsTemplate(xml_node node)
  331. {
  332. assert(IsMemberdef(node) || IsCompounddef(node));
  333. return node.child("templateparamlist");
  334. }
  335. vector<string> ExtractTemplateParams(xml_node node)
  336. {
  337. assert(IsMemberdef(node) || IsCompounddef(node));
  338. vector<string> result;
  339. xml_node templateparamlist = node.child("templateparamlist");
  340. for (xml_node param : templateparamlist.children("param"))
  341. {
  342. string type = param.child_value("type");
  343. type = CutStart(type, "class ");
  344. type = CutStart(type, "typename ");
  345. result.push_back(type);
  346. }
  347. return result;
  348. }
  349. // ============================================================================
  350. EnumAnalyzer::EnumAnalyzer(xml_node memberdef)
  351. : memberdef_(memberdef)
  352. {
  353. assert(IsMemberdef(memberdef));
  354. assert(ExtractKind(memberdef) == "enum");
  355. }
  356. string EnumAnalyzer::GetBaseType() const
  357. {
  358. string result = memberdef_.child("type").child_value();
  359. if (result.empty())
  360. return "int";
  361. return result;
  362. }
  363. string EnumAnalyzer::GetLocation() const
  364. {
  365. string baseType = GetBaseType();
  366. if (baseType == "int")
  367. return "enum " + GetTypeName() + " | File: " + GetHeaderFile();
  368. return "enum " + GetTypeName() + " : " + baseType + " | File: " + GetHeaderFile();
  369. }
  370. vector<string> EnumAnalyzer::GetEnumerators() const
  371. {
  372. vector<string> result;
  373. for (xml_node enumvalue : memberdef_.children("enumvalue"))
  374. result.push_back(ExtractName(enumvalue));
  375. return result;
  376. }
  377. // ============================================================================
  378. GlobalVariableAnalyzer::GlobalVariableAnalyzer(xml_node memberdef)
  379. : memberdef_(memberdef)
  380. {
  381. assert(IsMemberdef(memberdef));
  382. assert(ExtractKind(memberdef) == "variable");
  383. }
  384. string GlobalVariableAnalyzer::GetLocation() const
  385. {
  386. string result = ExtractDefinition(memberdef_);
  387. result = RemoveFirst(result, "URHO3D_API ");
  388. result = RemoveFirst(result, " URHO3D_API");
  389. assert(Contains(result, " Urho3D::"));
  390. result = ReplaceFirst(result, " Urho3D::", " ");
  391. if (IsStatic())
  392. result = "static " + result;
  393. result = BeautifyDefinition(result);
  394. result += " | File: " + GetHeaderFile();
  395. return result;
  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. bool ClassAnalyzer::IsInternal() const
  411. {
  412. if (GetHeaderFile().empty()) // Defined in *.cpp
  413. return true;
  414. if (Contains(GetClassName(), "::")) // Defined inside another class
  415. return true;
  416. return false;
  417. }
  418. vector<xml_node> ClassAnalyzer::GetMemberdefs() const
  419. {
  420. vector<xml_node> result;
  421. xml_node listofallmembers = compounddef_.child("listofallmembers");
  422. assert(listofallmembers);
  423. for (xml_node member : listofallmembers.children("member"))
  424. {
  425. xml_attribute ambiguityscope = member.attribute("ambiguityscope");
  426. if (!ambiguityscope.empty()) // Overridden method from parent class
  427. continue;
  428. string refid = member.attribute("refid").value();
  429. assert(!refid.empty());
  430. auto it = SourceData::members_.find(refid);
  431. if (it == SourceData::members_.end())
  432. continue;
  433. xml_node memberdef = it->second;
  434. result.push_back(memberdef);
  435. }
  436. return result;
  437. }
  438. vector<MethodAnalyzer> ClassAnalyzer::GetAllMethods() const
  439. {
  440. vector<MethodAnalyzer> result;
  441. vector<xml_node> memberdefs = GetMemberdefs();
  442. for (xml_node memberdef : memberdefs)
  443. {
  444. if (ExtractKind(memberdef) == "function")
  445. result.push_back(MethodAnalyzer(*this, memberdef));
  446. }
  447. return result;
  448. }
  449. vector<MethodAnalyzer> ClassAnalyzer::GetThisPublicMethods() const
  450. {
  451. vector<MethodAnalyzer> result;
  452. xml_node sectiondef = FindSectiondef(compounddef_, "public-func");
  453. for (xml_node memberdef : sectiondef.children("memberdef"))
  454. {
  455. MethodAnalyzer methodAnalyzer(*this, memberdef);
  456. result.push_back(methodAnalyzer);
  457. }
  458. return result;
  459. }
  460. vector<ClassVariableAnalyzer> ClassAnalyzer::GetVariables() const
  461. {
  462. vector<ClassVariableAnalyzer> result;
  463. vector<xml_node> memberdefs = GetMemberdefs();
  464. for (xml_node memberdef : memberdefs)
  465. {
  466. if (ExtractKind(memberdef) == "variable")
  467. result.push_back(ClassVariableAnalyzer(*this, memberdef));
  468. }
  469. return result;
  470. }
  471. bool ClassAnalyzer::ContainsMethod(const string& name) const
  472. {
  473. vector<MethodAnalyzer> methods = GetAllMethods();
  474. for (const MethodAnalyzer& method : methods)
  475. {
  476. if (method.GetName() == name)
  477. return true;
  478. }
  479. return false;
  480. }
  481. shared_ptr<MethodAnalyzer> ClassAnalyzer::GetMethod(const string& name) const
  482. {
  483. vector<MethodAnalyzer> methods = GetAllMethods();
  484. for (const MethodAnalyzer& method : methods)
  485. {
  486. if (method.GetName() == name)
  487. return make_shared<MethodAnalyzer>(method);
  488. }
  489. return nullptr;
  490. }
  491. int ClassAnalyzer::NumMethods(const string& name) const
  492. {
  493. int result = 0;
  494. vector<MethodAnalyzer> methods = GetAllMethods();
  495. for (const MethodAnalyzer& method : methods)
  496. {
  497. if (method.GetName() == name)
  498. result++;
  499. }
  500. return result;
  501. }
  502. bool ClassAnalyzer::IsAbstract() const
  503. {
  504. vector<MethodAnalyzer> methods = GetAllMethods();
  505. for (const MethodAnalyzer& method : methods)
  506. {
  507. if (method.IsPureVirtual())
  508. {
  509. if (!IsRefCounted())
  510. return true;
  511. // Some pure virtual functions is implemented by URHO3D_OBJECT
  512. string name = method.GetName();
  513. if (name == "GetType" || name == "GetTypeInfo" || name == "GetTypeName")
  514. {
  515. if (ContainsMethod("URHO3D_OBJECT"))
  516. continue;
  517. }
  518. return true;
  519. }
  520. }
  521. return false;
  522. }
  523. bool ClassAnalyzer::AllFloats() const
  524. {
  525. if (Contains(GetComment(), "ALL_FLOATS")) // TODO: remove
  526. return true;
  527. vector<ClassVariableAnalyzer> variables = GetVariables();
  528. for (const ClassVariableAnalyzer& variable : variables)
  529. {
  530. if (variable.IsStatic())
  531. continue;
  532. string type = variable.GetType().ToString();
  533. if (type != "float" && type != "double")
  534. return false;
  535. }
  536. return true;
  537. }
  538. bool ClassAnalyzer::AllInts() const
  539. {
  540. if (Contains(GetComment(), "ALL_INTS")) // TODO: remove
  541. return true;
  542. vector<ClassVariableAnalyzer> variables = GetVariables();
  543. for (const ClassVariableAnalyzer& variable : variables)
  544. {
  545. if (variable.IsStatic())
  546. continue;
  547. string type = variable.GetType().ToString();
  548. if (type != "int" && type != "unsigned")
  549. return false;
  550. }
  551. return true;
  552. }
  553. bool ClassAnalyzer::IsPod() const
  554. {
  555. bool result = Contains(GetComment(), "IS_POD");
  556. if (AllFloats() || AllInts())
  557. result = true;
  558. return result;
  559. }
  560. shared_ptr<ClassAnalyzer> ClassAnalyzer::GetBaseClass() const
  561. {
  562. xml_node basecompoundref = compounddef_.child("basecompoundref");
  563. if (!basecompoundref)
  564. return shared_ptr<ClassAnalyzer>();
  565. string refid = basecompoundref.attribute("refid").value();
  566. assert(!refid.empty());
  567. auto it = SourceData::classesByID_.find(refid);
  568. if (it == SourceData::classesByID_.end())
  569. return shared_ptr<ClassAnalyzer>();
  570. xml_node compounddef = it->second;
  571. return make_shared<ClassAnalyzer>(compounddef);
  572. }
  573. vector<ClassAnalyzer> ClassAnalyzer::GetBaseClasses() const
  574. {
  575. vector<ClassAnalyzer> result;
  576. for (xml_node basecompoundref : compounddef_.children("basecompoundref"))
  577. {
  578. string refid = basecompoundref.attribute("refid").value();
  579. if (refid.empty()) // Type from ThirdParty lib
  580. continue;
  581. auto it = SourceData::classesByID_.find(refid);
  582. if (it == SourceData::classesByID_.end())
  583. continue;
  584. xml_node compounddef = it->second;
  585. result.push_back(ClassAnalyzer(compounddef));
  586. }
  587. return result;
  588. }
  589. static void RecursivelyGetBaseClasses(const ClassAnalyzer& analyzer, vector<ClassAnalyzer>& outResult)
  590. {
  591. for (const ClassAnalyzer& baseClass : analyzer.GetBaseClasses())
  592. {
  593. outResult.push_back(baseClass);
  594. RecursivelyGetBaseClasses(baseClass, outResult);
  595. }
  596. }
  597. vector<ClassAnalyzer> ClassAnalyzer::GetAllBaseClasses() const
  598. {
  599. vector<ClassAnalyzer> result;
  600. RecursivelyGetBaseClasses(*this, result);
  601. return result;
  602. }
  603. bool ClassAnalyzer::HasThisConstructor() const
  604. {
  605. vector<MethodAnalyzer> methods = GetAllMethods();
  606. for (const MethodAnalyzer& method : methods)
  607. {
  608. if (method.IsThisConstructor())
  609. return true;
  610. }
  611. return false;
  612. }
  613. bool ClassAnalyzer::IsRefCounted() const
  614. {
  615. if (GetClassName() == "RefCounted")
  616. return true;
  617. vector<ClassAnalyzer> baseClasses = GetAllBaseClasses();
  618. for (const ClassAnalyzer& classAnalyzer : baseClasses)
  619. {
  620. if (classAnalyzer.GetClassName() == "RefCounted")
  621. return true;
  622. }
  623. return false;
  624. }
  625. shared_ptr<MethodAnalyzer> ClassAnalyzer::GetDefinedThisDefaultConstructor() const
  626. {
  627. vector<MethodAnalyzer> methods = GetAllMethods();
  628. for (const MethodAnalyzer& method : methods)
  629. {
  630. if (method.IsThisDefaultConstructor())
  631. return make_shared<MethodAnalyzer>(method);
  632. }
  633. return nullptr;
  634. }
  635. vector<MethodAnalyzer> ClassAnalyzer::GetThisNonDefaultConstructors() const
  636. {
  637. vector<MethodAnalyzer> result;
  638. vector<MethodAnalyzer> methods = GetAllMethods();
  639. for (const MethodAnalyzer& method : methods)
  640. {
  641. if (method.IsThisNonDefaultConstructor())
  642. result.push_back(method);
  643. }
  644. return result;
  645. }
  646. // ============================================================================
  647. FunctionAnalyzer::FunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  648. : memberdef_(memberdef)
  649. , specialization_(specialization)
  650. {
  651. assert(IsMemberdef(memberdef));
  652. assert(ExtractKind(memberdef) == "function");
  653. }
  654. // ============================================================================
  655. MethodAnalyzer::MethodAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  656. : FunctionAnalyzer(memberdef, specialization)
  657. , classAnalyzer_(classAnalyzer)
  658. {
  659. // Append template specialization from class
  660. specialization_.insert(classAnalyzer.GetSpecialization().begin(), classAnalyzer.GetSpecialization().end());
  661. }
  662. string MethodAnalyzer::GetVirt() const
  663. {
  664. string result = memberdef_.attribute("virt").value();
  665. assert(!result.empty());
  666. return result;
  667. }
  668. string MethodAnalyzer::GetContainsClassName() const
  669. {
  670. string argsstring = ExtractArgsstring(memberdef_);
  671. assert(!argsstring.empty());
  672. string prototype = ExtractDefinition(memberdef_) + argsstring;
  673. smatch match;
  674. regex_match(prototype, match, regex(".*Urho3D::(.+?)::.*"));
  675. assert(match.size());
  676. string result = match[1].str();
  677. return result;
  678. }
  679. string GetFunctionDeclaration(xml_node memberdef)
  680. {
  681. assert(IsMemberdef(memberdef));
  682. assert(ExtractKind(memberdef) == "function");
  683. string argsstring = ExtractArgsstring(memberdef);
  684. assert(!argsstring.empty());
  685. string prototype = ExtractDefinition(memberdef) + argsstring;
  686. smatch match;
  687. regex_match(prototype, match, regex("([^(]*)Urho3D::(.+?)"));
  688. assert(match.size());
  689. string result = match[1].str() + match[2].str();
  690. if (IsExplicit(memberdef))
  691. result = "explicit " + result;
  692. if (IsTemplate(memberdef))
  693. {
  694. string t = "";
  695. xml_node templateparamlist = memberdef.child("templateparamlist");
  696. for (xml_node param : templateparamlist.children("param"))
  697. {
  698. if (t.length() > 0)
  699. t += ", ";
  700. xml_node type = param.child("type");
  701. assert(type);
  702. t += type.child_value();
  703. xml_node declname = param.child("declname");
  704. if (!declname.empty())
  705. t += " " + string(declname.child_value());
  706. }
  707. result = "template<" + t + "> " + result;
  708. }
  709. result = RemoveFirst(result, "URHO3D_API ");
  710. result = RemoveFirst(result, " URHO3D_API");
  711. result = BeautifyDefinition(result);
  712. return result;
  713. }
  714. string GetFunctionLocation(xml_node memberdef)
  715. {
  716. return GetFunctionDeclaration(memberdef) + " | File: " + ExtractHeaderFile(memberdef);
  717. }
  718. bool MethodAnalyzer::IsConst() const
  719. {
  720. string constAttr = memberdef_.attribute("const").value();
  721. assert(!constAttr.empty());
  722. return constAttr == "yes";
  723. }
  724. bool MethodAnalyzer::CanBeGetProperty() const
  725. {
  726. string returnType = GetReturnType().ToString();
  727. if (returnType == "void" || returnType.empty())
  728. return false;
  729. if (GetParams().size() != 0 && GetParams().size() != 1)
  730. return false;
  731. return true;
  732. }
  733. bool MethodAnalyzer::CanBeSetProperty() const
  734. {
  735. string returnType = GetReturnType().ToString();
  736. if (returnType != "void")
  737. return false;
  738. if (GetParams().size() != 1 && GetParams().size() != 2)
  739. return false;
  740. return true;
  741. }
  742. bool MethodAnalyzer::IsParentDestructor() const
  743. {
  744. string functionName = GetName();
  745. if (!StartsWith(functionName, "~"))
  746. return false;
  747. return !IsThisDestructor();
  748. }
  749. bool MethodAnalyzer::IsParentConstructor() const
  750. {
  751. if (IsThisConstructor())
  752. return false;
  753. string name = GetName();
  754. return ExtractDefinition(memberdef_) == "Urho3D::" + name + "::" + name;
  755. }
  756. shared_ptr<MethodAnalyzer> MethodAnalyzer::Reimplements() const
  757. {
  758. xml_node reimplements = memberdef_.child("reimplements");
  759. if (!reimplements)
  760. return shared_ptr<MethodAnalyzer>();
  761. string refid = reimplements.attribute("refid").value();
  762. assert(!refid.empty());
  763. auto it = SourceData::members_.find(refid);
  764. if (it == SourceData::members_.end())
  765. return shared_ptr<MethodAnalyzer>();
  766. xml_node memberdef = it->second;
  767. return make_shared<MethodAnalyzer>(classAnalyzer_, memberdef);
  768. }
  769. // ============================================================================
  770. ClassVariableAnalyzer::ClassVariableAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  771. : classAnalyzer_(classAnalyzer)
  772. , memberdef_(memberdef)
  773. {
  774. assert(IsMemberdef(memberdef));
  775. assert(ExtractKind(memberdef) == "variable");
  776. }
  777. string ClassVariableAnalyzer::GetLocation() const
  778. {
  779. string definition = ExtractDefinition(memberdef_);
  780. assert(!definition.empty());
  781. // Remove Urho3D::
  782. smatch match;
  783. regex_match(definition, match, regex("(.*)Urho3D::(.+)"));
  784. assert(match.size() == 3);
  785. string result = match[1].str() + match[2].str();
  786. result = BeautifyDefinition(result);
  787. result += " | File: " + GetHeaderFile();
  788. if (!classAnalyzer_.usingLocation_.empty())
  789. result = classAnalyzer_.usingLocation_ + " | " + result;
  790. return result;
  791. }
  792. // ============================================================================
  793. NamespaceAnalyzer::NamespaceAnalyzer(xml_node compounddef)
  794. : compounddef_(compounddef)
  795. {
  796. assert(IsCompounddef(compounddef));
  797. assert(ExtractKind(compounddef) == "namespace");
  798. }
  799. vector<EnumAnalyzer> NamespaceAnalyzer::GetEnums()
  800. {
  801. xml_node sectiondef = FindSectiondef(compounddef_, "enum");
  802. assert(sectiondef);
  803. vector<EnumAnalyzer> result;
  804. for (xml_node memberdef : sectiondef.children("memberdef"))
  805. {
  806. EnumAnalyzer analyzer(memberdef);
  807. result.push_back(analyzer);
  808. }
  809. return result;
  810. }
  811. vector<GlobalVariableAnalyzer> NamespaceAnalyzer::GetVariables()
  812. {
  813. xml_node sectiondef = FindSectiondef(compounddef_, "var");
  814. assert(sectiondef);
  815. vector<GlobalVariableAnalyzer> result;
  816. for (xml_node memberdef : sectiondef.children("memberdef"))
  817. result.push_back(GlobalVariableAnalyzer(memberdef));
  818. return result;
  819. }
  820. vector<GlobalFunctionAnalyzer> NamespaceAnalyzer::GetFunctions()
  821. {
  822. xml_node sectiondef = FindSectiondef(compounddef_, "func");
  823. assert(sectiondef);
  824. vector<GlobalFunctionAnalyzer> result;
  825. for (xml_node memberdef : sectiondef.children("memberdef"))
  826. result.push_back(GlobalFunctionAnalyzer(memberdef));
  827. return result;
  828. }
  829. // ============================================================================
  830. UsingAnalyzer::UsingAnalyzer(xml_node memberdef)
  831. : memberdef_(memberdef)
  832. {
  833. assert(IsMemberdef(memberdef));
  834. assert(ExtractKind(memberdef) == "typedef");
  835. }
  836. // ============================================================================
  837. GlobalFunctionAnalyzer::GlobalFunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  838. : FunctionAnalyzer(memberdef, specialization)
  839. {
  840. }
  841. // ============================================================================
  842. ClassStaticFunctionAnalyzer::ClassStaticFunctionAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  843. : FunctionAnalyzer(memberdef, specialization)
  844. , classAnalyzer_(classAnalyzer)
  845. {
  846. assert(IsStatic(memberdef));
  847. }