From 85ea60b7f1121a95ffc2429d367ec211692cef16 Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 26 Feb 2021 21:39:47 +0200 Subject: [PATCH] LibCpp: Don't fail when encountering #elif statements However, we currently always treat the expression in #elif as true. --- Userland/Libraries/LibCpp/Preprocessor.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCpp/Preprocessor.cpp b/Userland/Libraries/LibCpp/Preprocessor.cpp index 3094d2b97c..99bef96ea3 100644 --- a/Userland/Libraries/LibCpp/Preprocessor.cpp +++ b/Userland/Libraries/LibCpp/Preprocessor.cpp @@ -81,7 +81,6 @@ void Preprocessor::handle_preprocessor_line(const StringView& line) m_state = State::Normal; } if (m_depths_of_taken_branches.contains_slow(m_current_depth - 1)) { - m_depths_of_taken_branches.contains_slow(m_current_depth - 1); m_state = State::SkipElseBranch; } return; @@ -94,7 +93,7 @@ void Preprocessor::handle_preprocessor_line(const StringView& line) m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth; }); } if (m_depths_of_taken_branches.contains_slow(m_current_depth)) { - m_depths_of_taken_branches.contains_slow(m_current_depth); + m_depths_of_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth; }); } m_state = State::Normal; return; @@ -159,6 +158,20 @@ void Preprocessor::handle_preprocessor_line(const StringView& line) } return; } + + if (keyword == "elif") { + VERIFY(m_current_depth > 0); + // FIXME: Evaluate the elif expression + // We currently always treat the expression in #elif as true. + if (m_depths_of_not_taken_branches.contains_slow(m_current_depth - 1) /* && should_take*/) { + m_depths_of_not_taken_branches.remove_all_matching([this](auto x) { return x == m_current_depth - 1; }); + m_state = State::Normal; + } + if (m_depths_of_taken_branches.contains_slow(m_current_depth - 1)) { + m_state = State::SkipElseBranch; + } + return; + } if (keyword == "pragma") { lexer.consume_all(); return;