cmDependsJavaParserHelper.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmDependsJavaParserHelper.h"
  4. #include "cmDependsJavaLexer.h"
  5. #include "cmSystemTools.h"
  6. #include "cmsys/FStream.hxx"
  7. #include <iostream>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <utility>
  12. int cmDependsJava_yyparse(yyscan_t yyscanner);
  13. cmDependsJavaParserHelper::cmDependsJavaParserHelper()
  14. {
  15. this->CurrentDepth = 0;
  16. this->UnionsAvailable = 0;
  17. this->LastClassId = 0;
  18. CurrentClass tl;
  19. tl.Name = "*";
  20. this->ClassStack.push_back(std::move(tl));
  21. }
  22. cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
  23. {
  24. this->CleanupParser();
  25. }
  26. void cmDependsJavaParserHelper::CurrentClass::AddFileNamesForPrinting(
  27. std::vector<std::string>* files, const char* prefix, const char* sep) const
  28. {
  29. std::string rname;
  30. if (prefix) {
  31. rname += prefix;
  32. rname += sep;
  33. }
  34. rname += this->Name;
  35. files->push_back(rname);
  36. for (CurrentClass const& nc : this->NestedClasses) {
  37. nc.AddFileNamesForPrinting(files, rname.c_str(), sep);
  38. }
  39. }
  40. void cmDependsJavaParserHelper::DeallocateParserType(char** pt)
  41. {
  42. if (!pt) {
  43. return;
  44. }
  45. if (!*pt) {
  46. return;
  47. }
  48. *pt = nullptr;
  49. this->UnionsAvailable--;
  50. }
  51. void cmDependsJavaParserHelper::AddClassFound(const char* sclass)
  52. {
  53. if (!sclass) {
  54. return;
  55. }
  56. for (std::string const& cf : this->ClassesFound) {
  57. if (cf == sclass) {
  58. return;
  59. }
  60. }
  61. this->ClassesFound.push_back(sclass);
  62. }
  63. void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass)
  64. {
  65. for (std::string const& pi : this->PackagesImport) {
  66. if (pi == sclass) {
  67. return;
  68. }
  69. }
  70. this->PackagesImport.push_back(sclass);
  71. }
  72. void cmDependsJavaParserHelper::SafePrintMissing(const char* str, int line,
  73. int cnt)
  74. {
  75. if (str) {
  76. std::cout << line << " String " << cnt << " exists: ";
  77. unsigned int cc;
  78. for (cc = 0; cc < strlen(str); cc++) {
  79. unsigned char ch = str[cc];
  80. if (ch >= 32 && ch <= 126) {
  81. std::cout << static_cast<char>(ch);
  82. } else {
  83. std::cout << "<" << static_cast<int>(ch) << ">";
  84. break;
  85. }
  86. }
  87. std::cout << "- " << strlen(str) << std::endl;
  88. }
  89. }
  90. void cmDependsJavaParserHelper::Print(const char* place, const char* str)
  91. {
  92. if (this->Verbose) {
  93. std::cout << "[" << place << "=" << str << "]" << std::endl;
  94. }
  95. }
  96. void cmDependsJavaParserHelper::CombineUnions(char** out, const char* in1,
  97. char** in2, const char* sep)
  98. {
  99. size_t len = 1;
  100. if (in1) {
  101. len += strlen(in1);
  102. }
  103. if (*in2) {
  104. len += strlen(*in2);
  105. }
  106. if (sep) {
  107. len += strlen(sep);
  108. }
  109. *out = new char[len];
  110. *out[0] = 0;
  111. if (in1) {
  112. strcat(*out, in1);
  113. }
  114. if (sep) {
  115. strcat(*out, sep);
  116. }
  117. if (*in2) {
  118. strcat(*out, *in2);
  119. }
  120. if (*in2) {
  121. this->DeallocateParserType(in2);
  122. }
  123. this->UnionsAvailable++;
  124. }
  125. void cmDependsJavaParserHelper::CheckEmpty(
  126. int line, int cnt, cmDependsJavaParserHelper::ParserType* pt)
  127. {
  128. int cc;
  129. int kk = -cnt + 1;
  130. for (cc = 1; cc <= cnt; cc++) {
  131. cmDependsJavaParserHelper::ParserType* cpt = pt + kk;
  132. this->SafePrintMissing(cpt->str, line, cc);
  133. kk++;
  134. }
  135. }
  136. void cmDependsJavaParserHelper::PrepareElement(
  137. cmDependsJavaParserHelper::ParserType* me)
  138. {
  139. // Inititalize self
  140. me->str = nullptr;
  141. }
  142. void cmDependsJavaParserHelper::AllocateParserType(
  143. cmDependsJavaParserHelper::ParserType* pt, const char* str, int len)
  144. {
  145. pt->str = nullptr;
  146. if (len == 0) {
  147. len = static_cast<int>(strlen(str));
  148. }
  149. if (len == 0) {
  150. return;
  151. }
  152. this->UnionsAvailable++;
  153. pt->str = new char[len + 1];
  154. strncpy(pt->str, str, len);
  155. pt->str[len] = 0;
  156. this->Allocates.push_back(pt->str);
  157. }
  158. void cmDependsJavaParserHelper::StartClass(const char* cls)
  159. {
  160. CurrentClass cl;
  161. cl.Name = cls;
  162. this->ClassStack.push_back(std::move(cl));
  163. this->CurrentDepth++;
  164. }
  165. void cmDependsJavaParserHelper::EndClass()
  166. {
  167. if (this->ClassStack.empty()) {
  168. std::cerr << "Error when parsing. Current class is null" << std::endl;
  169. abort();
  170. }
  171. if (this->ClassStack.size() <= 1) {
  172. std::cerr << "Error when parsing. Parent class is null" << std::endl;
  173. abort();
  174. }
  175. CurrentClass& current = this->ClassStack.back();
  176. CurrentClass& parent = this->ClassStack[this->ClassStack.size() - 2];
  177. this->CurrentDepth--;
  178. parent.NestedClasses.push_back(current);
  179. this->ClassStack.pop_back();
  180. }
  181. void cmDependsJavaParserHelper::PrintClasses()
  182. {
  183. if (this->ClassStack.empty()) {
  184. std::cerr << "Error when parsing. No classes on class stack" << std::endl;
  185. abort();
  186. }
  187. for (std::string const& f : this->GetFilesProduced()) {
  188. std::cout << " " << f << ".class" << std::endl;
  189. }
  190. }
  191. std::vector<std::string> cmDependsJavaParserHelper::GetFilesProduced()
  192. {
  193. std::vector<std::string> files;
  194. CurrentClass const& toplevel = this->ClassStack.front();
  195. for (CurrentClass const& nc : toplevel.NestedClasses) {
  196. nc.AddFileNamesForPrinting(&files, nullptr, "$");
  197. }
  198. return files;
  199. }
  200. int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
  201. {
  202. if (!str) {
  203. return 0;
  204. }
  205. this->Verbose = verb;
  206. this->InputBuffer = str;
  207. this->InputBufferPos = 0;
  208. this->CurrentLine = 0;
  209. yyscan_t yyscanner;
  210. cmDependsJava_yylex_init(&yyscanner);
  211. cmDependsJava_yyset_extra(this, yyscanner);
  212. int res = cmDependsJava_yyparse(yyscanner);
  213. cmDependsJava_yylex_destroy(yyscanner);
  214. if (res != 0) {
  215. std::cout << "JP_Parse returned: " << res << std::endl;
  216. return 0;
  217. }
  218. if (verb) {
  219. if (!this->CurrentPackage.empty()) {
  220. std::cout << "Current package is: " << this->CurrentPackage << std::endl;
  221. }
  222. std::cout << "Imports packages:";
  223. if (!this->PackagesImport.empty()) {
  224. std::vector<std::string>::iterator it;
  225. for (it = this->PackagesImport.begin(); it != this->PackagesImport.end();
  226. ++it) {
  227. std::cout << " " << *it;
  228. }
  229. }
  230. std::cout << std::endl;
  231. std::cout << "Depends on:";
  232. if (!this->ClassesFound.empty()) {
  233. for (std::string const& cf : this->ClassesFound) {
  234. std::cout << " " << cf;
  235. }
  236. }
  237. std::cout << std::endl;
  238. std::cout << "Generated files:" << std::endl;
  239. this->PrintClasses();
  240. if (this->UnionsAvailable != 0) {
  241. std::cout << "There are still " << this->UnionsAvailable
  242. << " unions available" << std::endl;
  243. }
  244. }
  245. this->CleanupParser();
  246. return 1;
  247. }
  248. void cmDependsJavaParserHelper::CleanupParser()
  249. {
  250. for (char* allocate : this->Allocates) {
  251. delete[] allocate;
  252. }
  253. this->Allocates.erase(this->Allocates.begin(), this->Allocates.end());
  254. }
  255. int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)
  256. {
  257. if (maxlen < 1) {
  258. return 0;
  259. }
  260. if (this->InputBufferPos < this->InputBuffer.size()) {
  261. buf[0] = this->InputBuffer[this->InputBufferPos++];
  262. if (buf[0] == '\n') {
  263. this->CurrentLine++;
  264. }
  265. return 1;
  266. }
  267. buf[0] = '\n';
  268. return 0;
  269. }
  270. void cmDependsJavaParserHelper::Error(const char* str)
  271. {
  272. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  273. fprintf(stderr, "JPError: %s (%lu / Line: %d)\n", str, pos,
  274. this->CurrentLine);
  275. int cc;
  276. std::cerr << "String: [";
  277. for (cc = 0;
  278. cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  279. cc++) {
  280. std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  281. }
  282. std::cerr << "]" << std::endl;
  283. }
  284. void cmDependsJavaParserHelper::UpdateCombine(const char* str1,
  285. const char* str2)
  286. {
  287. if (this->CurrentCombine.empty() && str1 != nullptr) {
  288. this->CurrentCombine = str1;
  289. }
  290. this->CurrentCombine += ".";
  291. this->CurrentCombine += str2;
  292. }
  293. int cmDependsJavaParserHelper::ParseFile(const char* file)
  294. {
  295. if (!cmSystemTools::FileExists(file)) {
  296. return 0;
  297. }
  298. cmsys::ifstream ifs(file);
  299. if (!ifs) {
  300. return 0;
  301. }
  302. std::string fullfile;
  303. std::string line;
  304. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  305. fullfile += line + "\n";
  306. }
  307. return this->ParseString(fullfile.c_str(), 0);
  308. }