Preprocessor.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Preprocessor.h"
  27. #include <AK/Assertions.h>
  28. #include <AK/GenericLexer.h>
  29. #include <AK/StringBuilder.h>
  30. #include <ctype.h>
  31. namespace Cpp {
  32. Preprocessor::Preprocessor(const String& filename, const StringView& program)
  33. : m_filename(filename)
  34. , m_program(program)
  35. {
  36. m_lines = m_program.split_view('\n', true);
  37. }
  38. const String& Preprocessor::process()
  39. {
  40. for (; m_line_index < m_lines.size(); ++m_line_index) {
  41. auto& line = m_lines[m_line_index];
  42. if (line.starts_with("#")) {
  43. handle_preprocessor_line(line);
  44. } else if (m_state == State::Normal) {
  45. m_builder.append(line);
  46. }
  47. m_builder.append("\n");
  48. }
  49. m_processed_text = m_builder.to_string();
  50. return m_processed_text;
  51. }
  52. void Preprocessor::handle_preprocessor_line(const StringView& line)
  53. {
  54. GenericLexer lexer(line);
  55. auto consume_whitespace = [&] {
  56. lexer.ignore_while([](char ch) { return isspace(ch); });
  57. if (lexer.peek() == '/' && lexer.peek(1) == '/')
  58. lexer.ignore_until([](char ch) { return ch == '\n'; });
  59. };
  60. consume_whitespace();
  61. lexer.consume_specific('#');
  62. consume_whitespace();
  63. auto keyword = lexer.consume_until(' ');
  64. if (keyword.is_empty() || keyword.is_null() || keyword.is_whitespace())
  65. return;
  66. if (keyword == "include") {
  67. consume_whitespace();
  68. m_included_paths.append(lexer.consume_all());
  69. return;
  70. }
  71. if (keyword == "else") {
  72. VERIFY(m_current_depth > 0);
  73. if (m_depths_of_not_taken_branches.contains_slow(m_current_depth - 1)) {
  74. m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth - 1; });
  75. m_state = State::Normal;
  76. }
  77. if (m_depths_of_taken_branches.contains_slow(m_current_depth - 1)) {
  78. m_state = State::SkipElseBranch;
  79. }
  80. return;
  81. }
  82. if (keyword == "endif") {
  83. VERIFY(m_current_depth > 0);
  84. --m_current_depth;
  85. if (m_depths_of_not_taken_branches.contains_slow(m_current_depth)) {
  86. m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth; });
  87. }
  88. if (m_depths_of_taken_branches.contains_slow(m_current_depth)) {
  89. m_depths_of_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth; });
  90. }
  91. m_state = State::Normal;
  92. return;
  93. }
  94. if (keyword == "define") {
  95. if (m_state == State::Normal) {
  96. auto key = lexer.consume_until(' ');
  97. consume_whitespace();
  98. DefinedValue value;
  99. value.filename = m_filename;
  100. value.line = m_line_index;
  101. auto string_value = lexer.consume_all();
  102. if (!string_value.is_empty())
  103. value.value = string_value;
  104. m_definitions.set(key, value);
  105. }
  106. return;
  107. }
  108. if (keyword == "undef") {
  109. if (m_state == State::Normal) {
  110. auto key = lexer.consume_until(' ');
  111. lexer.consume_all();
  112. m_definitions.remove(key);
  113. }
  114. return;
  115. }
  116. if (keyword == "ifdef") {
  117. ++m_current_depth;
  118. if (m_state == State::Normal) {
  119. auto key = lexer.consume_until(' ');
  120. if (m_definitions.contains(key)) {
  121. m_depths_of_taken_branches.append(m_current_depth - 1);
  122. return;
  123. } else {
  124. m_depths_of_not_taken_branches.append(m_current_depth - 1);
  125. m_state = State::SkipIfBranch;
  126. return;
  127. }
  128. }
  129. return;
  130. }
  131. if (keyword == "ifndef") {
  132. ++m_current_depth;
  133. if (m_state == State::Normal) {
  134. auto key = lexer.consume_until(' ');
  135. if (!m_definitions.contains(key)) {
  136. m_depths_of_taken_branches.append(m_current_depth - 1);
  137. return;
  138. } else {
  139. m_depths_of_not_taken_branches.append(m_current_depth - 1);
  140. m_state = State::SkipIfBranch;
  141. return;
  142. }
  143. }
  144. return;
  145. }
  146. if (keyword == "if") {
  147. ++m_current_depth;
  148. if (m_state == State::Normal) {
  149. // FIXME: Implement #if logic
  150. // We currently always take #if branches.
  151. m_depths_of_taken_branches.append(m_current_depth - 1);
  152. }
  153. return;
  154. }
  155. if (keyword == "elif") {
  156. VERIFY(m_current_depth > 0);
  157. // FIXME: Evaluate the elif expression
  158. // We currently always treat the expression in #elif as true.
  159. if (m_depths_of_not_taken_branches.contains_slow(m_current_depth - 1) /* && should_take*/) {
  160. m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth - 1; });
  161. m_state = State::Normal;
  162. }
  163. if (m_depths_of_taken_branches.contains_slow(m_current_depth - 1)) {
  164. m_state = State::SkipElseBranch;
  165. }
  166. return;
  167. }
  168. if (keyword == "pragma") {
  169. lexer.consume_all();
  170. return;
  171. }
  172. if (!m_options.ignore_unsupported_keywords) {
  173. dbgln("Unsupported preprocessor keyword: {}", keyword);
  174. VERIFY_NOT_REACHED();
  175. }
  176. }
  177. const String& Preprocessor::processed_text()
  178. {
  179. VERIFY(!m_processed_text.is_null());
  180. return m_processed_text;
  181. }
  182. };