2
0

XmlAnalyzer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. TypeAnalyzer::TypeAnalyzer(xml_node type, const map<string, string>& templateSpecialization)
  28. {
  29. assert(type.name() == string("type"));
  30. for (xml_node part : type.children())
  31. {
  32. if (part.name() == string("ref"))
  33. fullType_ += part.child_value();
  34. else
  35. fullType_ += part.value();
  36. }
  37. fullType_ = RemoveFirst(fullType_, "URHO3D_API ");
  38. fullType_ = CutStart(fullType_, "constexpr ");
  39. fullType_ = ReplaceAll(fullType_, " *", "*");
  40. fullType_ = ReplaceAll(fullType_, " &", "&");
  41. fullType_ = ReplaceAll(fullType_, "< ", "<");
  42. fullType_ = ReplaceAll(fullType_, " >", ">");
  43. for (pair<string, string> it : templateSpecialization)
  44. {
  45. regex rgx("\\b" + it.first + "\\b");
  46. fullType_ = regex_replace(fullType_, rgx, it.second);
  47. }
  48. isConst_ = StartsWith(fullType_, "const ");
  49. name_ = CutStart(fullType_, "const ");
  50. isRvalueReference_ = EndsWith(name_, "&&");
  51. name_ = CutEnd(name_, "&&");
  52. isRefToPoiner_ = EndsWith(name_, "*&");
  53. name_ = CutEnd(name_, "*&");
  54. isDoublePointer_ = EndsWith(name_, "**");
  55. name_ = CutEnd(name_, "**");
  56. isPointer_ = EndsWith(name_, "*");
  57. name_ = CutEnd(name_, "*");
  58. isReference_ = EndsWith(name_, "&");
  59. name_ = CutEnd(name_, "&");
  60. smatch match;
  61. if (regex_match(name_, match, regex("(.+?)<(.+)>")))
  62. {
  63. templateParams_ = match[2].str();
  64. name_ = match[1].str();
  65. }
  66. }
  67. // ============================================================================
  68. ParamAnalyzer::ParamAnalyzer(xml_node param, const map<string, string>& templateSpecialization)
  69. : node_(param)
  70. , templateSpecialization_(templateSpecialization)
  71. {
  72. assert(node_.name() == string("param"));
  73. }
  74. string ParamAnalyzer::ToString() const
  75. {
  76. string type = GetType().ToString();
  77. assert(!type.empty());
  78. string name = GetDeclname();
  79. assert(!name.empty());
  80. return type + " " + name;
  81. }
  82. TypeAnalyzer ParamAnalyzer::GetType() const
  83. {
  84. xml_node type = node_.child("type");
  85. assert(type);
  86. return TypeAnalyzer(type, templateSpecialization_);
  87. }
  88. string ParamAnalyzer::GetDeclname() const
  89. {
  90. string result = node_.child("declname").child_value();
  91. assert(!result.empty());
  92. return result;
  93. }
  94. string ParamAnalyzer::GetDefval() const
  95. {
  96. string result;
  97. xml_node defval = node_.child("defval");
  98. for (xml_node part : defval.children())
  99. {
  100. if (part.name() == string("ref"))
  101. result += part.child_value();
  102. else
  103. result += part.value();
  104. }
  105. return result;
  106. }
  107. // ============================================================================
  108. string ExtractCompoundname(xml_node compounddef)
  109. {
  110. assert(IsCompounddef(compounddef));
  111. string result = compounddef.child("compoundname").child_value();
  112. assert(!result.empty());
  113. return result;
  114. }
  115. xml_node FindSectiondef(xml_node compounddef, const string& kind)
  116. {
  117. assert(IsCompounddef(compounddef));
  118. assert(!kind.empty());
  119. for (xml_node sectiondef : compounddef.children("sectiondef"))
  120. {
  121. if (ExtractKind(sectiondef) == kind)
  122. return sectiondef;
  123. }
  124. return xml_node();
  125. }
  126. bool IsStatic(xml_node memberdef)
  127. {
  128. assert(IsMemberdef(memberdef));
  129. string staticAttr = memberdef.attribute("static").value();
  130. assert(!staticAttr.empty());
  131. return staticAttr == "yes";
  132. }
  133. bool IsExplicit(xml_node memberdef)
  134. {
  135. assert(IsMemberdef(memberdef));
  136. assert(ExtractKind(memberdef) == "function");
  137. string explicitAttr = memberdef.attribute("explicit").value();
  138. assert(!explicitAttr.empty());
  139. return explicitAttr == "yes";
  140. }
  141. string ExtractDefinition(xml_node memberdef)
  142. {
  143. assert(IsMemberdef(memberdef));
  144. string result = memberdef.child("definition").child_value();
  145. assert(!result.empty());
  146. return result;
  147. }
  148. string ExtractArgsstring(xml_node memberdef)
  149. {
  150. assert(IsMemberdef(memberdef));
  151. xml_node argsstring = memberdef.child("argsstring");
  152. assert(argsstring);
  153. return argsstring.child_value();
  154. }
  155. vector<string> ExtractTemplateParams(xml_node memberdef)
  156. {
  157. assert(IsMemberdef(memberdef));
  158. vector<string> result;
  159. xml_node templateparamlist = memberdef.child("templateparamlist");
  160. for (xml_node param : templateparamlist.children("param"))
  161. {
  162. string type = param.child_value("type");
  163. type = CutStart(type, "class ");
  164. type = CutStart(type, "typename ");
  165. result.push_back(type);
  166. }
  167. return result;
  168. }
  169. string ExtractProt(xml_node memberdef)
  170. {
  171. assert(IsMemberdef(memberdef));
  172. string result = memberdef.attribute("prot").value();
  173. assert(!result.empty());
  174. return result;
  175. }
  176. TypeAnalyzer ExtractType(xml_node memberdef, const map<string, string>& templateSpecialization)
  177. {
  178. assert(IsMemberdef(memberdef));
  179. xml_node type = memberdef.child("type");
  180. assert(type);
  181. return TypeAnalyzer(type, templateSpecialization);
  182. }
  183. vector<ParamAnalyzer> ExtractParams(xml_node memberdef, const map<string, string>& templateSpecialization)
  184. {
  185. assert(IsMemberdef(memberdef));
  186. assert(ExtractKind(memberdef) == "function");
  187. vector<ParamAnalyzer> result;
  188. for (xml_node param : memberdef.children("param"))
  189. result.push_back(ParamAnalyzer(param, templateSpecialization));
  190. return result;
  191. }
  192. string JoinParamsTypes(xml_node memberdef, const map<string, string>& templateSpecialization)
  193. {
  194. assert(IsMemberdef(memberdef));
  195. assert(ExtractKind(memberdef) == "function");
  196. string result;
  197. vector<ParamAnalyzer> params = ExtractParams(memberdef, templateSpecialization);
  198. for (ParamAnalyzer param : params)
  199. {
  200. if (!result.empty())
  201. result += ", ";
  202. result += param.GetType().ToString();
  203. }
  204. return result;
  205. }
  206. string JoinParamsNames(xml_node memberdef, bool skipContext)
  207. {
  208. assert(IsMemberdef(memberdef));
  209. assert(ExtractKind(memberdef) == "function");
  210. string result;
  211. vector<ParamAnalyzer> params = ExtractParams(memberdef);
  212. for (size_t i = 0; i < params.size(); i++)
  213. {
  214. ParamAnalyzer param = params[i];
  215. if (skipContext && i == 0)
  216. {
  217. assert(param.GetType().ToString() == "Context*");
  218. continue;
  219. }
  220. if (!result.empty())
  221. result += ", ";
  222. result += param.GetDeclname();
  223. }
  224. return result;
  225. }
  226. string ExtractID(xml_node node)
  227. {
  228. assert(IsMemberdef(node) || IsCompounddef(node));
  229. string result = node.attribute("id").value();
  230. assert(!result.empty());
  231. return result;
  232. }
  233. string ExtractKind(xml_node node)
  234. {
  235. assert(IsMemberdef(node) || IsCompounddef(node) || IsMember(node) || IsSectiondef(node));
  236. string result = node.attribute("kind").value();
  237. assert(!result.empty());
  238. return result;
  239. }
  240. string ExtractName(xml_node node)
  241. {
  242. assert(IsMemberdef(node) || IsEnumvalue(node));
  243. string result = node.child("name").child_value();
  244. assert(!result.empty());
  245. return result;
  246. }
  247. string ExtractLine(xml_node node)
  248. {
  249. assert(IsMemberdef(node) || IsCompounddef(node));
  250. string result = node.child("location").attribute("line").value();
  251. assert(!result.empty());
  252. return result;
  253. }
  254. string ExtractColumn(xml_node node)
  255. {
  256. assert(IsMemberdef(node) || IsCompounddef(node));
  257. string result = node.child("location").attribute("column").value();
  258. assert(!result.empty());
  259. return result;
  260. }
  261. static string DescriptionToString(xml_node description)
  262. {
  263. string result;
  264. for (xml_node para : description.children("para"))
  265. {
  266. for (xml_node part : para.children())
  267. {
  268. if (part.name() == string("ref"))
  269. result += part.child_value();
  270. else
  271. result += part.value();
  272. }
  273. result += " "; // To avoid gluing words from different paragraphs
  274. }
  275. return result;
  276. }
  277. string ExtractComment(xml_node node)
  278. {
  279. assert(IsMemberdef(node) || IsCompounddef(node));
  280. xml_node brief = node.child("briefdescription");
  281. assert(brief);
  282. xml_node detailed = node.child("detaileddescription");
  283. assert(detailed);
  284. return DescriptionToString(brief) + DescriptionToString(detailed);
  285. }
  286. static string HeaderFullPathToRelative(const string& fullPath)
  287. {
  288. size_t pos = fullPath.rfind("Source/Urho3D");
  289. assert(pos != string::npos);
  290. return ".." + fullPath.substr(pos + strlen("Source/Urho3D"));
  291. }
  292. string ExtractHeaderFile(xml_node node)
  293. {
  294. assert(IsMemberdef(node) || IsCompounddef(node));
  295. xml_node location = node.child("location");
  296. assert(!location.empty());
  297. string declfile = location.attribute("declfile").value();
  298. if (EndsWith(declfile, ".h"))
  299. return HeaderFullPathToRelative(declfile);
  300. string file = location.attribute("file").value();
  301. if (EndsWith(file, ".h"))
  302. return HeaderFullPathToRelative(file);
  303. return string();
  304. }
  305. bool IsTemplate(xml_node node)
  306. {
  307. assert(IsMemberdef(node) || IsCompounddef(node));
  308. return node.child("templateparamlist");
  309. }
  310. // ============================================================================
  311. EnumAnalyzer::EnumAnalyzer(xml_node memberdef)
  312. : memberdef_(memberdef)
  313. {
  314. assert(IsMemberdef(memberdef));
  315. assert(ExtractKind(memberdef) == "enum");
  316. }
  317. string EnumAnalyzer::GetBaseType() const
  318. {
  319. string result = memberdef_.child("type").child_value();
  320. if (result.empty())
  321. return "int";
  322. return result;
  323. }
  324. string EnumAnalyzer::GetLocation() const
  325. {
  326. string baseType = GetBaseType();
  327. if (baseType == "int")
  328. return "enum " + GetTypeName() + " | File: " + GetHeaderFile();
  329. return "enum " + GetTypeName() + " : " + baseType + " | File: " + GetHeaderFile();
  330. }
  331. vector<string> EnumAnalyzer::GetEnumerators() const
  332. {
  333. vector<string> result;
  334. for (xml_node enumvalue : memberdef_.children("enumvalue"))
  335. result.push_back(ExtractName(enumvalue));
  336. return result;
  337. }
  338. // ============================================================================
  339. GlobalVariableAnalyzer::GlobalVariableAnalyzer(xml_node memberdef)
  340. : memberdef_(memberdef)
  341. {
  342. assert(IsMemberdef(memberdef));
  343. assert(ExtractKind(memberdef) == "variable");
  344. }
  345. string GlobalVariableAnalyzer::GetLocation() const
  346. {
  347. string result = ExtractDefinition(memberdef_);
  348. result = RemoveFirst(result, "URHO3D_API ");
  349. assert(Contains(result, " Urho3D::"));
  350. result = ReplaceFirst(result, " Urho3D::", " ");
  351. if (IsStatic())
  352. result = "static " + result;
  353. result += " | File: " + GetHeaderFile();
  354. return result;
  355. }
  356. // ============================================================================
  357. ClassAnalyzer::ClassAnalyzer(xml_node compounddef)
  358. : compounddef_(compounddef)
  359. {
  360. assert(IsCompounddef(compounddef));
  361. }
  362. string ClassAnalyzer::GetClassName() const
  363. {
  364. string compoundname = ExtractCompoundname(compounddef_);
  365. assert(StartsWith(compoundname, "Urho3D::"));
  366. return CutStart(compoundname, "Urho3D::");
  367. }
  368. bool ClassAnalyzer::IsInternal() const
  369. {
  370. if (GetHeaderFile().empty()) // Defined in *.cpp
  371. return true;
  372. if (Contains(GetClassName(), "::")) // Defined inside another class
  373. return true;
  374. return false;
  375. }
  376. vector<xml_node> ClassAnalyzer::GetMemberdefs() const
  377. {
  378. vector<xml_node> result;
  379. xml_node listofallmembers = compounddef_.child("listofallmembers");
  380. assert(listofallmembers);
  381. for (xml_node member : listofallmembers.children("member"))
  382. {
  383. xml_attribute ambiguityscope = member.attribute("ambiguityscope");
  384. if (!ambiguityscope.empty()) // Overridden method from parent class
  385. continue;
  386. string refid = member.attribute("refid").value();
  387. assert(!refid.empty());
  388. auto it = SourceData::members_.find(refid);
  389. if (it == SourceData::members_.end())
  390. continue;
  391. xml_node memberdef = it->second;
  392. result.push_back(memberdef);
  393. }
  394. return result;
  395. }
  396. vector<ClassFunctionAnalyzer> ClassAnalyzer::GetFunctions() const
  397. {
  398. vector<ClassFunctionAnalyzer> result;
  399. vector<xml_node> memberdefs = GetMemberdefs();
  400. for (xml_node memberdef : memberdefs)
  401. {
  402. if (ExtractKind(memberdef) == "function")
  403. result.push_back(ClassFunctionAnalyzer(*this, memberdef));
  404. }
  405. return result;
  406. }
  407. vector<ClassVariableAnalyzer> ClassAnalyzer::GetVariables() const
  408. {
  409. vector<ClassVariableAnalyzer> result;
  410. vector<xml_node> memberdefs = GetMemberdefs();
  411. for (xml_node memberdef : memberdefs)
  412. {
  413. if (ExtractKind(memberdef) == "variable")
  414. result.push_back(ClassVariableAnalyzer(*this, memberdef));
  415. }
  416. return result;
  417. }
  418. bool ClassAnalyzer::ContainsFunction(const string& name) const
  419. {
  420. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  421. for (ClassFunctionAnalyzer function : functions)
  422. {
  423. if (function.GetName() == name)
  424. return true;
  425. }
  426. return false;
  427. }
  428. ClassFunctionAnalyzer ClassAnalyzer::GetFunction(const string& name) const
  429. {
  430. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  431. for (ClassFunctionAnalyzer function : functions)
  432. {
  433. if (function.GetName() == name)
  434. return function;
  435. }
  436. assert(false);
  437. return (ClassFunctionAnalyzer(*this, xml_node())); // xml_node can not be empty, so here we return incorrect value
  438. }
  439. int ClassAnalyzer::NumFunctions(const string& name) const
  440. {
  441. int result = 0;
  442. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  443. for (ClassFunctionAnalyzer function : functions)
  444. {
  445. if (function.GetName() == name)
  446. result++;
  447. }
  448. return result;
  449. }
  450. bool ClassAnalyzer::IsAbstract() const
  451. {
  452. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  453. for (ClassFunctionAnalyzer function : functions)
  454. {
  455. if (function.IsPureVirtual())
  456. {
  457. if (!IsRefCounted())
  458. return true;
  459. // Some pure virtual functions is implemented by URHO3D_OBJECT
  460. string name = function.GetName();
  461. if (name == "GetType" || name == "GetTypeInfo" || name == "GetTypeName")
  462. {
  463. if (ContainsFunction("URHO3D_OBJECT"))
  464. continue;
  465. }
  466. return true;
  467. }
  468. }
  469. return false;
  470. }
  471. bool ClassAnalyzer::AllFloats() const
  472. {
  473. if (Contains(GetComment(), "ALL_FLOATS")) // TODO: remove
  474. return true;
  475. vector<ClassVariableAnalyzer> variables = GetVariables();
  476. for (ClassVariableAnalyzer variable : variables)
  477. {
  478. if (variable.IsStatic())
  479. continue;
  480. string type = variable.GetType().ToString();
  481. if (type != "float" && type != "double")
  482. return false;
  483. }
  484. return true;
  485. }
  486. bool ClassAnalyzer::AllInts() const
  487. {
  488. if (Contains(GetComment(), "ALL_INTS")) // TODO: remove
  489. return true;
  490. vector<ClassVariableAnalyzer> variables = GetVariables();
  491. for (ClassVariableAnalyzer variable : variables)
  492. {
  493. if (variable.IsStatic())
  494. continue;
  495. string type = variable.GetType().ToString();
  496. if (type != "int" && type != "unsigned")
  497. return false;
  498. }
  499. return true;
  500. }
  501. bool ClassAnalyzer::IsPod() const
  502. {
  503. bool result = Contains(GetComment(), "IS_POD");
  504. if (AllFloats() || AllInts())
  505. result = true;
  506. return result;
  507. }
  508. shared_ptr<ClassAnalyzer> ClassAnalyzer::GetBaseClass() const
  509. {
  510. xml_node basecompoundref = compounddef_.child("basecompoundref");
  511. if (!basecompoundref)
  512. return shared_ptr<ClassAnalyzer>();
  513. string refid = basecompoundref.attribute("refid").value();
  514. assert(!refid.empty());
  515. auto it = SourceData::classesByID_.find(refid);
  516. if (it == SourceData::classesByID_.end())
  517. return shared_ptr<ClassAnalyzer>();
  518. xml_node compounddef = it->second;
  519. return make_shared<ClassAnalyzer>(compounddef);
  520. }
  521. vector<ClassAnalyzer> ClassAnalyzer::GetBaseClasses() const
  522. {
  523. vector<ClassAnalyzer> result;
  524. for (xml_node basecompoundref : compounddef_.children("basecompoundref"))
  525. {
  526. string refid = basecompoundref.attribute("refid").value();
  527. if (refid.empty()) // Type from ThirdParty lib
  528. continue;
  529. auto it = SourceData::classesByID_.find(refid);
  530. if (it == SourceData::classesByID_.end())
  531. continue;
  532. xml_node compounddef = it->second;
  533. result.push_back(ClassAnalyzer(compounddef));
  534. }
  535. return result;
  536. }
  537. static void RecursivelyGetBaseClasses(const ClassAnalyzer& analyzer, vector<ClassAnalyzer>& outResult)
  538. {
  539. for (ClassAnalyzer baseClass : analyzer.GetBaseClasses())
  540. {
  541. outResult.push_back(baseClass);
  542. RecursivelyGetBaseClasses(baseClass, outResult);
  543. }
  544. }
  545. vector<ClassAnalyzer> ClassAnalyzer::GetAllBaseClasses() const
  546. {
  547. vector<ClassAnalyzer> result;
  548. RecursivelyGetBaseClasses(*this, result);
  549. return result;
  550. }
  551. bool ClassAnalyzer::HasThisConstructor() const
  552. {
  553. vector<ClassFunctionAnalyzer> functions = GetFunctions();
  554. for (ClassFunctionAnalyzer function : functions)
  555. {
  556. if (function.IsThisConstructor())
  557. return true;
  558. }
  559. return false;
  560. }
  561. // ============================================================================
  562. ClassFunctionAnalyzer::ClassFunctionAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  563. : classAnalyzer_(classAnalyzer)
  564. , memberdef_(memberdef)
  565. {
  566. assert(IsMemberdef(memberdef));
  567. assert(ExtractKind(memberdef) == "function");
  568. }
  569. string ClassFunctionAnalyzer::GetVirt() const
  570. {
  571. string result = memberdef_.attribute("virt").value();
  572. assert(!result.empty());
  573. return result;
  574. }
  575. string ClassFunctionAnalyzer::GetContainsClassName() const
  576. {
  577. string argsstring = ExtractArgsstring(memberdef_);
  578. assert(!argsstring.empty());
  579. string prototype = ExtractDefinition(memberdef_) + argsstring;
  580. smatch match;
  581. regex_match(prototype, match, regex(".*Urho3D::(.+?)::.*"));
  582. assert(match.size());
  583. string result = match[1].str();
  584. return result;
  585. }
  586. static string GetFunctionLocation(xml_node memberdef)
  587. {
  588. string argsstring = ExtractArgsstring(memberdef);
  589. assert(!argsstring.empty());
  590. string prototype = ExtractDefinition(memberdef) + argsstring;
  591. smatch match;
  592. regex_match(prototype, match, regex("([^(]*)Urho3D::(.+?)"));
  593. assert(match.size());
  594. string result = match[1].str() + match[2].str();
  595. if (IsExplicit(memberdef))
  596. result = "explicit " + result;
  597. result += " | File: " + ExtractHeaderFile(memberdef);
  598. if (IsTemplate(memberdef))
  599. {
  600. string t = "";
  601. xml_node templateparamlist = memberdef.child("templateparamlist");
  602. for (xml_node param : templateparamlist.children("param"))
  603. {
  604. if (t.length() > 0)
  605. t += ", ";
  606. xml_node type = param.child("type");
  607. assert(type);
  608. t += type.child_value();
  609. xml_node declname = param.child("declname");
  610. if (!declname.empty())
  611. t += " " + string(declname.child_value());
  612. }
  613. result = "template<" + t + "> " + result;
  614. }
  615. result = RemoveFirst(result, "URHO3D_API ");
  616. result = ReplaceAll(result, " **", "** ");
  617. result = ReplaceAll(result, " &&", "&& ");
  618. result = ReplaceAll(result, " *&", "*& ");
  619. result = ReplaceAll(result, " *", "* ");
  620. result = ReplaceAll(result, " &", "& ");
  621. result = ReplaceAll(result, " )", ")");
  622. result = ReplaceAll(result, "< ", "<");
  623. while (Contains(result, " >"))
  624. result = ReplaceAll(result, " >", ">");
  625. return result;
  626. }
  627. string ClassFunctionAnalyzer::GetLocation() const
  628. {
  629. return GetFunctionLocation(memberdef_);
  630. }
  631. bool ClassFunctionAnalyzer::IsConst() const
  632. {
  633. string constAttr = memberdef_.attribute("const").value();
  634. assert(!constAttr.empty());
  635. return constAttr == "yes";
  636. }
  637. bool ClassFunctionAnalyzer::CanBeGetProperty() const
  638. {
  639. string returnType = GetReturnType().ToString();
  640. if (returnType == "void" || returnType.empty())
  641. return false;
  642. if (GetParams().size() != 0 && GetParams().size() != 1)
  643. return false;
  644. return true;
  645. }
  646. bool ClassFunctionAnalyzer::CanBeSetProperty() const
  647. {
  648. string returnType = GetReturnType().ToString();
  649. if (returnType != "void")
  650. return false;
  651. if (GetParams().size() != 1 && GetParams().size() != 2)
  652. return false;
  653. return true;
  654. }
  655. bool ClassFunctionAnalyzer::IsParentDestructor() const
  656. {
  657. string functionName = GetName();
  658. if (!StartsWith(functionName, "~"))
  659. return false;
  660. return !IsThisDestructor();
  661. }
  662. bool ClassFunctionAnalyzer::IsParentConstructor() const
  663. {
  664. if (IsThisConstructor())
  665. return false;
  666. string name = GetName();
  667. return ExtractDefinition(memberdef_) == "Urho3D::" + name + "::" + name;
  668. }
  669. shared_ptr<ClassFunctionAnalyzer> ClassFunctionAnalyzer::Reimplements() const
  670. {
  671. xml_node reimplements = memberdef_.child("reimplements");
  672. if (!reimplements)
  673. return shared_ptr<ClassFunctionAnalyzer>();
  674. string refid = reimplements.attribute("refid").value();
  675. assert(!refid.empty());
  676. auto it = SourceData::members_.find(refid);
  677. if (it == SourceData::members_.end())
  678. return shared_ptr<ClassFunctionAnalyzer>();
  679. xml_node memberdef = it->second;
  680. return make_shared<ClassFunctionAnalyzer>(classAnalyzer_, memberdef);
  681. }
  682. // ============================================================================
  683. ClassVariableAnalyzer::ClassVariableAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  684. : classAnalyzer_(classAnalyzer)
  685. , memberdef_(memberdef)
  686. {
  687. assert(IsMemberdef(memberdef));
  688. assert(ExtractKind(memberdef) == "variable");
  689. }
  690. string ClassVariableAnalyzer::GetLocation() const
  691. {
  692. string definition = ExtractDefinition(memberdef_);
  693. assert(!definition.empty());
  694. // Remove Urho3D::
  695. smatch match;
  696. regex_match(definition, match, regex("(.*)Urho3D::(.+)"));
  697. assert(match.size() == 3);
  698. string result = match[1].str() + match[2].str();
  699. result += " | File: " + GetHeaderFile();
  700. return result;
  701. }
  702. // ============================================================================
  703. NamespaceAnalyzer::NamespaceAnalyzer(xml_node compounddef)
  704. : compounddef_(compounddef)
  705. {
  706. assert(IsCompounddef(compounddef));
  707. assert(ExtractKind(compounddef) == "namespace");
  708. }
  709. vector<EnumAnalyzer> NamespaceAnalyzer::GetEnums()
  710. {
  711. xml_node sectiondef = FindSectiondef(compounddef_, "enum");
  712. assert(sectiondef);
  713. vector<EnumAnalyzer> result;
  714. for (xml_node memberdef : sectiondef.children("memberdef"))
  715. {
  716. EnumAnalyzer analyzer(memberdef);
  717. result.push_back(analyzer);
  718. }
  719. return result;
  720. }
  721. vector<GlobalVariableAnalyzer> NamespaceAnalyzer::GetVariables()
  722. {
  723. xml_node sectiondef = FindSectiondef(compounddef_, "var");
  724. assert(sectiondef);
  725. vector<GlobalVariableAnalyzer> result;
  726. for (xml_node memberdef : sectiondef.children("memberdef"))
  727. {
  728. GlobalVariableAnalyzer analyzer(memberdef);
  729. result.push_back(analyzer);
  730. }
  731. return result;
  732. }
  733. vector<GlobalFunctionAnalyzer> NamespaceAnalyzer::GetFunctions()
  734. {
  735. xml_node sectiondef = FindSectiondef(compounddef_, "func");
  736. assert(sectiondef);
  737. vector<GlobalFunctionAnalyzer> result;
  738. for (xml_node memberdef : sectiondef.children("memberdef"))
  739. {
  740. GlobalFunctionAnalyzer analyzer(memberdef);
  741. result.push_back(analyzer);
  742. }
  743. return result;
  744. }
  745. // ============================================================================
  746. UsingAnalyzer::UsingAnalyzer(xml_node memberdef)
  747. : memberdef_(memberdef)
  748. {
  749. assert(IsMemberdef(memberdef));
  750. assert(ExtractKind(memberdef) == "typedef");
  751. }
  752. string UsingAnalyzer::GetIdentifier() const
  753. {
  754. string definition = ExtractDefinition(memberdef_);
  755. assert(StartsWith(definition, "using Urho3D::"));
  756. string result = CutStart(definition, "using Urho3D::");
  757. result = GetFirstWord(result);
  758. return result;
  759. }
  760. // ============================================================================
  761. GlobalFunctionAnalyzer::GlobalFunctionAnalyzer(xml_node memberdef)
  762. : memberdef_(memberdef)
  763. {
  764. assert(IsMemberdef(memberdef));
  765. assert(ExtractKind(memberdef) == "function");
  766. }
  767. string GlobalFunctionAnalyzer::GetLocation() const
  768. {
  769. return GetFunctionLocation(memberdef_);
  770. }
  771. // ============================================================================
  772. ClassStaticFunctionAnalyzer::ClassStaticFunctionAnalyzer(ClassAnalyzer classAnalyzer, xml_node memberdef)
  773. : classAnalyzer_(classAnalyzer)
  774. , memberdef_(memberdef)
  775. {
  776. assert(IsMemberdef(memberdef));
  777. assert(ExtractKind(memberdef) == "function");
  778. assert(IsStatic(memberdef));
  779. }
  780. string ClassStaticFunctionAnalyzer::GetLocation() const
  781. {
  782. return GetFunctionLocation(memberdef_);
  783. }