From 11296409a794c698a777796c91a281d52352656d Mon Sep 17 00:00:00 2001 From: glank Date: Thu, 3 Jul 2025 21:13:40 +0200 Subject: [PATCH] Add conditionals skipping --- simplecpp.cpp | 15 ++++++++++++++- simplecpp.h | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index a69dc0c2..e9641b04 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -3532,6 +3532,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL // AlwaysFalse => drop all code in #if and #else enum IfState { True, ElseIsTrue, AlwaysFalse }; std::stack ifstates; + std::stack iftokens; ifstates.push(True); std::stack includetokenstack; @@ -3855,15 +3856,24 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL ifstates.push(AlwaysFalse); else ifstates.push(conditionIsTrue ? True : ElseIsTrue); + iftokens.push(rawtok); } else if (ifstates.top() == True) { ifstates.top() = AlwaysFalse; + iftokens.top()->nextcond = rawtok; + iftokens.top() = rawtok; } else if (ifstates.top() == ElseIsTrue && conditionIsTrue) { ifstates.top() = True; + iftokens.top()->nextcond = rawtok; + iftokens.top() = rawtok; } } else if (rawtok->str() == ELSE) { ifstates.top() = (ifstates.top() == ElseIsTrue) ? True : AlwaysFalse; + iftokens.top()->nextcond = rawtok; + iftokens.top() = rawtok; } else if (rawtok->str() == ENDIF) { ifstates.pop(); + iftokens.top()->nextcond = rawtok; + iftokens.pop(); } else if (rawtok->str() == UNDEF) { if (ifstates.top() == True) { const Token *tok = rawtok->next; @@ -3875,7 +3885,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL } else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) { pragmaOnce.insert(rawtok->location.file()); } - rawtok = gotoNextLine(rawtok); + if (ifstates.top() != True && rawtok->nextcond) + rawtok = rawtok->nextcond->previous; + else + rawtok = gotoNextLine(rawtok); continue; } diff --git a/simplecpp.h b/simplecpp.h index 9fd95808..579e6e14 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -96,12 +96,12 @@ namespace simplecpp { class SIMPLECPP_LIB Token { public: Token(const TokenString &s, const Location &loc, bool wsahead = false) : - whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), string(s) { + whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), nextcond(nullptr), string(s) { flags(); } Token(const Token &tok) : - macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) { + macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), nextcond(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) { } void flags() { @@ -137,6 +137,7 @@ namespace simplecpp { Location location; Token *previous; Token *next; + mutable const Token *nextcond; const Token *previousSkipComments() const { const Token *tok = this->previous;