cmGccDepfileLexerHelper.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "cmGccDepfileLexerHelper.h"
  4. #include <cstdio>
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "cmGccDepfileReaderTypes.h"
  9. #include "LexerParser/cmGccDepfileLexer.h"
  10. #ifdef _WIN32
  11. # include "cmsys/Encoding.h"
  12. #endif
  13. bool cmGccDepfileLexerHelper::readFile(const char* filePath)
  14. {
  15. #ifdef _WIN32
  16. wchar_t* wpath = cmsysEncoding_DupToWide(filePath);
  17. FILE* file = _wfopen(wpath, L"rb");
  18. free(wpath);
  19. #else
  20. FILE* file = fopen(filePath, "r");
  21. #endif
  22. if (!file) {
  23. return false;
  24. }
  25. newEntry();
  26. yyscan_t scanner;
  27. cmGccDepfile_yylex_init(&scanner);
  28. cmGccDepfile_yyset_extra(this, scanner);
  29. cmGccDepfile_yyrestart(file, scanner);
  30. cmGccDepfile_yylex(scanner);
  31. cmGccDepfile_yylex_destroy(scanner);
  32. sanitizeContent();
  33. fclose(file);
  34. return true;
  35. }
  36. void cmGccDepfileLexerHelper::newEntry()
  37. {
  38. this->HelperState = State::Rule;
  39. this->Content.emplace_back();
  40. newRule();
  41. }
  42. void cmGccDepfileLexerHelper::newRule()
  43. {
  44. auto& entry = this->Content.back();
  45. if (entry.rules.empty() || !entry.rules.back().empty()) {
  46. entry.rules.emplace_back();
  47. }
  48. }
  49. void cmGccDepfileLexerHelper::newDependency()
  50. {
  51. // printf("NEW DEP\n");
  52. this->HelperState = State::Dependency;
  53. if (this->Content.back().paths.empty() ||
  54. !this->Content.back().paths.back().empty()) {
  55. this->Content.back().paths.emplace_back();
  56. }
  57. }
  58. void cmGccDepfileLexerHelper::newRuleOrDependency()
  59. {
  60. if (this->HelperState == State::Rule) {
  61. newRule();
  62. } else {
  63. newDependency();
  64. }
  65. }
  66. void cmGccDepfileLexerHelper::addToCurrentPath(const char* s)
  67. {
  68. if (this->Content.empty()) {
  69. return;
  70. }
  71. cmGccStyleDependency* dep = &this->Content.back();
  72. std::string* dst = nullptr;
  73. switch (this->HelperState) {
  74. case State::Rule: {
  75. if (dep->rules.empty()) {
  76. return;
  77. }
  78. dst = &dep->rules.back();
  79. } break;
  80. case State::Dependency: {
  81. if (dep->paths.empty()) {
  82. return;
  83. }
  84. dst = &dep->paths.back();
  85. } break;
  86. }
  87. dst->append(s);
  88. }
  89. void cmGccDepfileLexerHelper::sanitizeContent()
  90. {
  91. for (auto it = this->Content.begin(); it != this->Content.end();) {
  92. // Remove empty rules
  93. for (auto rit = it->rules.begin(); rit != it->rules.end();) {
  94. if (rit->empty()) {
  95. rit = it->rules.erase(rit);
  96. } else {
  97. ++rit;
  98. }
  99. }
  100. // Remove the entry if rules are empty
  101. if (it->rules.empty()) {
  102. it = this->Content.erase(it);
  103. } else {
  104. // Remove empty paths
  105. for (auto pit = it->paths.begin(); pit != it->paths.end();) {
  106. if (pit->empty()) {
  107. pit = it->paths.erase(pit);
  108. } else {
  109. ++pit;
  110. }
  111. }
  112. ++it;
  113. }
  114. }
  115. }