cmGccDepfileLexerHelper.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. this->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. this->sanitizeContent();
  33. fclose(file);
  34. return this->HelperState != State::Failed;
  35. }
  36. void cmGccDepfileLexerHelper::newEntry()
  37. {
  38. if (this->HelperState == State::Rule && !this->Content.empty()) {
  39. if (!this->Content.back().rules.empty() &&
  40. !this->Content.back().rules.back().empty()) {
  41. this->HelperState = State::Failed;
  42. }
  43. return;
  44. }
  45. this->HelperState = State::Rule;
  46. this->Content.emplace_back();
  47. this->newRule();
  48. }
  49. void cmGccDepfileLexerHelper::newRule()
  50. {
  51. auto& entry = this->Content.back();
  52. if (entry.rules.empty() || !entry.rules.back().empty()) {
  53. entry.rules.emplace_back();
  54. }
  55. }
  56. void cmGccDepfileLexerHelper::newDependency()
  57. {
  58. if (this->HelperState == State::Failed) {
  59. return;
  60. }
  61. this->HelperState = State::Dependency;
  62. auto& entry = this->Content.back();
  63. if (entry.paths.empty() || !entry.paths.back().empty()) {
  64. entry.paths.emplace_back();
  65. }
  66. }
  67. void cmGccDepfileLexerHelper::newRuleOrDependency()
  68. {
  69. if (this->HelperState == State::Rule) {
  70. this->newRule();
  71. } else if (this->HelperState == State::Dependency) {
  72. this->newDependency();
  73. }
  74. }
  75. void cmGccDepfileLexerHelper::addToCurrentPath(const char* s)
  76. {
  77. if (this->Content.empty()) {
  78. return;
  79. }
  80. cmGccStyleDependency* dep = &this->Content.back();
  81. std::string* dst = nullptr;
  82. switch (this->HelperState) {
  83. case State::Rule: {
  84. if (dep->rules.empty()) {
  85. return;
  86. }
  87. dst = &dep->rules.back();
  88. } break;
  89. case State::Dependency: {
  90. if (dep->paths.empty()) {
  91. return;
  92. }
  93. dst = &dep->paths.back();
  94. } break;
  95. case State::Failed:
  96. return;
  97. }
  98. dst->append(s);
  99. }
  100. void cmGccDepfileLexerHelper::sanitizeContent()
  101. {
  102. for (auto it = this->Content.begin(); it != this->Content.end();) {
  103. // Remove empty rules
  104. for (auto rit = it->rules.begin(); rit != it->rules.end();) {
  105. if (rit->empty()) {
  106. rit = it->rules.erase(rit);
  107. } else {
  108. ++rit;
  109. }
  110. }
  111. // Remove the entry if rules are empty
  112. if (it->rules.empty()) {
  113. it = this->Content.erase(it);
  114. } else {
  115. // Remove empty paths
  116. for (auto pit = it->paths.begin(); pit != it->paths.end();) {
  117. if (pit->empty()) {
  118. pit = it->paths.erase(pit);
  119. } else {
  120. ++pit;
  121. }
  122. }
  123. ++it;
  124. }
  125. }
  126. }