1
0

cmDependsJavaParserHelper.cxx 8.5 KB

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