Preprocessor.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 StringView& program)
  33. : m_program(program)
  34. {
  35. m_lines = m_program.split_view('\n', true);
  36. }
  37. const String& Preprocessor::process()
  38. {
  39. for (; m_line_index < m_lines.size(); ++m_line_index) {
  40. auto& line = m_lines[m_line_index];
  41. if (line.starts_with("#")) {
  42. handle_preprocessor_line(line);
  43. } else if (m_state == State::Normal) {
  44. m_builder.append(line);
  45. }
  46. m_builder.append("\n");
  47. }
  48. m_processed_text = m_builder.to_string();
  49. return m_processed_text;
  50. }
  51. void Preprocessor::handle_preprocessor_line(const StringView& line)
  52. {
  53. GenericLexer lexer(line);
  54. auto consume_whitespace = [&] {
  55. lexer.ignore_while([](char ch) { return isspace(ch); });
  56. if (lexer.peek() == '/' && lexer.peek(1) == '/')
  57. lexer.ignore_until([](char ch) { return ch == '\n'; });
  58. };
  59. consume_whitespace();
  60. lexer.consume_specific('#');
  61. consume_whitespace();
  62. auto keyword = lexer.consume_until(' ');
  63. if (keyword.is_empty() || keyword.is_null() || keyword.is_whitespace())
  64. return;
  65. if (keyword == "include") {
  66. consume_whitespace();
  67. m_included_paths.append(lexer.consume_all());
  68. return;
  69. }
  70. if (keyword == "else") {
  71. VERIFY(m_current_depth > 0);
  72. if (m_depths_of_not_taken_branches.contains_slow(m_current_depth - 1)) {
  73. m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth - 1; });
  74. m_state = State::Normal;
  75. }
  76. if (m_depths_of_taken_branches.contains_slow(m_current_depth - 1)) {
  77. m_state = State::SkipElseBranch;
  78. }
  79. return;
  80. }
  81. if (keyword == "endif") {
  82. VERIFY(m_current_depth > 0);
  83. --m_current_depth;
  84. if (m_depths_of_not_taken_branches.contains_slow(m_current_depth)) {
  85. m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth; });
  86. }
  87. if (m_depths_of_taken_branches.contains_slow(m_current_depth)) {
  88. m_depths_of_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth; });
  89. }
  90. m_state = State::Normal;
  91. return;
  92. }
  93. if (keyword == "define") {
  94. if (m_state == State::Normal) {
  95. auto key = lexer.consume_until(' ');
  96. consume_whitespace();
  97. DefinedValue value;
  98. auto string_value = lexer.consume_all();
  99. if (!string_value.is_empty())
  100. value.value = string_value;
  101. m_definitions.set(key, value);
  102. }
  103. return;
  104. }
  105. if (keyword == "undef") {
  106. if (m_state == State::Normal) {
  107. auto key = lexer.consume_until(' ');
  108. lexer.consume_all();
  109. m_definitions.remove(key);
  110. }
  111. return;
  112. }
  113. if (keyword == "ifdef") {
  114. ++m_current_depth;
  115. if (m_state == State::Normal) {
  116. auto key = lexer.consume_until(' ');
  117. if (m_definitions.contains(key)) {
  118. m_depths_of_taken_branches.append(m_current_depth - 1);
  119. return;
  120. } else {
  121. m_depths_of_not_taken_branches.append(m_current_depth - 1);
  122. m_state = State::SkipIfBranch;
  123. return;
  124. }
  125. }
  126. return;
  127. }
  128. if (keyword == "ifndef") {
  129. ++m_current_depth;
  130. if (m_state == State::Normal) {
  131. auto key = lexer.consume_until(' ');
  132. if (!m_definitions.contains(key)) {
  133. m_depths_of_taken_branches.append(m_current_depth - 1);
  134. return;
  135. } else {
  136. m_depths_of_not_taken_branches.append(m_current_depth - 1);
  137. m_state = State::SkipIfBranch;
  138. return;
  139. }
  140. }
  141. return;
  142. }
  143. if (keyword == "if") {
  144. ++m_current_depth;
  145. if (m_state == State::Normal) {
  146. // FIXME: Implement #if logic
  147. // We currently always take #if branches.
  148. m_depths_of_taken_branches.append(m_current_depth - 1);
  149. }
  150. return;
  151. }
  152. if (keyword == "elif") {
  153. VERIFY(m_current_depth > 0);
  154. // FIXME: Evaluate the elif expression
  155. // We currently always treat the expression in #elif as true.
  156. if (m_depths_of_not_taken_branches.contains_slow(m_current_depth - 1) /* && should_take*/) {
  157. m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth - 1; });
  158. m_state = State::Normal;
  159. }
  160. if (m_depths_of_taken_branches.contains_slow(m_current_depth - 1)) {
  161. m_state = State::SkipElseBranch;
  162. }
  163. return;
  164. }
  165. if (keyword == "pragma") {
  166. lexer.consume_all();
  167. return;
  168. }
  169. dbgln("Unsupported preprocessor keyword: {}", keyword);
  170. VERIFY_NOT_REACHED();
  171. }
  172. };