Skip to content

Commit 1ab1174

Browse files
committed
test
1 parent b0589c8 commit 1ab1174

File tree

2 files changed

+34
-66
lines changed

2 files changed

+34
-66
lines changed

externals/simplecpp/simplecpp.cpp

Lines changed: 34 additions & 64 deletions
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

externals/simplecpp/simplecpp.h

Lines changed: 0 additions & 2 deletions
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

0 commit comments

Comments
 (0)