cmDependsJavaParserHelper.cxx 8.6 KB

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