Skip to content

Commit 09a8163

Browse files
authored
Revert "Fix #404: simplecpp::TokenList::constFold does not fold '( 0 ) && 10 < X' properly (#405)" (#416)
1 parent 9ce981c commit 09a8163

File tree

3 files changed

+34
-92
lines changed

3 files changed

+34
-92
lines changed

simplecpp.cpp

+34-64
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ void simplecpp::TokenList::constFold()
947947
constFoldQuestionOp(&tok);
948948

949949
// If there is no '(' we are done with the constant folding
950-
if (!tok || tok->op != '(')
950+
if (tok->op != '(')
951951
break;
952952

953953
if (!tok->next || !tok->next->next || tok->next->next->op != ')')
@@ -1157,7 +1157,10 @@ void simplecpp::TokenList::constFoldMulDivRem(Token *tok)
11571157
} else
11581158
continue;
11591159

1160-
simpleSquash(tok, toString(result));
1160+
tok = tok->previous;
1161+
tok->setstr(toString(result));
1162+
deleteToken(tok->next);
1163+
deleteToken(tok->next);
11611164
}
11621165
}
11631166

@@ -1177,7 +1180,10 @@ void simplecpp::TokenList::constFoldAddSub(Token *tok)
11771180
else
11781181
continue;
11791182

1180-
simpleSquash(tok, toString(result));
1183+
tok = tok->previous;
1184+
tok->setstr(toString(result));
1185+
deleteToken(tok->next);
1186+
deleteToken(tok->next);
11811187
}
11821188
}
11831189

@@ -1197,7 +1203,10 @@ void simplecpp::TokenList::constFoldShift(Token *tok)
11971203
else
11981204
continue;
11991205

1200-
simpleSquash(tok, toString(result));
1206+
tok = tok->previous;
1207+
tok->setstr(toString(result));
1208+
deleteToken(tok->next);
1209+
deleteToken(tok->next);
12011210
}
12021211
}
12031212

@@ -1231,7 +1240,10 @@ void simplecpp::TokenList::constFoldComparison(Token *tok)
12311240
else
12321241
continue;
12331242

1234-
simpleSquash(tok, toString(result));
1243+
tok = tok->previous;
1244+
tok->setstr(toString(result));
1245+
deleteToken(tok->next);
1246+
deleteToken(tok->next);
12351247
}
12361248
}
12371249

@@ -1263,51 +1275,12 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
12631275
result = (stringToLL(tok->previous->str()) ^ stringToLL(tok->next->str()));
12641276
else /*if (*op == '|')*/
12651277
result = (stringToLL(tok->previous->str()) | stringToLL(tok->next->str()));
1266-
simpleSquash(tok, toString(result));
1267-
}
1268-
}
1269-
}
1270-
1271-
void simplecpp::TokenList::simpleSquash(Token *&tok, const std::string & result)
1272-
{
1273-
tok = tok->previous;
1274-
tok->setstr(result);
1275-
deleteToken(tok->next);
1276-
deleteToken(tok->next);
1277-
}
1278-
1279-
void simplecpp::TokenList::squashTokens(Token *&tok, const std::set<std::string> & breakPoints, bool forwardDirection, const std::string & result)
1280-
{
1281-
const char * const brackets = forwardDirection ? "()" : ")(";
1282-
Token* Token::* const step = forwardDirection ? &Token::next : &Token::previous;
1283-
int skip = 0;
1284-
const Token * const tok1 = tok->*step;
1285-
while (tok1 && tok1->*step) {
1286-
if ((tok1->*step)->op == brackets[1]){
1287-
if (skip) {
1288-
--skip;
1289-
deleteToken(tok1->*step);
1290-
} else
1291-
break;
1292-
} else if ((tok1->*step)->op == brackets[0]) {
1293-
++skip;
1294-
deleteToken(tok1->*step);
1295-
} else if (skip) {
1296-
deleteToken(tok1->*step);
1297-
} else if (breakPoints.count((tok1->*step)->str()) != 0) {
1298-
break;
1299-
} else {
1300-
deleteToken(tok1->*step);
1278+
tok = tok->previous;
1279+
tok->setstr(toString(result));
1280+
deleteToken(tok->next);
1281+
deleteToken(tok->next);
13011282
}
13021283
}
1303-
simpleSquash(tok, result);
1304-
}
1305-
1306-
static simplecpp::Token * constFoldGetOperand(simplecpp::Token * tok, bool forwardDirection)
1307-
{
1308-
simplecpp::Token* simplecpp::Token::* const step = forwardDirection ? &simplecpp::Token::next : &simplecpp::Token::previous;
1309-
const char bracket = forwardDirection ? ')' : '(';
1310-
return tok->*step && (tok->*step)->number && (!((tok->*step)->*step) || (((tok->*step)->*step)->op == bracket)) ? tok->*step : nullptr;
13111284
}
13121285

13131286
static const std::string AND("and");
@@ -1323,24 +1296,21 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok)
13231296
}
13241297
if (tok->str() != "&&" && tok->str() != "||")
13251298
continue;
1326-
const Token* const lhs = constFoldGetOperand(tok, false);
1327-
const Token* const rhs = constFoldGetOperand(tok, true);
1328-
if (!lhs) // if lhs is not a single number we don't need to fold
1299+
if (!tok->previous || !tok->previous->number)
1300+
continue;
1301+
if (!tok->next || !tok->next->number)
13291302
continue;
13301303

1331-
std::set<std::string> breakPoints;
1332-
breakPoints.insert(":");
1333-
breakPoints.insert("?");
1334-
if (tok->str() == "||"){
1335-
if (stringToLL(lhs->str()) != 0LL || (rhs && stringToLL(rhs->str()) != 0LL))
1336-
squashTokens(tok, breakPoints, stringToLL(lhs->str()) != 0LL, toString(1));
1337-
} else /*if (tok->str() == "&&")*/ {
1338-
breakPoints.insert("||");
1339-
if (stringToLL(lhs->str()) == 0LL || (rhs && stringToLL(rhs->str()) == 0LL))
1340-
squashTokens(tok, breakPoints, stringToLL(lhs->str()) == 0LL, toString(0));
1341-
else if (rhs && stringToLL(lhs->str()) && stringToLL(rhs->str()))
1342-
simpleSquash(tok, "1");
1343-
}
1304+
int result;
1305+
if (tok->str() == "||")
1306+
result = (stringToLL(tok->previous->str()) || stringToLL(tok->next->str()));
1307+
else /*if (tok->str() == "&&")*/
1308+
result = (stringToLL(tok->previous->str()) && stringToLL(tok->next->str()));
1309+
1310+
tok = tok->previous;
1311+
tok->setstr(toString(result));
1312+
deleteToken(tok->next);
1313+
deleteToken(tok->next);
13441314
}
13451315
}
13461316

simplecpp.h

-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ namespace simplecpp {
301301
void constFoldLogicalOp(Token *tok);
302302
void constFoldQuestionOp(Token **tok1);
303303

304-
void simpleSquash(Token *&tok, const std::string & result);
305-
void squashTokens(Token *&tok, const std::set<std::string> & breakPoints, bool forwardDirection, const std::string & result);
306304
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
307305
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
308306

test.cpp

-26
Original file line numberDiff line numberDiff line change
@@ -452,15 +452,6 @@ static void constFold()
452452
ASSERT_EQUALS("1", testConstFold("010==8"));
453453
ASSERT_EQUALS("exception", testConstFold("!1 ? 2 :"));
454454
ASSERT_EQUALS("exception", testConstFold("?2:3"));
455-
ASSERT_EQUALS("0", testConstFold("( 0 ) && 10 < X"));
456-
ASSERT_EQUALS("0", testConstFold("1+2*(3+4) && 7 - 7"));
457-
ASSERT_EQUALS("1", testConstFold("( 1 ) || 10 < X"));
458-
ASSERT_EQUALS("1", testConstFold("1+2*(3+4) || 8 - 7"));
459-
ASSERT_EQUALS("X && 0", testConstFold("X && 0"));
460-
ASSERT_EQUALS("X >= 0 || 0 < Y", testConstFold("X >= 0 || 0 < Y"));
461-
ASSERT_EQUALS("X && 1 && Z", testConstFold("X && (1 || Y) && Z"));
462-
ASSERT_EQUALS("0 || Y", testConstFold("0 && X || Y"));
463-
ASSERT_EQUALS("X > 0 && Y", testConstFold("X > 0 && Y"));
464455
}
465456

466457
#ifdef __CYGWIN__
@@ -1617,22 +1608,6 @@ static void ifA()
16171608
ASSERT_EQUALS("\nX", preprocess(code, dui));
16181609
}
16191610

1620-
static void ifXorY()
1621-
{
1622-
const char code[] = "#if Z > 0 || 0 < Y\n"
1623-
"X\n"
1624-
"#endif";
1625-
ASSERT_EQUALS("", preprocess(code));
1626-
1627-
simplecpp::DUI dui;
1628-
dui.defines.push_back("Z=1");
1629-
ASSERT_EQUALS("\nX", preprocess(code, dui));
1630-
1631-
dui.defines.clear();
1632-
dui.defines.push_back("Y=15");
1633-
ASSERT_EQUALS("\nX", preprocess(code, dui));
1634-
}
1635-
16361611
static void ifCharLiteral()
16371612
{
16381613
const char code[] = "#if ('A'==0x41)\n"
@@ -3151,7 +3126,6 @@ int main(int argc, char **argv)
31513126
TEST_CASE(ifdef2);
31523127
TEST_CASE(ifndef);
31533128
TEST_CASE(ifA);
3154-
TEST_CASE(ifXorY);
31553129
TEST_CASE(ifCharLiteral);
31563130
TEST_CASE(ifDefined);
31573131
TEST_CASE(ifDefinedNoPar);

0 commit comments

Comments
 (0)