XmlAnalyzer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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<ClassFunctionAnalyzer> ClassAnalyzer::GetFunctions() const
  439. {
  440. vector<ClassFunctionAnalyzer> result;
  441. vector<xml_node> memberdefs = GetMemberdefs();
  442. for (xml_node memberdef : memberdefs)
  443. {
  444. if (ExtractKind(memberdef) == "function")
  445. result.push_back(ClassFunctionAnalyzer(*this, memberdef));
  446. }
  447. return result;
  448. }
  449. vector<ClassVariableAnalyzer> ClassAnalyzer::GetVariables() const
  450. {
  451. vector<ClassVariableAnalyzer> result;
  452. vector<xml_node> memberdefs = GetMemberdefs();
  453. for (xml_node memberdef : memberdefs)
  454. {
  455. if (ExtractKind(memberdef) == "variable")
  456. result.push_back(ClassVariableAnalyzer(*this, memberdef));
  457. }
  458. return result;
  459. }
  460. bool ClassAnalyzer::ContainsFunction(const string& name) const
  461. {
  462. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  463. for (const ClassFunctionAnalyzer& function : functions)
  464. {
  465. if (function.GetName() == name)
  466. return true;
  467. }
  468. return false;
  469. }
  470. shared_ptr<ClassFunctionAnalyzer> ClassAnalyzer::GetFunction(const string& name) const
  471. {
  472. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  473. for (const ClassFunctionAnalyzer& function : functions)
  474. {
  475. if (function.GetName() == name)
  476. return make_shared<ClassFunctionAnalyzer>(function);
  477. }
  478. return nullptr;
  479. }
  480. int ClassAnalyzer::NumFunctions(const string& name) const
  481. {
  482. int result = 0;
  483. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  484. for (const ClassFunctionAnalyzer& function : functions)
  485. {
  486. if (function.GetName() == name)
  487. result++;
  488. }
  489. return result;
  490. }
  491. bool ClassAnalyzer::IsAbstract() const
  492. {
  493. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  494. for (const ClassFunctionAnalyzer& function : functions)
  495. {
  496. if (function.IsPureVirtual())
  497. {
  498. if (!IsRefCounted())
  499. return true;
  500. // Some pure virtual functions is implemented by URHO3D_OBJECT
  501. string name = function.GetName();
  502. if (name == "GetType" || name == "GetTypeInfo" || name == "GetTypeName")
  503. {
  504. if (ContainsFunction("URHO3D_OBJECT"))
  505. continue;
  506. }
  507. return true;
  508. }
  509. }
  510. return false;
  511. }
  512. bool ClassAnalyzer::AllFloats() const
  513. {
  514. if (Contains(GetComment(), "ALL_FLOATS")) // TODO: remove
  515. return true;
  516. vector<ClassVariableAnalyzer> variables = GetVariables();
  517. for (const ClassVariableAnalyzer& variable : variables)
  518. {
  519. if (variable.IsStatic())
  520. continue;
  521. string type = variable.GetType().ToString();
  522. if (type != "float" && type != "double")
  523. return false;
  524. }
  525. return true;
  526. }
  527. bool ClassAnalyzer::AllInts() const
  528. {
  529. if (Contains(GetComment(), "ALL_INTS")) // TODO: remove
  530. return true;
  531. vector<ClassVariableAnalyzer> variables = GetVariables();
  532. for (const ClassVariableAnalyzer& variable : variables)
  533. {
  534. if (variable.IsStatic())
  535. continue;
  536. string type = variable.GetType().ToString();
  537. if (type != "int" && type != "unsigned")
  538. return false;
  539. }
  540. return true;
  541. }
  542. bool ClassAnalyzer::IsPod() const
  543. {
  544. bool result = Contains(GetComment(), "IS_POD");
  545. if (AllFloats() || AllInts())
  546. result = true;
  547. return result;
  548. }
  549. shared_ptr<ClassAnalyzer> ClassAnalyzer::GetBaseClass() const
  550. {
  551. xml_node basecompoundref = compounddef_.child("basecompoundref");
  552. if (!basecompoundref)
  553. return shared_ptr<ClassAnalyzer>();
  554. string refid = basecompoundref.attribute("refid").value();
  555. assert(!refid.empty());
  556. auto it = SourceData::classesByID_.find(refid);
  557. if (it == SourceData::classesByID_.end())
  558. return shared_ptr<ClassAnalyzer>();
  559. xml_node compounddef = it->second;
  560. return make_shared<ClassAnalyzer>(compounddef);
  561. }
  562. vector<ClassAnalyzer> ClassAnalyzer::GetBaseClasses() const
  563. {
  564. vector<ClassAnalyzer> result;
  565. for (xml_node basecompoundref : compounddef_.children("basecompoundref"))
  566. {
  567. string refid = basecompoundref.attribute("refid").value();
  568. if (refid.empty()) // Type from ThirdParty lib
  569. continue;
  570. auto it = SourceData::classesByID_.find(refid);
  571. if (it == SourceData::classesByID_.end())
  572. continue;
  573. xml_node compounddef = it->second;
  574. result.push_back(ClassAnalyzer(compounddef));
  575. }
  576. return result;
  577. }
  578. static void RecursivelyGetBaseClasses(const ClassAnalyzer& analyzer, vector<ClassAnalyzer>& outResult)
  579. {
  580. for (const ClassAnalyzer& baseClass : analyzer.GetBaseClasses())
  581. {
  582. outResult.push_back(baseClass);
  583. RecursivelyGetBaseClasses(baseClass, outResult);
  584. }
  585. }
  586. vector<ClassAnalyzer> ClassAnalyzer::GetAllBaseClasses() const
  587. {
  588. vector<ClassAnalyzer> result;
  589. RecursivelyGetBaseClasses(*this, result);
  590. return result;
  591. }
  592. bool ClassAnalyzer::HasThisConstructor() const
  593. {
  594. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  595. for (const ClassFunctionAnalyzer& function : functions)
  596. {
  597. if (function.IsThisConstructor())
  598. return true;
  599. }
  600. return false;
  601. }
  602. bool ClassAnalyzer::IsRefCounted() const
  603. {
  604. if (GetClassName() == "RefCounted")
  605. return true;
  606. vector<ClassAnalyzer> baseClasses = GetAllBaseClasses();
  607. for (const ClassAnalyzer& classAnalyzer : baseClasses)
  608. {
  609. if (classAnalyzer.GetClassName() == "RefCounted")
  610. return true;
  611. }
  612. return false;
  613. }
  614. shared_ptr<ClassFunctionAnalyzer> ClassAnalyzer::GetDefinedThisDefaultConstructor() const
  615. {
  616. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  617. for (const ClassFunctionAnalyzer& function : functions)
  618. {
  619. if (function.IsThisDefaultConstructor())
  620. return make_shared<ClassFunctionAnalyzer>(function);
  621. }
  622. return nullptr;
  623. }
  624. vector<ClassFunctionAnalyzer> ClassAnalyzer::GetThisNonDefaultConstructors() const
  625. {
  626. vector<ClassFunctionAnalyzer> result;
  627. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  628. for (const ClassFunctionAnalyzer& function : functions)
  629. {
  630. if (function.IsThisNonDefaultConstructor())
  631. result.push_back(function);
  632. }
  633. return result;
  634. }
  635. // ============================================================================
  636. FunctionAnalyzer::FunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  637. : memberdef_(memberdef)
  638. , specialization_(specialization)
  639. {
  640. assert(IsMemberdef(memberdef));
  641. assert(ExtractKind(memberdef) == "function");
  642. }
  643. // ============================================================================
  644. ClassFunctionAnalyzer::ClassFunctionAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  645. : FunctionAnalyzer(memberdef, specialization)
  646. , classAnalyzer_(classAnalyzer)
  647. {
  648. // Append template specialization from class
  649. specialization_.insert(classAnalyzer.GetSpecialization().begin(), classAnalyzer.GetSpecialization().end());
  650. }
  651. string ClassFunctionAnalyzer::GetVirt() const
  652. {
  653. string result = memberdef_.attribute("virt").value();
  654. assert(!result.empty());
  655. return result;
  656. }
  657. string ClassFunctionAnalyzer::GetContainsClassName() const
  658. {
  659. string argsstring = ExtractArgsstring(memberdef_);
  660. assert(!argsstring.empty());
  661. string prototype = ExtractDefinition(memberdef_) + argsstring;
  662. smatch match;
  663. regex_match(prototype, match, regex(".*Urho3D::(.+?)::.*"));
  664. assert(match.size());
  665. string result = match[1].str();
  666. return result;
  667. }
  668. string GetFunctionDeclaration(xml_node memberdef)
  669. {
  670. assert(IsMemberdef(memberdef));
  671. assert(ExtractKind(memberdef) == "function");
  672. string argsstring = ExtractArgsstring(memberdef);
  673. assert(!argsstring.empty());
  674. string prototype = ExtractDefinition(memberdef) + argsstring;
  675. smatch match;
  676. regex_match(prototype, match, regex("([^(]*)Urho3D::(.+?)"));
  677. assert(match.size());
  678. string result = match[1].str() + match[2].str();
  679. if (IsExplicit(memberdef))
  680. result = "explicit " + result;
  681. if (IsTemplate(memberdef))
  682. {
  683. string t = "";
  684. xml_node templateparamlist = memberdef.child("templateparamlist");
  685. for (xml_node param : templateparamlist.children("param"))
  686. {
  687. if (t.length() > 0)
  688. t += ", ";
  689. xml_node type = param.child("type");
  690. assert(type);
  691. t += type.child_value();
  692. xml_node declname = param.child("declname");
  693. if (!declname.empty())
  694. t += " " + string(declname.child_value());
  695. }
  696. result = "template<" + t + "> " + result;
  697. }
  698. result = RemoveFirst(result, "URHO3D_API ");
  699. result = RemoveFirst(result, " URHO3D_API");
  700. result = BeautifyDefinition(result);
  701. return result;
  702. }
  703. string GetFunctionLocation(xml_node memberdef)
  704. {
  705. return GetFunctionDeclaration(memberdef) + " | File: " + ExtractHeaderFile(memberdef);
  706. }
  707. bool ClassFunctionAnalyzer::IsConst() const
  708. {
  709. string constAttr = memberdef_.attribute("const").value();
  710. assert(!constAttr.empty());
  711. return constAttr == "yes";
  712. }
  713. bool ClassFunctionAnalyzer::CanBeGetProperty() const
  714. {
  715. string returnType = GetReturnType().ToString();
  716. if (returnType == "void" || returnType.empty())
  717. return false;
  718. if (GetParams().size() != 0 && GetParams().size() != 1)
  719. return false;
  720. return true;
  721. }
  722. bool ClassFunctionAnalyzer::CanBeSetProperty() const
  723. {
  724. string returnType = GetReturnType().ToString();
  725. if (returnType != "void")
  726. return false;
  727. if (GetParams().size() != 1 && GetParams().size() != 2)
  728. return false;
  729. return true;
  730. }
  731. bool ClassFunctionAnalyzer::IsParentDestructor() const
  732. {
  733. string functionName = GetName();
  734. if (!StartsWith(functionName, "~"))
  735. return false;
  736. return !IsThisDestructor();
  737. }
  738. bool ClassFunctionAnalyzer::IsParentConstructor() const
  739. {
  740. if (IsThisConstructor())
  741. return false;
  742. string name = GetName();
  743. return ExtractDefinition(memberdef_) == "Urho3D::" + name + "::" + name;
  744. }
  745. shared_ptr<ClassFunctionAnalyzer> ClassFunctionAnalyzer::Reimplements() const
  746. {
  747. xml_node reimplements = memberdef_.child("reimplements");
  748. if (!reimplements)
  749. return shared_ptr<ClassFunctionAnalyzer>();
  750. string refid = reimplements.attribute("refid").value();
  751. assert(!refid.empty());
  752. auto it = SourceData::members_.find(refid);
  753. if (it == SourceData::members_.end())
  754. return shared_ptr<ClassFunctionAnalyzer>();
  755. xml_node memberdef = it->second;
  756. return make_shared<ClassFunctionAnalyzer>(classAnalyzer_, memberdef);
  757. }
  758. // ============================================================================
  759. ClassVariableAnalyzer::ClassVariableAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  760. : classAnalyzer_(classAnalyzer)
  761. , memberdef_(memberdef)
  762. {
  763. assert(IsMemberdef(memberdef));
  764. assert(ExtractKind(memberdef) == "variable");
  765. }
  766. string ClassVariableAnalyzer::GetLocation() const
  767. {
  768. string definition = ExtractDefinition(memberdef_);
  769. assert(!definition.empty());
  770. // Remove Urho3D::
  771. smatch match;
  772. regex_match(definition, match, regex("(.*)Urho3D::(.+)"));
  773. assert(match.size() == 3);
  774. string result = match[1].str() + match[2].str();
  775. result = BeautifyDefinition(result);
  776. result += " | File: " + GetHeaderFile();
  777. if (!classAnalyzer_.usingLocation_.empty())
  778. result = classAnalyzer_.usingLocation_ + " | " + result;
  779. return result;
  780. }
  781. // ============================================================================
  782. NamespaceAnalyzer::NamespaceAnalyzer(xml_node compounddef)
  783. : compounddef_(compounddef)
  784. {
  785. assert(IsCompounddef(compounddef));
  786. assert(ExtractKind(compounddef) == "namespace");
  787. }
  788. vector<EnumAnalyzer> NamespaceAnalyzer::GetEnums()
  789. {
  790. xml_node sectiondef = FindSectiondef(compounddef_, "enum");
  791. assert(sectiondef);
  792. vector<EnumAnalyzer> result;
  793. for (xml_node memberdef : sectiondef.children("memberdef"))
  794. {
  795. EnumAnalyzer analyzer(memberdef);
  796. result.push_back(analyzer);
  797. }
  798. return result;
  799. }
  800. vector<GlobalVariableAnalyzer> NamespaceAnalyzer::GetVariables()
  801. {
  802. xml_node sectiondef = FindSectiondef(compounddef_, "var");
  803. assert(sectiondef);
  804. vector<GlobalVariableAnalyzer> result;
  805. for (xml_node memberdef : sectiondef.children("memberdef"))
  806. result.push_back(GlobalVariableAnalyzer(memberdef));
  807. return result;
  808. }
  809. vector<GlobalFunctionAnalyzer> NamespaceAnalyzer::GetFunctions()
  810. {
  811. xml_node sectiondef = FindSectiondef(compounddef_, "func");
  812. assert(sectiondef);
  813. vector<GlobalFunctionAnalyzer> result;
  814. for (xml_node memberdef : sectiondef.children("memberdef"))
  815. result.push_back(GlobalFunctionAnalyzer(memberdef));
  816. return result;
  817. }
  818. // ============================================================================
  819. UsingAnalyzer::UsingAnalyzer(xml_node memberdef)
  820. : memberdef_(memberdef)
  821. {
  822. assert(IsMemberdef(memberdef));
  823. assert(ExtractKind(memberdef) == "typedef");
  824. }
  825. // ============================================================================
  826. GlobalFunctionAnalyzer::GlobalFunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  827. : FunctionAnalyzer(memberdef, specialization)
  828. {
  829. }
  830. // ============================================================================
  831. ClassStaticFunctionAnalyzer::ClassStaticFunctionAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  832. : FunctionAnalyzer(memberdef, specialization)
  833. , classAnalyzer_(classAnalyzer)
  834. {
  835. assert(IsStatic(memberdef));
  836. }