Skip to content

Commit b0589c8

Browse files
committed
bump simplecpp to 1.3.1
1 parent 257fd5d commit b0589c8

File tree

2 files changed

+69
-36
lines changed

2 files changed

+69
-36
lines changed

externals/simplecpp/simplecpp.cpp

Lines changed: 67 additions & 36 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->op != '(')
950+
if (!tok || tok->op != '(')
951951
break;
952952

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

1160-
tok = tok->previous;
1161-
tok->setstr(toString(result));
1162-
deleteToken(tok->next);
1163-
deleteToken(tok->next);
1160+
simpleSquash(tok, toString(result));
11641161
}
11651162
}
11661163

@@ -1180,10 +1177,7 @@ void simplecpp::TokenList::constFoldAddSub(Token *tok)
11801177
else
11811178
continue;
11821179

1183-
tok = tok->previous;
1184-
tok->setstr(toString(result));
1185-
deleteToken(tok->next);
1186-
deleteToken(tok->next);
1180+
simpleSquash(tok, toString(result));
11871181
}
11881182
}
11891183

@@ -1203,10 +1197,7 @@ void simplecpp::TokenList::constFoldShift(Token *tok)
12031197
else
12041198
continue;
12051199

1206-
tok = tok->previous;
1207-
tok->setstr(toString(result));
1208-
deleteToken(tok->next);
1209-
deleteToken(tok->next);
1200+
simpleSquash(tok, toString(result));
12101201
}
12111202
}
12121203

@@ -1240,10 +1231,7 @@ void simplecpp::TokenList::constFoldComparison(Token *tok)
12401231
else
12411232
continue;
12421233

1243-
tok = tok->previous;
1244-
tok->setstr(toString(result));
1245-
deleteToken(tok->next);
1246-
deleteToken(tok->next);
1234+
simpleSquash(tok, toString(result));
12471235
}
12481236
}
12491237

@@ -1275,12 +1263,51 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
12751263
result = (stringToLL(tok->previous->str()) ^ stringToLL(tok->next->str()));
12761264
else /*if (*op == '|')*/
12771265
result = (stringToLL(tok->previous->str()) | stringToLL(tok->next->str()));
1278-
tok = tok->previous;
1279-
tok->setstr(toString(result));
1280-
deleteToken(tok->next);
1281-
deleteToken(tok->next);
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);
12821301
}
12831302
}
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;
12841311
}
12851312

12861313
static const std::string AND("and");
@@ -1296,21 +1323,24 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok)
12961323
}
12971324
if (tok->str() != "&&" && tok->str() != "||")
12981325
continue;
1299-
if (!tok->previous || !tok->previous->number)
1300-
continue;
1301-
if (!tok->next || !tok->next->number)
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
13021329
continue;
13031330

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);
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+
}
13141344
}
13151345
}
13161346

@@ -2088,7 +2118,7 @@ namespace simplecpp {
20882118
if (expandArg(&temp, defToken, parametertokens))
20892119
macroName = temp.cback()->str();
20902120
if (expandArg(&temp, defToken->next->next->next, parametertokens))
2091-
macroName += temp.cback()->str();
2121+
macroName += temp.cback() ? temp.cback()->str() : "";
20922122
else
20932123
macroName += defToken->next->next->next->str();
20942124
lastToken = defToken->next->next->next;
@@ -2132,7 +2162,8 @@ namespace simplecpp {
21322162
for (const Token *partok = parametertokens[argnr]->next; partok != parametertokens[argnr + 1U];) {
21332163
const MacroMap::const_iterator it = macros.find(partok->str());
21342164
if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) {
2135-
const std::set<TokenString> expandedmacros2; // temporary amnesia to allow reexpansion of currently expanding macros during argument evaluation
2165+
std::set<TokenString> expandedmacros2(expandedmacros); // temporary amnesia to allow reexpansion of currently expanding macros during argument evaluation
2166+
expandedmacros2.erase(name());
21362167
partok = it->second.expand(output, loc, partok, macros, expandedmacros2);
21372168
} else {
21382169
output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros), partok));

externals/simplecpp/simplecpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ 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);
304306
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
305307
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
306308

0 commit comments

Comments
 (0)