From 24f3b8dd3d2877db4deebf4159a34265bcf033bc Mon Sep 17 00:00:00 2001 From: Polshakov Dmitry Date: Sun, 13 Feb 2022 13:50:13 +0300 Subject: [PATCH] pattern: Make preprocessor not drop defines after include (#433) Co-authored-by: Dmitry Polshakov --- .../source/pattern_language/preprocessor.cpp | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/libimhex/source/pattern_language/preprocessor.cpp b/lib/libimhex/source/pattern_language/preprocessor.cpp index f5638f146..81dee9841 100644 --- a/lib/libimhex/source/pattern_language/preprocessor.cpp +++ b/lib/libimhex/source/pattern_language/preprocessor.cpp @@ -84,7 +84,7 @@ namespace hex::pl { preprocessor.m_defines = this->m_defines; preprocessor.m_onceIncludedFiles = this->m_onceIncludedFiles; - auto preprocessedInclude = preprocessor.preprocess(file.readString(), true); + auto preprocessedInclude = preprocessor.preprocess(file.readString(), /*initialRun =*/false); if (!preprocessedInclude.has_value()) throw *preprocessor.m_error; @@ -201,30 +201,28 @@ namespace hex::pl { offset += 1; } - if (initialRun) { - // Apply defines - std::vector> sortedDefines; - std::copy(this->m_defines.begin(), this->m_defines.end(), std::back_inserter(sortedDefines)); - std::sort(sortedDefines.begin(), sortedDefines.end(), [](const auto &left, const auto &right) { - return std::get<0>(left).size() > std::get<0>(right).size(); - }); + // Apply defines + std::vector> sortedDefines; + std::copy(this->m_defines.begin(), this->m_defines.end(), std::back_inserter(sortedDefines)); + std::sort(sortedDefines.begin(), sortedDefines.end(), [](const auto &left, const auto &right) { + return std::get<0>(left).size() > std::get<0>(right).size(); + }); - for (const auto &[define, value, defineLine] : sortedDefines) { - i32 index = 0; - while ((index = output.find(define, index)) != std::string::npos) { - output.replace(index, define.length(), value); - index += value.length(); - } + for (const auto &[define, value, defineLine] : sortedDefines) { + i32 index = 0; + while ((index = output.find(define, index)) != std::string::npos) { + output.replace(index, define.length(), value); + index += value.length(); } + } - // Handle pragmas - for (const auto &[type, value, pragmaLine] : this->m_pragmas) { - if (this->m_pragmaHandlers.contains(type)) { - if (!this->m_pragmaHandlers[type](value)) - throwPreprocessorError(hex::format("invalid value provided to '{0}' #pragma directive", type.c_str()), pragmaLine); - } else - throwPreprocessorError(hex::format("no #pragma handler registered for type {0}", type.c_str()), pragmaLine); - } + // Handle pragmas + for (const auto &[type, value, pragmaLine] : this->m_pragmas) { + if (this->m_pragmaHandlers.contains(type)) { + if (!this->m_pragmaHandlers[type](value)) + throwPreprocessorError(hex::format("invalid value provided to '{0}' #pragma directive", type.c_str()), pragmaLine); + } else + throwPreprocessorError(hex::format("no #pragma handler registered for type {0}", type.c_str()), pragmaLine); } } catch (PatternLanguageError &e) { this->m_error = e;