XmlAnalyzer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. string ExtractArgsstring(xml_node memberdef)
  162. {
  163. assert(IsMemberdef(memberdef));
  164. xml_node argsstring = memberdef.child("argsstring");
  165. assert(argsstring);
  166. return argsstring.child_value();
  167. }
  168. string ExtractCleanedFunctionArgsstring(xml_node memberdef)
  169. {
  170. assert(ExtractKind(memberdef) == "function");
  171. string argsstring = ExtractArgsstring(memberdef);
  172. assert(StartsWith(argsstring, "("));
  173. size_t endPos = argsstring.find_last_of(')');
  174. assert(endPos != string::npos);
  175. return argsstring.substr(1, endPos - 1);
  176. }
  177. string ExtractProt(xml_node memberdef)
  178. {
  179. assert(IsMemberdef(memberdef));
  180. string result = memberdef.attribute("prot").value();
  181. assert(!result.empty());
  182. return result;
  183. }
  184. TypeAnalyzer ExtractType(xml_node memberdef, const TemplateSpecialization& specialization)
  185. {
  186. assert(IsMemberdef(memberdef));
  187. xml_node type = memberdef.child("type");
  188. assert(type);
  189. // Doxygen bug workaround https://github.com/doxygen/doxygen/issues/7732
  190. // For user-defined conversion operator exract return type from function name
  191. if (RemoveRefs(type).empty() && ExtractKind(memberdef) == "function")
  192. {
  193. string functionName = ExtractName(memberdef);
  194. if (StartsWith(functionName, "operator "))
  195. return TypeAnalyzer(CutStart(functionName, "operator "));
  196. }
  197. return TypeAnalyzer(type, specialization);
  198. }
  199. vector<ParamAnalyzer> ExtractParams(xml_node memberdef, const TemplateSpecialization& specialization)
  200. {
  201. assert(IsMemberdef(memberdef));
  202. assert(ExtractKind(memberdef) == "function");
  203. vector<ParamAnalyzer> result;
  204. for (xml_node param : memberdef.children("param"))
  205. result.push_back(ParamAnalyzer(param, specialization));
  206. return result;
  207. }
  208. string JoinParamsTypes(xml_node memberdef, const TemplateSpecialization& specialization)
  209. {
  210. assert(IsMemberdef(memberdef));
  211. assert(ExtractKind(memberdef) == "function");
  212. string result;
  213. vector<ParamAnalyzer> params = ExtractParams(memberdef, specialization);
  214. for (const ParamAnalyzer& param : params)
  215. {
  216. if (!result.empty())
  217. result += ", ";
  218. result += param.GetType().ToString();
  219. }
  220. return result;
  221. }
  222. string JoinParamsNames(xml_node memberdef, bool skipContext)
  223. {
  224. assert(IsMemberdef(memberdef));
  225. assert(ExtractKind(memberdef) == "function");
  226. string result;
  227. vector<ParamAnalyzer> params = ExtractParams(memberdef);
  228. for (size_t i = 0; i < params.size(); i++)
  229. {
  230. ParamAnalyzer param = params[i];
  231. if (skipContext && i == 0)
  232. {
  233. assert(param.GetType().ToString() == "Context*");
  234. continue;
  235. }
  236. if (!result.empty())
  237. result += ", ";
  238. result += param.GetDeclname();
  239. }
  240. return result;
  241. }
  242. string ExtractID(xml_node node)
  243. {
  244. assert(IsMemberdef(node) || IsCompounddef(node));
  245. string result = node.attribute("id").value();
  246. assert(!result.empty());
  247. return result;
  248. }
  249. string ExtractKind(xml_node node)
  250. {
  251. assert(IsMemberdef(node) || IsCompounddef(node) || IsMember(node) || IsSectiondef(node));
  252. string result = node.attribute("kind").value();
  253. assert(!result.empty());
  254. return result;
  255. }
  256. string ExtractName(xml_node node)
  257. {
  258. assert(IsMemberdef(node) || IsEnumvalue(node));
  259. string result = node.child("name").child_value();
  260. assert(!result.empty());
  261. return result;
  262. }
  263. string ExtractLine(xml_node node)
  264. {
  265. assert(IsMemberdef(node) || IsCompounddef(node));
  266. string result = node.child("location").attribute("line").value();
  267. assert(!result.empty());
  268. return result;
  269. }
  270. string ExtractColumn(xml_node node)
  271. {
  272. assert(IsMemberdef(node) || IsCompounddef(node));
  273. string result = node.child("location").attribute("column").value();
  274. assert(!result.empty());
  275. return result;
  276. }
  277. static string DescriptionToString(xml_node description)
  278. {
  279. string result;
  280. for (xml_node para : description.children("para"))
  281. {
  282. result += RemoveRefs(para);
  283. result += " "; // To avoid gluing words from different paragraphs
  284. }
  285. return result;
  286. }
  287. string ExtractComment(xml_node node)
  288. {
  289. assert(IsMemberdef(node) || IsCompounddef(node));
  290. xml_node brief = node.child("briefdescription");
  291. assert(brief);
  292. xml_node detailed = node.child("detaileddescription");
  293. assert(detailed);
  294. return DescriptionToString(brief) + DescriptionToString(detailed);
  295. }
  296. static string HeaderFullPathToRelative(const string& fullPath)
  297. {
  298. size_t pos = fullPath.rfind("Source/Urho3D");
  299. assert(pos != string::npos);
  300. return ".." + fullPath.substr(pos + strlen("Source/Urho3D"));
  301. }
  302. string ExtractHeaderFile(xml_node node)
  303. {
  304. assert(IsMemberdef(node) || IsCompounddef(node));
  305. xml_node location = node.child("location");
  306. assert(!location.empty());
  307. string declfile = location.attribute("declfile").value();
  308. if (EndsWith(declfile, ".h"))
  309. return HeaderFullPathToRelative(declfile);
  310. string file = location.attribute("file").value();
  311. if (EndsWith(file, ".h"))
  312. return HeaderFullPathToRelative(file);
  313. return string();
  314. }
  315. bool IsTemplate(xml_node node)
  316. {
  317. assert(IsMemberdef(node) || IsCompounddef(node));
  318. return node.child("templateparamlist");
  319. }
  320. vector<string> ExtractTemplateParams(xml_node node)
  321. {
  322. assert(IsMemberdef(node) || IsCompounddef(node));
  323. vector<string> result;
  324. xml_node templateparamlist = node.child("templateparamlist");
  325. for (xml_node param : templateparamlist.children("param"))
  326. {
  327. string type = param.child_value("type");
  328. type = CutStart(type, "class ");
  329. type = CutStart(type, "typename ");
  330. result.push_back(type);
  331. }
  332. return result;
  333. }
  334. // ============================================================================
  335. EnumAnalyzer::EnumAnalyzer(xml_node memberdef)
  336. : memberdef_(memberdef)
  337. {
  338. assert(IsMemberdef(memberdef));
  339. assert(ExtractKind(memberdef) == "enum");
  340. }
  341. string EnumAnalyzer::GetBaseType() const
  342. {
  343. string result = memberdef_.child("type").child_value();
  344. if (result.empty())
  345. return "int";
  346. return result;
  347. }
  348. string EnumAnalyzer::GetLocation() const
  349. {
  350. string baseType = GetBaseType();
  351. if (baseType == "int")
  352. return "enum " + GetTypeName() + " | File: " + GetHeaderFile();
  353. return "enum " + GetTypeName() + " : " + baseType + " | File: " + GetHeaderFile();
  354. }
  355. vector<string> EnumAnalyzer::GetEnumerators() const
  356. {
  357. vector<string> result;
  358. for (xml_node enumvalue : memberdef_.children("enumvalue"))
  359. result.push_back(ExtractName(enumvalue));
  360. return result;
  361. }
  362. // ============================================================================
  363. GlobalVariableAnalyzer::GlobalVariableAnalyzer(xml_node memberdef)
  364. : memberdef_(memberdef)
  365. {
  366. assert(IsMemberdef(memberdef));
  367. assert(ExtractKind(memberdef) == "variable");
  368. }
  369. string GlobalVariableAnalyzer::GetLocation() const
  370. {
  371. string result = ExtractDefinition(memberdef_);
  372. result = RemoveFirst(result, "URHO3D_API ");
  373. result = RemoveFirst(result, " URHO3D_API");
  374. assert(Contains(result, " Urho3D::"));
  375. result = ReplaceFirst(result, " Urho3D::", " ");
  376. if (IsStatic())
  377. result = "static " + result;
  378. result += " | File: " + GetHeaderFile();
  379. return result;
  380. }
  381. // ============================================================================
  382. ClassAnalyzer::ClassAnalyzer(xml_node compounddef, const TemplateSpecialization& specialization)
  383. : compounddef_(compounddef)
  384. , specialization_(specialization)
  385. {
  386. assert(IsCompounddef(compounddef));
  387. }
  388. string ClassAnalyzer::GetClassName() const
  389. {
  390. string compoundname = ExtractCompoundname(compounddef_);
  391. assert(StartsWith(compoundname, "Urho3D::"));
  392. return CutStart(compoundname, "Urho3D::");
  393. }
  394. bool ClassAnalyzer::IsInternal() const
  395. {
  396. if (GetHeaderFile().empty()) // Defined in *.cpp
  397. return true;
  398. if (Contains(GetClassName(), "::")) // Defined inside another class
  399. return true;
  400. return false;
  401. }
  402. vector<xml_node> ClassAnalyzer::GetMemberdefs() const
  403. {
  404. vector<xml_node> result;
  405. xml_node listofallmembers = compounddef_.child("listofallmembers");
  406. assert(listofallmembers);
  407. for (xml_node member : listofallmembers.children("member"))
  408. {
  409. xml_attribute ambiguityscope = member.attribute("ambiguityscope");
  410. if (!ambiguityscope.empty()) // Overridden method from parent class
  411. continue;
  412. string refid = member.attribute("refid").value();
  413. assert(!refid.empty());
  414. auto it = SourceData::members_.find(refid);
  415. if (it == SourceData::members_.end())
  416. continue;
  417. xml_node memberdef = it->second;
  418. result.push_back(memberdef);
  419. }
  420. return result;
  421. }
  422. vector<ClassFunctionAnalyzer> ClassAnalyzer::GetFunctions() const
  423. {
  424. vector<ClassFunctionAnalyzer> result;
  425. vector<xml_node> memberdefs = GetMemberdefs();
  426. for (xml_node memberdef : memberdefs)
  427. {
  428. if (ExtractKind(memberdef) == "function")
  429. result.push_back(ClassFunctionAnalyzer(*this, memberdef));
  430. }
  431. return result;
  432. }
  433. vector<ClassVariableAnalyzer> ClassAnalyzer::GetVariables() const
  434. {
  435. vector<ClassVariableAnalyzer> result;
  436. vector<xml_node> memberdefs = GetMemberdefs();
  437. for (xml_node memberdef : memberdefs)
  438. {
  439. if (ExtractKind(memberdef) == "variable")
  440. result.push_back(ClassVariableAnalyzer(*this, memberdef));
  441. }
  442. return result;
  443. }
  444. bool ClassAnalyzer::ContainsFunction(const string& name) const
  445. {
  446. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  447. for (const ClassFunctionAnalyzer& function : functions)
  448. {
  449. if (function.GetName() == name)
  450. return true;
  451. }
  452. return false;
  453. }
  454. shared_ptr<ClassFunctionAnalyzer> ClassAnalyzer::GetFunction(const string& name) const
  455. {
  456. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  457. for (const ClassFunctionAnalyzer& function : functions)
  458. {
  459. if (function.GetName() == name)
  460. return make_shared<ClassFunctionAnalyzer>(function);
  461. }
  462. return nullptr;
  463. }
  464. int ClassAnalyzer::NumFunctions(const string& name) const
  465. {
  466. int result = 0;
  467. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  468. for (const ClassFunctionAnalyzer& function : functions)
  469. {
  470. if (function.GetName() == name)
  471. result++;
  472. }
  473. return result;
  474. }
  475. bool ClassAnalyzer::IsAbstract() const
  476. {
  477. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  478. for (const ClassFunctionAnalyzer& function : functions)
  479. {
  480. if (function.IsPureVirtual())
  481. {
  482. if (!IsRefCounted())
  483. return true;
  484. // Some pure virtual functions is implemented by URHO3D_OBJECT
  485. string name = function.GetName();
  486. if (name == "GetType" || name == "GetTypeInfo" || name == "GetTypeName")
  487. {
  488. if (ContainsFunction("URHO3D_OBJECT"))
  489. continue;
  490. }
  491. return true;
  492. }
  493. }
  494. return false;
  495. }
  496. bool ClassAnalyzer::AllFloats() const
  497. {
  498. if (Contains(GetComment(), "ALL_FLOATS")) // TODO: remove
  499. return true;
  500. vector<ClassVariableAnalyzer> variables = GetVariables();
  501. for (const ClassVariableAnalyzer& variable : variables)
  502. {
  503. if (variable.IsStatic())
  504. continue;
  505. string type = variable.GetType().ToString();
  506. if (type != "float" && type != "double")
  507. return false;
  508. }
  509. return true;
  510. }
  511. bool ClassAnalyzer::AllInts() const
  512. {
  513. if (Contains(GetComment(), "ALL_INTS")) // TODO: remove
  514. return true;
  515. vector<ClassVariableAnalyzer> variables = GetVariables();
  516. for (const ClassVariableAnalyzer& variable : variables)
  517. {
  518. if (variable.IsStatic())
  519. continue;
  520. string type = variable.GetType().ToString();
  521. if (type != "int" && type != "unsigned")
  522. return false;
  523. }
  524. return true;
  525. }
  526. bool ClassAnalyzer::IsPod() const
  527. {
  528. bool result = Contains(GetComment(), "IS_POD");
  529. if (AllFloats() || AllInts())
  530. result = true;
  531. return result;
  532. }
  533. shared_ptr<ClassAnalyzer> ClassAnalyzer::GetBaseClass() const
  534. {
  535. xml_node basecompoundref = compounddef_.child("basecompoundref");
  536. if (!basecompoundref)
  537. return shared_ptr<ClassAnalyzer>();
  538. string refid = basecompoundref.attribute("refid").value();
  539. assert(!refid.empty());
  540. auto it = SourceData::classesByID_.find(refid);
  541. if (it == SourceData::classesByID_.end())
  542. return shared_ptr<ClassAnalyzer>();
  543. xml_node compounddef = it->second;
  544. return make_shared<ClassAnalyzer>(compounddef);
  545. }
  546. vector<ClassAnalyzer> ClassAnalyzer::GetBaseClasses() const
  547. {
  548. vector<ClassAnalyzer> result;
  549. for (xml_node basecompoundref : compounddef_.children("basecompoundref"))
  550. {
  551. string refid = basecompoundref.attribute("refid").value();
  552. if (refid.empty()) // Type from ThirdParty lib
  553. continue;
  554. auto it = SourceData::classesByID_.find(refid);
  555. if (it == SourceData::classesByID_.end())
  556. continue;
  557. xml_node compounddef = it->second;
  558. result.push_back(ClassAnalyzer(compounddef));
  559. }
  560. return result;
  561. }
  562. static void RecursivelyGetBaseClasses(const ClassAnalyzer& analyzer, vector<ClassAnalyzer>& outResult)
  563. {
  564. for (const ClassAnalyzer& baseClass : analyzer.GetBaseClasses())
  565. {
  566. outResult.push_back(baseClass);
  567. RecursivelyGetBaseClasses(baseClass, outResult);
  568. }
  569. }
  570. vector<ClassAnalyzer> ClassAnalyzer::GetAllBaseClasses() const
  571. {
  572. vector<ClassAnalyzer> result;
  573. RecursivelyGetBaseClasses(*this, result);
  574. return result;
  575. }
  576. bool ClassAnalyzer::HasThisConstructor() const
  577. {
  578. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  579. for (const ClassFunctionAnalyzer& function : functions)
  580. {
  581. if (function.IsThisConstructor())
  582. return true;
  583. }
  584. return false;
  585. }
  586. bool ClassAnalyzer::IsRefCounted() const
  587. {
  588. if (GetClassName() == "RefCounted")
  589. return true;
  590. vector<ClassAnalyzer> baseClasses = GetAllBaseClasses();
  591. for (const ClassAnalyzer& classAnalyzer : baseClasses)
  592. {
  593. if (classAnalyzer.GetClassName() == "RefCounted")
  594. return true;
  595. }
  596. return false;
  597. }
  598. shared_ptr<ClassFunctionAnalyzer> ClassAnalyzer::GetDefinedThisDefaultConstructor() const
  599. {
  600. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  601. for (const ClassFunctionAnalyzer& function : functions)
  602. {
  603. if (function.IsThisDefaultConstructor())
  604. return make_shared<ClassFunctionAnalyzer>(function);
  605. }
  606. return nullptr;
  607. }
  608. vector<ClassFunctionAnalyzer> ClassAnalyzer::GetThisNonDefaultConstructors() const
  609. {
  610. vector<ClassFunctionAnalyzer> result;
  611. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  612. for (const ClassFunctionAnalyzer& function : functions)
  613. {
  614. if (function.IsThisNonDefaultConstructor())
  615. result.push_back(function);
  616. }
  617. return result;
  618. }
  619. // ============================================================================
  620. FunctionAnalyzer::FunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  621. : memberdef_(memberdef)
  622. , specialization_(specialization)
  623. {
  624. assert(IsMemberdef(memberdef));
  625. assert(ExtractKind(memberdef) == "function");
  626. }
  627. // ============================================================================
  628. ClassFunctionAnalyzer::ClassFunctionAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  629. : FunctionAnalyzer(memberdef, specialization)
  630. , classAnalyzer_(classAnalyzer)
  631. {
  632. // Append template specialization from class
  633. specialization_.insert(classAnalyzer.GetSpecialization().begin(), classAnalyzer.GetSpecialization().end());
  634. }
  635. string ClassFunctionAnalyzer::GetVirt() const
  636. {
  637. string result = memberdef_.attribute("virt").value();
  638. assert(!result.empty());
  639. return result;
  640. }
  641. string ClassFunctionAnalyzer::GetContainsClassName() const
  642. {
  643. string argsstring = ExtractArgsstring(memberdef_);
  644. assert(!argsstring.empty());
  645. string prototype = ExtractDefinition(memberdef_) + argsstring;
  646. smatch match;
  647. regex_match(prototype, match, regex(".*Urho3D::(.+?)::.*"));
  648. assert(match.size());
  649. string result = match[1].str();
  650. return result;
  651. }
  652. string GetFunctionLocation(xml_node memberdef)
  653. {
  654. assert(IsMemberdef(memberdef));
  655. assert(ExtractKind(memberdef) == "function");
  656. string argsstring = ExtractArgsstring(memberdef);
  657. assert(!argsstring.empty());
  658. string prototype = ExtractDefinition(memberdef) + argsstring;
  659. smatch match;
  660. regex_match(prototype, match, regex("([^(]*)Urho3D::(.+?)"));
  661. assert(match.size());
  662. string result = match[1].str() + match[2].str();
  663. if (IsExplicit(memberdef))
  664. result = "explicit " + result;
  665. result += " | File: " + ExtractHeaderFile(memberdef);
  666. if (IsTemplate(memberdef))
  667. {
  668. string t = "";
  669. xml_node templateparamlist = memberdef.child("templateparamlist");
  670. for (xml_node param : templateparamlist.children("param"))
  671. {
  672. if (t.length() > 0)
  673. t += ", ";
  674. xml_node type = param.child("type");
  675. assert(type);
  676. t += type.child_value();
  677. xml_node declname = param.child("declname");
  678. if (!declname.empty())
  679. t += " " + string(declname.child_value());
  680. }
  681. result = "template<" + t + "> " + result;
  682. }
  683. result = RemoveFirst(result, "URHO3D_API ");
  684. result = RemoveFirst(result, " URHO3D_API");
  685. result = ReplaceAll(result, " **", "** ");
  686. result = ReplaceAll(result, " &&", "&& ");
  687. result = ReplaceAll(result, " *&", "*& ");
  688. result = ReplaceAll(result, " *", "* ");
  689. result = ReplaceAll(result, " &", "& ");
  690. result = ReplaceAll(result, " )", ")");
  691. result = ReplaceAll(result, "< ", "<");
  692. while (Contains(result, " >"))
  693. result = ReplaceAll(result, " >", ">");
  694. return result;
  695. }
  696. bool ClassFunctionAnalyzer::IsConst() const
  697. {
  698. string constAttr = memberdef_.attribute("const").value();
  699. assert(!constAttr.empty());
  700. return constAttr == "yes";
  701. }
  702. bool ClassFunctionAnalyzer::CanBeGetProperty() const
  703. {
  704. string returnType = GetReturnType().ToString();
  705. if (returnType == "void" || returnType.empty())
  706. return false;
  707. if (GetParams().size() != 0 && GetParams().size() != 1)
  708. return false;
  709. return true;
  710. }
  711. bool ClassFunctionAnalyzer::CanBeSetProperty() const
  712. {
  713. string returnType = GetReturnType().ToString();
  714. if (returnType != "void")
  715. return false;
  716. if (GetParams().size() != 1 && GetParams().size() != 2)
  717. return false;
  718. return true;
  719. }
  720. bool ClassFunctionAnalyzer::IsParentDestructor() const
  721. {
  722. string functionName = GetName();
  723. if (!StartsWith(functionName, "~"))
  724. return false;
  725. return !IsThisDestructor();
  726. }
  727. bool ClassFunctionAnalyzer::IsParentConstructor() const
  728. {
  729. if (IsThisConstructor())
  730. return false;
  731. string name = GetName();
  732. return ExtractDefinition(memberdef_) == "Urho3D::" + name + "::" + name;
  733. }
  734. shared_ptr<ClassFunctionAnalyzer> ClassFunctionAnalyzer::Reimplements() const
  735. {
  736. xml_node reimplements = memberdef_.child("reimplements");
  737. if (!reimplements)
  738. return shared_ptr<ClassFunctionAnalyzer>();
  739. string refid = reimplements.attribute("refid").value();
  740. assert(!refid.empty());
  741. auto it = SourceData::members_.find(refid);
  742. if (it == SourceData::members_.end())
  743. return shared_ptr<ClassFunctionAnalyzer>();
  744. xml_node memberdef = it->second;
  745. return make_shared<ClassFunctionAnalyzer>(classAnalyzer_, memberdef);
  746. }
  747. // ============================================================================
  748. ClassVariableAnalyzer::ClassVariableAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  749. : classAnalyzer_(classAnalyzer)
  750. , memberdef_(memberdef)
  751. {
  752. assert(IsMemberdef(memberdef));
  753. assert(ExtractKind(memberdef) == "variable");
  754. }
  755. string ClassVariableAnalyzer::GetLocation() const
  756. {
  757. string definition = ExtractDefinition(memberdef_);
  758. assert(!definition.empty());
  759. // Remove Urho3D::
  760. smatch match;
  761. regex_match(definition, match, regex("(.*)Urho3D::(.+)"));
  762. assert(match.size() == 3);
  763. string result = match[1].str() + match[2].str();
  764. result += " | File: " + GetHeaderFile();
  765. if (!classAnalyzer_.usingLocation_.empty())
  766. result = classAnalyzer_.usingLocation_ + " | " + result;
  767. return result;
  768. }
  769. // ============================================================================
  770. NamespaceAnalyzer::NamespaceAnalyzer(xml_node compounddef)
  771. : compounddef_(compounddef)
  772. {
  773. assert(IsCompounddef(compounddef));
  774. assert(ExtractKind(compounddef) == "namespace");
  775. }
  776. vector<EnumAnalyzer> NamespaceAnalyzer::GetEnums()
  777. {
  778. xml_node sectiondef = FindSectiondef(compounddef_, "enum");
  779. assert(sectiondef);
  780. vector<EnumAnalyzer> result;
  781. for (xml_node memberdef : sectiondef.children("memberdef"))
  782. {
  783. EnumAnalyzer analyzer(memberdef);
  784. result.push_back(analyzer);
  785. }
  786. return result;
  787. }
  788. vector<GlobalVariableAnalyzer> NamespaceAnalyzer::GetVariables()
  789. {
  790. xml_node sectiondef = FindSectiondef(compounddef_, "var");
  791. assert(sectiondef);
  792. vector<GlobalVariableAnalyzer> result;
  793. for (xml_node memberdef : sectiondef.children("memberdef"))
  794. result.push_back(GlobalVariableAnalyzer(memberdef));
  795. return result;
  796. }
  797. vector<GlobalFunctionAnalyzer> NamespaceAnalyzer::GetFunctions()
  798. {
  799. xml_node sectiondef = FindSectiondef(compounddef_, "func");
  800. assert(sectiondef);
  801. vector<GlobalFunctionAnalyzer> result;
  802. for (xml_node memberdef : sectiondef.children("memberdef"))
  803. result.push_back(GlobalFunctionAnalyzer(memberdef));
  804. return result;
  805. }
  806. // ============================================================================
  807. UsingAnalyzer::UsingAnalyzer(xml_node memberdef)
  808. : memberdef_(memberdef)
  809. {
  810. assert(IsMemberdef(memberdef));
  811. assert(ExtractKind(memberdef) == "typedef");
  812. }
  813. // ============================================================================
  814. GlobalFunctionAnalyzer::GlobalFunctionAnalyzer(xml_node memberdef, const TemplateSpecialization& specialization)
  815. : FunctionAnalyzer(memberdef, specialization)
  816. {
  817. }
  818. // ============================================================================
  819. ClassStaticFunctionAnalyzer::ClassStaticFunctionAnalyzer(const ClassAnalyzer& classAnalyzer, xml_node memberdef, const TemplateSpecialization& specialization)
  820. : FunctionAnalyzer(memberdef, specialization)
  821. , classAnalyzer_(classAnalyzer)
  822. {
  823. assert(IsStatic(memberdef));
  824. }