From dacf7a9fd8fb9180856f19a9fefb883481f963fa Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 12:55:58 +0200 Subject: [PATCH 01/24] Test 4 --- simplecpp.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/simplecpp.h b/simplecpp.h index f5c69593..0f41cba4 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -301,6 +301,9 @@ namespace simplecpp { void constFoldLogicalOp(Token *tok); void constFoldQuestionOp(Token **tok1); + //Token * deleteUntil(Token *tok, const std::string & breakPoint, Token *(step)(Token *)); + //static int getTokensDeleteCount(Token *tok, const std::set & breakPoints, Token *(step)(Token *), const std::pair & brackets); + std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList); void lineDirective(unsigned int fileIndex, unsigned int line, Location *location); From e188d4afb78438c5a099170dc948f03a2757064a Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 13:01:29 +0200 Subject: [PATCH 02/24] Test 2 --- simplecpp.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++---- simplecpp.h | 2 +- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 20ae2528..0154fd33 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -954,7 +954,7 @@ void simplecpp::TokenList::constFold() constFoldQuestionOp(&tok); // If there is no '(' we are done with the constant folding - if (tok->op != '(') + if (!tok || tok->op != '(') break; if (!tok->next || !tok->next->next || tok->next->next->op != ')') @@ -1290,6 +1290,55 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok) } } +static simplecpp::Token * stepForward(simplecpp::Token *tok) +{ + if (!tok) + return nullptr; + return tok->next; +} + +static simplecpp::Token * stepBack(simplecpp::Token *tok) +{ + if (!tok) + return nullptr; + return tok->previous; +} + +/* simplecpp::Token * simplecpp::TokenList::deleteUntil(Token *tok, const std::string & breakPoint, Token *(step)(Token *)) +{ + while (tok) { + if (tok->str() == breakPoint) + return tok; + Token * oldToken = tok; + tok = (step)(tok); + deleteToken(oldToken); + } + return nullptr; +} */ + +int simplecpp::TokenList::getTokensDeleteCount(Token *tok, const std::set & breakPoints, Token *(step)(Token *), const std::pair & brackets) +{ + int count = 0; + bool skip = false; + for (; tok; tok = (step)(tok)) { + if (skip){ + ++count; + if (tok->str() == brackets.second) { + skip = false; + continue; + } + } else if (tok->str() == brackets.first) { + skip = true; + ++count; + } else if (breakPoints.count(tok->str()) != 0) { + break; + } else { + ++count; + } + } + return count; +} + static const std::string AND("and"); static const std::string OR("or"); void simplecpp::TokenList::constFoldLogicalOp(Token *tok) @@ -1303,16 +1352,57 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) } if (tok->str() != "&&" && tok->str() != "||") continue; - if (!tok->previous || !tok->previous->number) + if (!tok->previous || !tok->next) continue; - if (!tok->next || !tok->next->number) + if (!tok->previous->number && !tok->next->number) continue; int result; - if (tok->str() == "||") - result = (stringToLL(tok->previous->str()) || stringToLL(tok->next->str())); - else /*if (tok->str() == "&&")*/ - result = (stringToLL(tok->previous->str()) && stringToLL(tok->next->str())); + std::set breakPoints; + breakPoints.insert(":"); + breakPoints.insert("?"); + if (tok->str() == "||"){ + if (stringToLL(tok->previous->str())) { + int count = getTokensDeleteCount(tok->next, breakPoints, &stepForward, std::make_pair("(", ")")); + tok = tok->previous; + count += 2; + tok->setstr(toString(1)); + for (; count > 0; --count) + deleteToken(tok->next); + break; + } + if (stringToLL(tok->next->str())) { + int count = getTokensDeleteCount(tok->previous, breakPoints, &stepBack, std::make_pair(")", "(")); + tok = tok->next; + count += 2; + tok->setstr(toString(1)); + for (; count > 0; --count) + deleteToken(tok->previous); + break; + } + result = 0; + } else /*if (tok->str() == "&&")*/ { + breakPoints.insert("||"); + if (!stringToLL(tok->previous->str())) { + int count = getTokensDeleteCount(tok->next, breakPoints, &stepForward, std::make_pair("(", ")")); + tok = tok->previous; + count += 2; + tok->setstr(toString(0)); + for (; count > 0; --count) + deleteToken(tok->next); + break; + } + if (!stringToLL(tok->next->str())) { + int count = getTokensDeleteCount(tok->previous, breakPoints, &stepBack, std::make_pair(")", "(")); + tok = tok->next; + count += 2; + tok->setstr(toString(0)); + for (; count > 0; --count) + deleteToken(tok->previous); + break; + } + result = 1; + } tok = tok->previous; tok->setstr(toString(result)); diff --git a/simplecpp.h b/simplecpp.h index 0f41cba4..11fb6ca8 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -302,7 +302,7 @@ namespace simplecpp { void constFoldQuestionOp(Token **tok1); //Token * deleteUntil(Token *tok, const std::string & breakPoint, Token *(step)(Token *)); - //static int getTokensDeleteCount(Token *tok, const std::set & breakPoints, Token *(step)(Token *), const std::pair & brackets); + static int getTokensDeleteCount(Token *tok, const std::set & breakPoints, Token *(step)(Token *), const std::pair & brackets); std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList); void lineDirective(unsigned int fileIndex, unsigned int line, Location *location); From 5277d211f5b4b562a6d91c98c4103f4237d47240 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 13:27:51 +0200 Subject: [PATCH 03/24] Test 33 --- simplecpp.cpp | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 0154fd33..9472f515 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1320,6 +1320,7 @@ int simplecpp::TokenList::getTokensDeleteCount(Token *tok, const std::setstr() == "||"){ if (stringToLL(tok->previous->str())) { int count = getTokensDeleteCount(tok->next, breakPoints, &stepForward, std::make_pair("(", ")")); - tok = tok->previous; - count += 2; - tok->setstr(toString(1)); for (; count > 0; --count) deleteToken(tok->next); - break; - } - if (stringToLL(tok->next->str())) { + result = 1; + } else if (stringToLL(tok->next->str())) { int count = getTokensDeleteCount(tok->previous, breakPoints, &stepBack, std::make_pair(")", "(")); - tok = tok->next; - count += 2; - tok->setstr(toString(1)); for (; count > 0; --count) deleteToken(tok->previous); - break; - } - result = 0; + result = 1; + } else + result = 0; } else /*if (tok->str() == "&&")*/ { breakPoints.insert("||"); if (!stringToLL(tok->previous->str())) { int count = getTokensDeleteCount(tok->next, breakPoints, &stepForward, std::make_pair("(", ")")); - tok = tok->previous; - count += 2; - tok->setstr(toString(0)); for (; count > 0; --count) deleteToken(tok->next); - break; - } - if (!stringToLL(tok->next->str())) { + result = 0; + } else if (!stringToLL(tok->next->str())) { int count = getTokensDeleteCount(tok->previous, breakPoints, &stepBack, std::make_pair(")", "(")); - tok = tok->next; - count += 2; - tok->setstr(toString(0)); for (; count > 0; --count) deleteToken(tok->previous); - break; - } - result = 1; + result = 0; + } else + result = 1; } tok = tok->previous; From c30f7be8fdc27a3c101df1b32dc1534600aca75b Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 13:32:41 +0200 Subject: [PATCH 04/24] Nits --- test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test.cpp b/test.cpp index 3ff00b33..e55bda16 100644 --- a/test.cpp +++ b/test.cpp @@ -452,6 +452,10 @@ static void constFold() ASSERT_EQUALS("1", testConstFold("010==8")); ASSERT_EQUALS("exception", testConstFold("!1 ? 2 :")); ASSERT_EQUALS("exception", testConstFold("?2:3")); + ASSERT_EQUALS("0", testConstFold("( 0 ) && 10 < X")); + ASSERT_EQUALS("0", testConstFold("1+2*(3+4) && 7 - 7")); + ASSERT_EQUALS("1", testConstFold("( 1 ) || 10 < X")); + ASSERT_EQUALS("1", testConstFold("1+2*(3+4) || 8 - 7")); } #ifdef __CYGWIN__ From 8999ca16af07f4c0c9a858769689c859dfc39783 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 14:58:19 +0200 Subject: [PATCH 05/24] Nits --- simplecpp.cpp | 64 ++++++++++++++++++--------------------------------- simplecpp.h | 5 ++-- 2 files changed, 25 insertions(+), 44 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 9472f515..505f2a01 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1304,19 +1304,7 @@ static simplecpp::Token * stepBack(simplecpp::Token *tok) return tok->previous; } -/* simplecpp::Token * simplecpp::TokenList::deleteUntil(Token *tok, const std::string & breakPoint, Token *(step)(Token *)) -{ - while (tok) { - if (tok->str() == breakPoint) - return tok; - Token * oldToken = tok; - tok = (step)(tok); - deleteToken(oldToken); - } - return nullptr; -} */ - -int simplecpp::TokenList::getTokensDeleteCount(Token *tok, const std::set & breakPoints, Token *(step)(Token *), const std::pair & brackets) +static int getTokensDeleteCount(simplecpp::Token *tok, const std::set & breakPoints, simplecpp::Token *(step)(simplecpp::Token *), const std::pair & brackets) { int count = 0; bool skip = false; @@ -1340,6 +1328,22 @@ int simplecpp::TokenList::getTokensDeleteCount(Token *tok, const std::setprevious; + tok->setstr(result); + deleteToken(tok->next); + deleteToken(tok->next); +} + +void simplecpp::TokenList::squashTokens(Token *tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) +{ + Token *(*step)(Token *) = forwardDirection ? &stepForward : &stepBack; + for (int count = getTokensDeleteCount((step)(tok), breakPoints, step, forwardDirection ? std::make_pair("(", ")") : std::make_pair(")", "(")); count > 0; --count) + deleteToken((step)(tok)); + simpleSquash(tok, result); +} + static const std::string AND("and"); static const std::string OR("or"); void simplecpp::TokenList::constFoldLogicalOp(Token *tok) @@ -1358,43 +1362,21 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) if (!tok->previous->number && !tok->next->number) continue; - int result; std::set breakPoints; breakPoints.insert(":"); breakPoints.insert("?"); if (tok->str() == "||"){ - if (stringToLL(tok->previous->str())) { - int count = getTokensDeleteCount(tok->next, breakPoints, &stepForward, std::make_pair("(", ")")); - for (; count > 0; --count) - deleteToken(tok->next); - result = 1; - } else if (stringToLL(tok->next->str())) { - int count = getTokensDeleteCount(tok->previous, breakPoints, &stepBack, std::make_pair(")", "(")); - for (; count > 0; --count) - deleteToken(tok->previous); - result = 1; + if (stringToLL(tok->previous->str()) || stringToLL(tok->next->str())) { + squashTokens(tok, breakPoints, stringToLL(tok->previous->str()), toString(1)); } else - result = 0; + simpleSquash(tok, toString(0)); } else /*if (tok->str() == "&&")*/ { breakPoints.insert("||"); - if (!stringToLL(tok->previous->str())) { - int count = getTokensDeleteCount(tok->next, breakPoints, &stepForward, std::make_pair("(", ")")); - for (; count > 0; --count) - deleteToken(tok->next); - result = 0; - } else if (!stringToLL(tok->next->str())) { - int count = getTokensDeleteCount(tok->previous, breakPoints, &stepBack, std::make_pair(")", "(")); - for (; count > 0; --count) - deleteToken(tok->previous); - result = 0; + if (!stringToLL(tok->previous->str()) || !stringToLL(tok->next->str())) { + squashTokens(tok, breakPoints, !stringToLL(tok->previous->str()), toString(0)); } else - result = 1; + simpleSquash(tok, toString(1)); } - - tok = tok->previous; - tok->setstr(toString(result)); - deleteToken(tok->next); - deleteToken(tok->next); } } diff --git a/simplecpp.h b/simplecpp.h index 11fb6ca8..5ed34bea 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -301,9 +301,8 @@ namespace simplecpp { void constFoldLogicalOp(Token *tok); void constFoldQuestionOp(Token **tok1); - //Token * deleteUntil(Token *tok, const std::string & breakPoint, Token *(step)(Token *)); - static int getTokensDeleteCount(Token *tok, const std::set & breakPoints, Token *(step)(Token *), const std::pair & brackets); - + void simpleSquash(Token *tok, const std::string & result); + void squashTokens(Token *tok, const std::set & breakPoints, bool forwardDirection, const std::string & result); std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList); void lineDirective(unsigned int fileIndex, unsigned int line, Location *location); From 0b2bd981f02308f957b6d74943f2b015b6d26bdf Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 15:44:21 +0200 Subject: [PATCH 06/24] Nits 7 --- simplecpp.cpp | 2 +- simplecpp.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 505f2a01..015ca134 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1328,7 +1328,7 @@ static int getTokensDeleteCount(simplecpp::Token *tok, const std::setprevious; tok->setstr(result); diff --git a/simplecpp.h b/simplecpp.h index 5ed34bea..499f9d9d 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -301,7 +301,7 @@ namespace simplecpp { void constFoldLogicalOp(Token *tok); void constFoldQuestionOp(Token **tok1); - void simpleSquash(Token *tok, const std::string & result); + void simpleSquash(Token *&tok, const std::string & result); void squashTokens(Token *tok, const std::set & breakPoints, bool forwardDirection, const std::string & result); std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList); void lineDirective(unsigned int fileIndex, unsigned int line, Location *location); From e8bf596b5149947fd6a967a29915cd9c6ae45b3f Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 15:47:57 +0200 Subject: [PATCH 07/24] Should work --- simplecpp.cpp | 2 +- simplecpp.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 015ca134..f6630d1a 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1336,7 +1336,7 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) deleteToken(tok->next); } -void simplecpp::TokenList::squashTokens(Token *tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) +void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { Token *(*step)(Token *) = forwardDirection ? &stepForward : &stepBack; for (int count = getTokensDeleteCount((step)(tok), breakPoints, step, forwardDirection ? std::make_pair("(", ")") : std::make_pair(")", "(")); count > 0; --count) diff --git a/simplecpp.h b/simplecpp.h index 499f9d9d..0be48306 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -302,7 +302,7 @@ namespace simplecpp { void constFoldQuestionOp(Token **tok1); void simpleSquash(Token *&tok, const std::string & result); - void squashTokens(Token *tok, const std::set & breakPoints, bool forwardDirection, const std::string & result); + void squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result); std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList); void lineDirective(unsigned int fileIndex, unsigned int line, Location *location); From e25615cd7ee06f7f30d67e3455354105637789ac Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 15:57:09 +0200 Subject: [PATCH 08/24] Nit --- simplecpp.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index f6630d1a..454a5666 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1338,8 +1338,13 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { - Token *(*step)(Token *) = forwardDirection ? &stepForward : &stepBack; - for (int count = getTokensDeleteCount((step)(tok), breakPoints, step, forwardDirection ? std::make_pair("(", ")") : std::make_pair(")", "(")); count > 0; --count) + Token *(*step)(Token *) = &stepForward; + std::pair brackets = std::make_pair("(", ")"); + if (!forwardDirection) { + step = &stepBack; + brackets = std::make_pair(")", "("); + } + for (int count = getTokensDeleteCount((step)(tok), breakPoints, step, brackets); count > 0; --count) deleteToken((step)(tok)); simpleSquash(tok, result); } From cb2b3ef4082a178db42b9d21a5a5ccdfa2654ef1 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 3 Feb 2025 16:02:00 +0200 Subject: [PATCH 09/24] Nit --- simplecpp.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 454a5666..f2d4ab4e 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1164,10 +1164,7 @@ void simplecpp::TokenList::constFoldMulDivRem(Token *tok) } else continue; - tok = tok->previous; - tok->setstr(toString(result)); - deleteToken(tok->next); - deleteToken(tok->next); + simpleSquash(tok, toString(result)); } } @@ -1187,10 +1184,7 @@ void simplecpp::TokenList::constFoldAddSub(Token *tok) else continue; - tok = tok->previous; - tok->setstr(toString(result)); - deleteToken(tok->next); - deleteToken(tok->next); + simpleSquash(tok, toString(result)); } } @@ -1210,10 +1204,7 @@ void simplecpp::TokenList::constFoldShift(Token *tok) else continue; - tok = tok->previous; - tok->setstr(toString(result)); - deleteToken(tok->next); - deleteToken(tok->next); + simpleSquash(tok, toString(result)); } } @@ -1247,10 +1238,7 @@ void simplecpp::TokenList::constFoldComparison(Token *tok) else continue; - tok = tok->previous; - tok->setstr(toString(result)); - deleteToken(tok->next); - deleteToken(tok->next); + simpleSquash(tok, toString(result)); } } @@ -1282,10 +1270,7 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok) result = (stringToLL(tok->previous->str()) ^ stringToLL(tok->next->str())); else /*if (*op == '|')*/ result = (stringToLL(tok->previous->str()) | stringToLL(tok->next->str())); - tok = tok->previous; - tok->setstr(toString(result)); - deleteToken(tok->next); - deleteToken(tok->next); + simpleSquash(tok, toString(result)); } } } From 3f34ef0e674d33a7a7b5b62e75cecdaf91a90831 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 4 Feb 2025 00:47:56 +0200 Subject: [PATCH 10/24] Nits --- simplecpp.cpp | 41 +++++++++++++++++++++++++++++++++++------ test.cpp | 18 ++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index f2d4ab4e..e78c2b96 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1334,6 +1334,37 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set simpleSquash(tok, result); } +static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool forwardDirection) +{ + simplecpp::Token *(*step)(simplecpp::Token *) = &stepForward; + int brackets = 0; + bool ret = false; + if (!forwardDirection) + step = &stepBack; + + for (; tok; tok = (step)(tok)) { + if (stringToLL(tok->str()) == value) { + ret = true; + continue; + } + if (tok->op == ')') { + if (brackets) { + --brackets; + continue; + } + if (ret) + break; + return false; + } + if (tok->op == '(') { + ++brackets; + continue; + } + return false; + } + return ret; +} + static const std::string AND("and"); static const std::string OR("or"); void simplecpp::TokenList::constFoldLogicalOp(Token *tok) @@ -1356,16 +1387,14 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) breakPoints.insert(":"); breakPoints.insert("?"); if (tok->str() == "||"){ - if (stringToLL(tok->previous->str()) || stringToLL(tok->next->str())) { + if (checkSideForSingleInt(tok->previous, 1LL, false) || checkSideForSingleInt(tok->next, 1LL, true)) squashTokens(tok, breakPoints, stringToLL(tok->previous->str()), toString(1)); - } else - simpleSquash(tok, toString(0)); } else /*if (tok->str() == "&&")*/ { breakPoints.insert("||"); - if (!stringToLL(tok->previous->str()) || !stringToLL(tok->next->str())) { + if (checkSideForSingleInt(tok->previous, 0LL, false) || checkSideForSingleInt(tok->next, 0LL, true)) squashTokens(tok, breakPoints, !stringToLL(tok->previous->str()), toString(0)); - } else - simpleSquash(tok, toString(1)); + else if (checkSideForSingleInt(tok->previous, 1LL, false) == checkSideForSingleInt(tok->next, 1LL, true)) + simpleSquash(tok, "1"); } } } diff --git a/test.cpp b/test.cpp index e55bda16..fb3a5b75 100644 --- a/test.cpp +++ b/test.cpp @@ -456,6 +456,7 @@ static void constFold() ASSERT_EQUALS("0", testConstFold("1+2*(3+4) && 7 - 7")); ASSERT_EQUALS("1", testConstFold("( 1 ) || 10 < X")); ASSERT_EQUALS("1", testConstFold("1+2*(3+4) || 8 - 7")); + ASSERT_EQUALS("X >= 0 || 0 < Y", testConstFold("X >= 0 || 0 < Y")); } #ifdef __CYGWIN__ @@ -1602,6 +1603,22 @@ static void ifA() ASSERT_EQUALS("\nX", preprocess(code, dui)); } +static void ifXorY() +{ + const char code[] = "#if Z > 0 || 0 < Y\n" + "X\n" + "#endif"; + ASSERT_EQUALS("", preprocess(code)); + + simplecpp::DUI dui; + dui.defines.push_back("Z=1"); + ASSERT_EQUALS("\nX", preprocess(code, dui)); + + dui.defines.clear(); + dui.defines.push_back("Y=15"); + ASSERT_EQUALS("\nX", preprocess(code, dui)); +} + static void ifCharLiteral() { const char code[] = "#if ('A'==0x41)\n" @@ -3108,6 +3125,7 @@ int main(int argc, char **argv) TEST_CASE(ifdef2); TEST_CASE(ifndef); TEST_CASE(ifA); + TEST_CASE(ifXorY); TEST_CASE(ifCharLiteral); TEST_CASE(ifDefined); TEST_CASE(ifDefinedNoPar); From c32a3becded2d3cadc14d32fdcebde78f3b56841 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Wed, 5 Feb 2025 11:32:50 +0200 Subject: [PATCH 11/24] Nits --- simplecpp.cpp | 52 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index e78c2b96..a4c736a3 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1289,30 +1289,6 @@ static simplecpp::Token * stepBack(simplecpp::Token *tok) return tok->previous; } -static int getTokensDeleteCount(simplecpp::Token *tok, const std::set & breakPoints, simplecpp::Token *(step)(simplecpp::Token *), const std::pair & brackets) -{ - int count = 0; - bool skip = false; - tok = (step)(tok); - for (; tok; tok = (step)(tok)) { - if (skip){ - ++count; - if (tok->str() == brackets.second) { - skip = false; - continue; - } - } else if (tok->str() == brackets.first) { - skip = true; - ++count; - } else if (breakPoints.count(tok->str()) != 0) { - break; - } else { - ++count; - } - } - return count; -} - void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) { tok = tok->previous; @@ -1323,14 +1299,34 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { - Token *(*step)(Token *) = &stepForward; + simplecpp::Token *(*step)(simplecpp::Token *) = &stepForward; std::pair brackets = std::make_pair("(", ")"); if (!forwardDirection) { step = &stepBack; brackets = std::make_pair(")", "("); } - for (int count = getTokensDeleteCount((step)(tok), breakPoints, step, brackets); count > 0; --count) - deleteToken((step)(tok)); + int count = 0; + int skip = 0; + Token * tok2 = step(tok); + tok2 = step(tok2); + for (; tok2; tok2 = step(tok2)) { + if (skip){ + ++count; + if (tok->str() == brackets.second) { + --skip; + continue; + } + } else if (tok2->str() == brackets.first) { + ++skip; + ++count; + } else if (breakPoints.count(tok2->str()) != 0) { + break; + } else { + ++count; + } + } + for (; count > 0; --count) + deleteToken(step(tok)); simpleSquash(tok, result); } @@ -1342,7 +1338,7 @@ static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool if (!forwardDirection) step = &stepBack; - for (; tok; tok = (step)(tok)) { + for (; tok; tok = step(tok)) { if (stringToLL(tok->str()) == value) { ret = true; continue; From 47c9f1ed73583624b194cc2347911020164d5046 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 12:36:28 +0000 Subject: [PATCH 12/24] Nits --- simplecpp.cpp | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index a4c736a3..babdbd0b 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1299,34 +1299,31 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { - simplecpp::Token *(*step)(simplecpp::Token *) = &stepForward; - std::pair brackets = std::make_pair("(", ")"); - if (!forwardDirection) { - step = &stepBack; - brackets = std::make_pair(")", "("); - } - int count = 0; + const char *brackets = forwardDirection ? (const char[2]) {'(',')'} : (const char[2]) {')','('}; int skip = 0; - Token * tok2 = step(tok); - tok2 = step(tok2); - for (; tok2; tok2 = step(tok2)) { + Token * tok1 = forwardDirection ? tok->next : tok->previous; + if (!tok1) { + simpleSquash(tok, result); + return; + } + for (Token * tok2 = forwardDirection ? tok1->next : tok1->previous; tok1 && tok2; tok1 = tok2, tok2 = forwardDirection ? tok2->next : tok2->previous) { if (skip){ - ++count; - if (tok->str() == brackets.second) { + if (tok2->op == brackets[1]) --skip; - continue; - } - } else if (tok2->str() == brackets.first) { + deleteToken(tok2); + tok2 = tok1; + } else if (tok2->op == brackets[0]) { ++skip; - ++count; + deleteToken(tok2); + tok2 = tok1; } else if (breakPoints.count(tok2->str()) != 0) { + deleteToken(tok2); break; } else { - ++count; + deleteToken(tok2); + tok2 = tok1; } } - for (; count > 0; --count) - deleteToken(step(tok)); simpleSquash(tok, result); } From d0844d795c2986be995b99907187c5606799d49c Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 12:43:05 +0000 Subject: [PATCH 13/24] Nits 2 --- simplecpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index babdbd0b..e924f89a 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1299,7 +1299,7 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { - const char *brackets = forwardDirection ? (const char[2]) {'(',')'} : (const char[2]) {')','('}; + const char *brackets = forwardDirection ? "()" : ")("; int skip = 0; Token * tok1 = forwardDirection ? tok->next : tok->previous; if (!tok1) { From c64c08eb07883afcfb29bef92815990d6d0dba34 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 12:46:33 +0000 Subject: [PATCH 14/24] Nits 3 --- simplecpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index e924f89a..7d4294e0 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1299,7 +1299,7 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { - const char *brackets = forwardDirection ? "()" : ")("; + const char const *brackets = forwardDirection ? "()" : ")("; int skip = 0; Token * tok1 = forwardDirection ? tok->next : tok->previous; if (!tok1) { From dca1bdfe065b44d91a6bdeb5f6954a7c613b7949 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 13:47:45 +0000 Subject: [PATCH 15/24] Nits 4 --- simplecpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 7d4294e0..49db7d58 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1299,7 +1299,7 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { - const char const *brackets = forwardDirection ? "()" : ")("; + const char * const brackets = forwardDirection ? "()" : ")("; int skip = 0; Token * tok1 = forwardDirection ? tok->next : tok->previous; if (!tok1) { From 9f4d7ca1316d9f6cab57f503fb235b9fd314434a Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 14:30:17 +0000 Subject: [PATCH 16/24] Nits 5 --- simplecpp.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 49db7d58..fcddcabd 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1300,28 +1300,22 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { const char * const brackets = forwardDirection ? "()" : ")("; + Token* Token:: * const step = forwardDirection ? &Token::next : &Token::previous; int skip = 0; - Token * tok1 = forwardDirection ? tok->next : tok->previous; - if (!tok1) { - simpleSquash(tok, result); - return; - } - for (Token * tok2 = forwardDirection ? tok1->next : tok1->previous; tok1 && tok2; tok1 = tok2, tok2 = forwardDirection ? tok2->next : tok2->previous) { + Token * tok1 = tok->*step; + while (tok1 && tok1->*step) { if (skip){ - if (tok2->op == brackets[1]) + if ((tok1->*step)->op == brackets[1]) --skip; - deleteToken(tok2); - tok2 = tok1; - } else if (tok2->op == brackets[0]) { + deleteToken(tok1->*step); + } else if ((tok1->*step)->op == brackets[0]) { ++skip; - deleteToken(tok2); - tok2 = tok1; - } else if (breakPoints.count(tok2->str()) != 0) { - deleteToken(tok2); + deleteToken(tok1->*step); + } else if (breakPoints.count((tok1->*step)->str()) != 0) { + deleteToken(tok1->*step); break; } else { - deleteToken(tok2); - tok2 = tok1; + deleteToken(tok1->*step); } } simpleSquash(tok, result); From d9543e2236ae6df278238e2d89e69a6a97d4481b Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 14:49:14 +0000 Subject: [PATCH 17/24] Ci fix --- simplecpp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index fcddcabd..e07b4ee4 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1300,9 +1300,9 @@ void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) void simplecpp::TokenList::squashTokens(Token *&tok, const std::set & breakPoints, bool forwardDirection, const std::string & result) { const char * const brackets = forwardDirection ? "()" : ")("; - Token* Token:: * const step = forwardDirection ? &Token::next : &Token::previous; + Token* Token::* const step = forwardDirection ? &Token::next : &Token::previous; int skip = 0; - Token * tok1 = tok->*step; + const Token * tok1 = tok->*step; while (tok1 && tok1->*step) { if (skip){ if ((tok1->*step)->op == brackets[1]) From 2ebe063042ce564180e6d5421634d12b17464de7 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 6 Feb 2025 14:53:12 +0000 Subject: [PATCH 18/24] Ci fix 2 --- simplecpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index e07b4ee4..d3ae3163 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1302,7 +1302,7 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set const char * const brackets = forwardDirection ? "()" : ")("; Token* Token::* const step = forwardDirection ? &Token::next : &Token::previous; int skip = 0; - const Token * tok1 = tok->*step; + const Token * const tok1 = tok->*step; while (tok1 && tok1->*step) { if (skip){ if ((tok1->*step)->op == brackets[1]) From e927b1c4674fb94fa26d8e51163897084e2cb5ec Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 7 Feb 2025 10:59:41 +0000 Subject: [PATCH 19/24] Nits --- simplecpp.cpp | 35 +++++++++-------------------------- test.cpp | 1 + 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index d3ae3163..5517318a 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1275,20 +1275,6 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok) } } -static simplecpp::Token * stepForward(simplecpp::Token *tok) -{ - if (!tok) - return nullptr; - return tok->next; -} - -static simplecpp::Token * stepBack(simplecpp::Token *tok) -{ - if (!tok) - return nullptr; - return tok->previous; -} - void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result) { tok = tok->previous; @@ -1321,16 +1307,13 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set simpleSquash(tok, result); } -static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool forwardDirection) +static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool forwardDirection, bool any = false) { - simplecpp::Token *(*step)(simplecpp::Token *) = &stepForward; + simplecpp::Token* simplecpp::Token::* const step = forwardDirection ? &simplecpp::Token::next : &simplecpp::Token::previous; int brackets = 0; bool ret = false; - if (!forwardDirection) - step = &stepBack; - - for (; tok; tok = step(tok)) { - if (stringToLL(tok->str()) == value) { + for (; tok; tok = tok->*step) { + if (tok->number && (stringToLL(tok->str()) == value || any)) { ret = true; continue; } @@ -1374,13 +1357,13 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) breakPoints.insert(":"); breakPoints.insert("?"); if (tok->str() == "||"){ - if (checkSideForSingleInt(tok->previous, 1LL, false) || checkSideForSingleInt(tok->next, 1LL, true)) - squashTokens(tok, breakPoints, stringToLL(tok->previous->str()), toString(1)); + if (checkSideForSingleInt(tok->previous, 1LL, false) || (checkSideForSingleInt(tok->next, 1LL, true) && checkSideForSingleInt(tok->previous, 1LL, false, true))) + squashTokens(tok, breakPoints, checkSideForSingleInt(tok->previous, 1LL, false), toString(1)); } else /*if (tok->str() == "&&")*/ { breakPoints.insert("||"); - if (checkSideForSingleInt(tok->previous, 0LL, false) || checkSideForSingleInt(tok->next, 0LL, true)) - squashTokens(tok, breakPoints, !stringToLL(tok->previous->str()), toString(0)); - else if (checkSideForSingleInt(tok->previous, 1LL, false) == checkSideForSingleInt(tok->next, 1LL, true)) + if (checkSideForSingleInt(tok->previous, 0LL, false) || (checkSideForSingleInt(tok->next, 0LL, true) && checkSideForSingleInt(tok->previous, 0LL, false, true))) + squashTokens(tok, breakPoints, checkSideForSingleInt(tok->previous, 0LL, false), toString(0)); + else if (checkSideForSingleInt(tok->previous, 1LL, false) && checkSideForSingleInt(tok->previous, 1LL, false) == checkSideForSingleInt(tok->next, 1LL, true)) simpleSquash(tok, "1"); } } diff --git a/test.cpp b/test.cpp index fb3a5b75..12d5ef92 100644 --- a/test.cpp +++ b/test.cpp @@ -456,6 +456,7 @@ static void constFold() ASSERT_EQUALS("0", testConstFold("1+2*(3+4) && 7 - 7")); ASSERT_EQUALS("1", testConstFold("( 1 ) || 10 < X")); ASSERT_EQUALS("1", testConstFold("1+2*(3+4) || 8 - 7")); + ASSERT_EQUALS("X && 0", testConstFold("X && 0")); ASSERT_EQUALS("X >= 0 || 0 < Y", testConstFold("X >= 0 || 0 < Y")); } From ff0f1fd00693e58310c85a3b212853134d609711 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Sun, 9 Feb 2025 17:04:39 +0000 Subject: [PATCH 20/24] Fix --- simplecpp.cpp | 29 +++++++++++++++-------------- test.cpp | 1 + 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 5517318a..f541f1f5 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1290,10 +1290,12 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set int skip = 0; const Token * const tok1 = tok->*step; while (tok1 && tok1->*step) { - if (skip){ - if ((tok1->*step)->op == brackets[1]) + if ((tok1->*step)->op == brackets[1]){ + if (skip) { --skip; - deleteToken(tok1->*step); + deleteToken(tok1->*step); + } else + break; } else if ((tok1->*step)->op == brackets[0]) { ++skip; deleteToken(tok1->*step); @@ -1310,25 +1312,24 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool forwardDirection, bool any = false) { simplecpp::Token* simplecpp::Token::* const step = forwardDirection ? &simplecpp::Token::next : &simplecpp::Token::previous; - int brackets = 0; + const char * const brackets = forwardDirection ? "()" : ")("; + int bracket = 0; bool ret = false; for (; tok; tok = tok->*step) { if (tok->number && (stringToLL(tok->str()) == value || any)) { ret = true; continue; } - if (tok->op == ')') { - if (brackets) { - --brackets; + if (tok->op == brackets[0]) { + ++bracket; + continue; + } + if (tok->op == brackets[1]) { + if (bracket) { + --bracket; continue; } - if (ret) - break; - return false; - } - if (tok->op == '(') { - ++brackets; - continue; + break; } return false; } diff --git a/test.cpp b/test.cpp index 12d5ef92..a86d963f 100644 --- a/test.cpp +++ b/test.cpp @@ -458,6 +458,7 @@ static void constFold() ASSERT_EQUALS("1", testConstFold("1+2*(3+4) || 8 - 7")); ASSERT_EQUALS("X && 0", testConstFold("X && 0")); ASSERT_EQUALS("X >= 0 || 0 < Y", testConstFold("X >= 0 || 0 < Y")); + ASSERT_EQUALS("X && 1 && Z", testConstFold("X && (1 || Y) && Z")); } #ifdef __CYGWIN__ From a048dc06f6ddea840874da6e7b7e4e18030b9129 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 10 Feb 2025 00:50:26 +0000 Subject: [PATCH 21/24] Nits --- simplecpp.cpp | 8 +++++--- test.cpp | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index f541f1f5..1a4c555c 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1296,6 +1296,8 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set deleteToken(tok1->*step); } else break; + } else if (skip) { + deleteToken(tok1->*step); } else if ((tok1->*step)->op == brackets[0]) { ++skip; deleteToken(tok1->*step); @@ -1314,10 +1316,10 @@ static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool simplecpp::Token* simplecpp::Token::* const step = forwardDirection ? &simplecpp::Token::next : &simplecpp::Token::previous; const char * const brackets = forwardDirection ? "()" : ")("; int bracket = 0; - bool ret = false; + bool found = false; for (; tok; tok = tok->*step) { if (tok->number && (stringToLL(tok->str()) == value || any)) { - ret = true; + found = true; continue; } if (tok->op == brackets[0]) { @@ -1333,7 +1335,7 @@ static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool } return false; } - return ret; + return found; } static const std::string AND("and"); diff --git a/test.cpp b/test.cpp index a86d963f..79253bbf 100644 --- a/test.cpp +++ b/test.cpp @@ -459,6 +459,7 @@ static void constFold() ASSERT_EQUALS("X && 0", testConstFold("X && 0")); ASSERT_EQUALS("X >= 0 || 0 < Y", testConstFold("X >= 0 || 0 < Y")); ASSERT_EQUALS("X && 1 && Z", testConstFold("X && (1 || Y) && Z")); + ASSERT_EQUALS("X > 0 && Y", testConstFold("X > 0 && Y")); } #ifdef __CYGWIN__ From ae28f7483946f357e7af3adce22d5eea18c8bdbb Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 10 Feb 2025 10:50:49 +0000 Subject: [PATCH 22/24] Nits --- simplecpp.cpp | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 1a4c555c..0fba8ac1 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1296,11 +1296,11 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set deleteToken(tok1->*step); } else break; - } else if (skip) { - deleteToken(tok1->*step); } else if ((tok1->*step)->op == brackets[0]) { ++skip; deleteToken(tok1->*step); + } else if (skip) { + deleteToken(tok1->*step); } else if (breakPoints.count((tok1->*step)->str()) != 0) { deleteToken(tok1->*step); break; @@ -1311,31 +1311,11 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set simpleSquash(tok, result); } -static bool checkSideForSingleInt(simplecpp::Token * tok, long long value, bool forwardDirection, bool any = false) +static simplecpp::Token * constFoldGetOperand(simplecpp::Token * tok, bool forwardDirection) { simplecpp::Token* simplecpp::Token::* const step = forwardDirection ? &simplecpp::Token::next : &simplecpp::Token::previous; - const char * const brackets = forwardDirection ? "()" : ")("; - int bracket = 0; - bool found = false; - for (; tok; tok = tok->*step) { - if (tok->number && (stringToLL(tok->str()) == value || any)) { - found = true; - continue; - } - if (tok->op == brackets[0]) { - ++bracket; - continue; - } - if (tok->op == brackets[1]) { - if (bracket) { - --bracket; - continue; - } - break; - } - return false; - } - return found; + const char bracket = forwardDirection ? ')' : '('; + return tok->*step && (tok->*step)->number && (!((tok->*step)->*step) || (((tok->*step)->*step)->op == bracket)) ? tok->*step : nullptr; } static const std::string AND("and"); @@ -1351,22 +1331,22 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) } if (tok->str() != "&&" && tok->str() != "||") continue; - if (!tok->previous || !tok->next) - continue; - if (!tok->previous->number && !tok->next->number) + const Token* lhs = constFoldGetOperand(tok, false); + const Token* rhs = constFoldGetOperand(tok, true); + if (!lhs) // if lhs is NaN we don't need fold as it will be evalueted continue; std::set breakPoints; breakPoints.insert(":"); breakPoints.insert("?"); if (tok->str() == "||"){ - if (checkSideForSingleInt(tok->previous, 1LL, false) || (checkSideForSingleInt(tok->next, 1LL, true) && checkSideForSingleInt(tok->previous, 1LL, false, true))) - squashTokens(tok, breakPoints, checkSideForSingleInt(tok->previous, 1LL, false), toString(1)); + if (stringToLL(lhs->str()) == 1LL || (rhs && stringToLL(rhs->str()) == 1LL)) + squashTokens(tok, breakPoints, stringToLL(lhs->str()) == 1LL, toString(1)); } else /*if (tok->str() == "&&")*/ { breakPoints.insert("||"); - if (checkSideForSingleInt(tok->previous, 0LL, false) || (checkSideForSingleInt(tok->next, 0LL, true) && checkSideForSingleInt(tok->previous, 0LL, false, true))) - squashTokens(tok, breakPoints, checkSideForSingleInt(tok->previous, 0LL, false), toString(0)); - else if (checkSideForSingleInt(tok->previous, 1LL, false) && checkSideForSingleInt(tok->previous, 1LL, false) == checkSideForSingleInt(tok->next, 1LL, true)) + if (stringToLL(lhs->str()) == 0LL || (rhs && stringToLL(rhs->str()) == 0LL)) + squashTokens(tok, breakPoints, stringToLL(lhs->str()) == 0LL, toString(0)); + else if (rhs && stringToLL(lhs->str()) && stringToLL(rhs->str())) simpleSquash(tok, "1"); } } From 566db5b404e387292144652b3d22bd7c8073881f Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 10 Feb 2025 11:05:21 +0000 Subject: [PATCH 23/24] Ci --- simplecpp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 0fba8ac1..ed3b10ef 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1331,8 +1331,8 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) } if (tok->str() != "&&" && tok->str() != "||") continue; - const Token* lhs = constFoldGetOperand(tok, false); - const Token* rhs = constFoldGetOperand(tok, true); + const Token* const lhs = constFoldGetOperand(tok, false); + const Token* const rhs = constFoldGetOperand(tok, true); if (!lhs) // if lhs is NaN we don't need fold as it will be evalueted continue; From 1dee5df066f33728e7e64de4cdab366706b4cf54 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 10 Feb 2025 11:24:22 +0000 Subject: [PATCH 24/24] Nits --- simplecpp.cpp | 7 +++---- test.cpp | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index ed3b10ef..576b0da7 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1302,7 +1302,6 @@ void simplecpp::TokenList::squashTokens(Token *&tok, const std::set } else if (skip) { deleteToken(tok1->*step); } else if (breakPoints.count((tok1->*step)->str()) != 0) { - deleteToken(tok1->*step); break; } else { deleteToken(tok1->*step); @@ -1333,15 +1332,15 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) continue; const Token* const lhs = constFoldGetOperand(tok, false); const Token* const rhs = constFoldGetOperand(tok, true); - if (!lhs) // if lhs is NaN we don't need fold as it will be evalueted + if (!lhs) // if lhs is not a single number we don't need to fold continue; std::set breakPoints; breakPoints.insert(":"); breakPoints.insert("?"); if (tok->str() == "||"){ - if (stringToLL(lhs->str()) == 1LL || (rhs && stringToLL(rhs->str()) == 1LL)) - squashTokens(tok, breakPoints, stringToLL(lhs->str()) == 1LL, toString(1)); + if (stringToLL(lhs->str()) != 0LL || (rhs && stringToLL(rhs->str()) != 0LL)) + squashTokens(tok, breakPoints, stringToLL(lhs->str()) != 0LL, toString(1)); } else /*if (tok->str() == "&&")*/ { breakPoints.insert("||"); if (stringToLL(lhs->str()) == 0LL || (rhs && stringToLL(rhs->str()) == 0LL)) diff --git a/test.cpp b/test.cpp index 79253bbf..622c9b90 100644 --- a/test.cpp +++ b/test.cpp @@ -459,6 +459,7 @@ static void constFold() ASSERT_EQUALS("X && 0", testConstFold("X && 0")); ASSERT_EQUALS("X >= 0 || 0 < Y", testConstFold("X >= 0 || 0 < Y")); ASSERT_EQUALS("X && 1 && Z", testConstFold("X && (1 || Y) && Z")); + ASSERT_EQUALS("0 || Y", testConstFold("0 && X || Y")); ASSERT_EQUALS("X > 0 && Y", testConstFold("X > 0 && Y")); }