Bump simplecpp

This commit is contained in:
Daniel Marjamäki 2020-07-02 18:18:30 +02:00
parent 25055cec62
commit eec622b515
1 changed files with 67 additions and 52 deletions

View File

@ -1026,15 +1026,15 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
{ {
Token * const tok1 = tok; Token * const tok1 = tok;
for (const char *op = "&^|"; *op; op++) { for (const char *op = "&^|"; *op; op++) {
const std::string* altop; const std::string* alternativeOp;
if (*op == '&') if (*op == '&')
altop = &BITAND; alternativeOp = &BITAND;
else if (*op == '|') else if (*op == '|')
altop = &BITOR; alternativeOp = &BITOR;
else else
altop = &XOR; alternativeOp = &XOR;
for (tok = tok1; tok && tok->op != ')'; tok = tok->next) { for (tok = tok1; tok && tok->op != ')'; tok = tok->next) {
if (tok->op != *op && !isAlternativeBinaryOp(tok, *altop)) if (tok->op != *op && !isAlternativeBinaryOp(tok, *alternativeOp))
continue; continue;
if (!tok->previous || !tok->previous->number) if (!tok->previous || !tok->previous->number)
continue; continue;
@ -1472,6 +1472,7 @@ namespace simplecpp {
} }
const Token *appendTokens(TokenList *tokens, const Token *appendTokens(TokenList *tokens,
const Location &rawloc,
const Token *lpar, const Token *lpar,
const std::map<TokenString,Macro> &macros, const std::map<TokenString,Macro> &macros,
const std::set<TokenString> &expandedmacros, const std::set<TokenString> &expandedmacros,
@ -1483,17 +1484,17 @@ namespace simplecpp {
while (sameline(lpar, tok)) { while (sameline(lpar, tok)) {
if (tok->op == '#' && sameline(tok,tok->next) && tok->next->op == '#' && sameline(tok,tok->next->next)) { if (tok->op == '#' && sameline(tok,tok->next) && tok->next->op == '#' && sameline(tok,tok->next->next)) {
// A##B => AB // A##B => AB
tok = expandHashHash(tokens, tok->location, tok, macros, expandedmacros, parametertokens); tok = expandHashHash(tokens, rawloc, tok, macros, expandedmacros, parametertokens);
} else if (tok->op == '#' && sameline(tok, tok->next) && tok->next->op != '#') { } else if (tok->op == '#' && sameline(tok, tok->next) && tok->next->op != '#') {
tok = expandHash(tokens, tok->location, tok, macros, expandedmacros, parametertokens); tok = expandHash(tokens, rawloc, tok, macros, expandedmacros, parametertokens);
} else { } else {
if (!expandArg(tokens, tok, tok->location, macros, expandedmacros, parametertokens)) { if (!expandArg(tokens, tok, rawloc, macros, expandedmacros, parametertokens)) {
bool expanded = false; bool expanded = false;
const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str()); const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str());
if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) { if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) {
const Macro &m = it->second; const Macro &m = it->second;
if (!m.functionLike()) { if (!m.functionLike()) {
m.expand(tokens, tok->location, tok, macros, expandedmacros); m.expand(tokens, rawloc, tok, macros, expandedmacros);
expanded = true; expanded = true;
} }
} }
@ -1612,13 +1613,19 @@ namespace simplecpp {
hashToken = hashToken->next; hashToken = hashToken->next;
++numberOfHash; ++numberOfHash;
} }
if (numberOfHash == 4) { if (numberOfHash == 4 && tok->next->location.col + 1 == tok->next->next->location.col) {
// # ## # => ## // # ## # => ##
output->push_back(newMacroToken("##", loc, isReplaced(expandedmacros))); output->push_back(newMacroToken("##", loc, isReplaced(expandedmacros)));
tok = hashToken; tok = hashToken;
continue; continue;
} }
if (numberOfHash >= 2 && tok->location.col + 1 < tok->next->location.col) {
output->push_back(new Token(*tok));
tok = tok->next;
continue;
}
tok = tok->next; tok = tok->next;
if (tok == endToken) { if (tok == endToken) {
output->push_back(new Token(*tok->previous)); output->push_back(new Token(*tok->previous));
@ -1645,6 +1652,41 @@ namespace simplecpp {
return functionLike() ? parametertokens2.back()->next : nameTokInst->next; return functionLike() ? parametertokens2.back()->next : nameTokInst->next;
} }
const Token *recursiveExpandToken(TokenList *output, TokenList &temp, const Location &loc, const Token *tok, const std::map<TokenString,Macro> &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
if (!(temp.cback() && temp.cback()->name && tok->next && tok->next->op == '(')) {
output->takeTokens(temp);
return tok->next;
}
if (!sameline(tok, tok->next)) {
output->takeTokens(temp);
return tok->next;
}
const std::map<TokenString, Macro>::const_iterator it = macros.find(temp.cback()->str());
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end()) {
output->takeTokens(temp);
return tok->next;
}
const Macro &calledMacro = it->second;
if (!calledMacro.functionLike()) {
output->takeTokens(temp);
return tok->next;
}
TokenList temp2(files);
temp2.push_back(new Token(temp.cback()->str(), tok->location));
const Token *tok2 = appendTokens(&temp2, loc, tok->next, macros, expandedmacros, parametertokens);
if (!tok2)
return tok->next;
output->takeTokens(temp);
output->deleteToken(output->back());
calledMacro.expand(output, loc, temp2.cfront(), macros, expandedmacros);
return tok2->next;
}
const Token *expandToken(TokenList *output, const Location &loc, const Token *tok, const std::map<TokenString,Macro> &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const { const Token *expandToken(TokenList *output, const Location &loc, const Token *tok, const std::map<TokenString,Macro> &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
// Not name.. // Not name..
if (!tok->name) { if (!tok->name) {
@ -1655,63 +1697,36 @@ namespace simplecpp {
// Macro parameter.. // Macro parameter..
{ {
TokenList temp(files); TokenList temp(files);
if (expandArg(&temp, tok, loc, macros, expandedmacros, parametertokens)) { if (expandArg(&temp, tok, loc, macros, expandedmacros, parametertokens))
if (!(temp.cback() && temp.cback()->name && tok->next && tok->next->op == '(')) { return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros, parametertokens);
output->takeTokens(temp);
return tok->next;
}
if (!sameline(tok, tok->next)) {
output->takeTokens(temp);
return tok->next;
}
const std::map<TokenString, Macro>::const_iterator it = macros.find(temp.cback()->str());
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end()) {
output->takeTokens(temp);
return tok->next;
}
const Macro &calledMacro = it->second;
if (!calledMacro.functionLike()) {
output->takeTokens(temp);
return tok->next;
}
TokenList temp2(files);
temp2.push_back(new Token(temp.cback()->str(), tok->location));
const Token *tok2 = appendTokens(&temp2, tok->next, macros, expandedmacros, parametertokens);
if (!tok2)
return tok->next;
output->takeTokens(temp);
output->deleteToken(output->back());
calledMacro.expand(output, loc, temp2.cfront(), macros, expandedmacros);
return tok2->next;
}
} }
// Macro.. // Macro..
const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str()); const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str());
if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) { if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) {
std::set<std::string> expandedmacros2(expandedmacros);
expandedmacros2.insert(tok->str());
const Macro &calledMacro = it->second; const Macro &calledMacro = it->second;
if (!calledMacro.functionLike()) if (!calledMacro.functionLike()) {
return calledMacro.expand(output, loc, tok, macros, expandedmacros); TokenList temp(files);
calledMacro.expand(&temp, loc, tok, macros, expandedmacros);
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros2, parametertokens);
}
if (!sameline(tok, tok->next) || tok->next->op != '(') { if (!sameline(tok, tok->next) || tok->next->op != '(') {
output->push_back(newMacroToken(tok->str(), loc, true)); output->push_back(newMacroToken(tok->str(), loc, true));
return tok->next; return tok->next;
} }
TokenList tokens(files); TokenList tokens(files);
tokens.push_back(new Token(*tok)); tokens.push_back(new Token(*tok));
const Token *tok2 = appendTokens(&tokens, tok->next, macros, expandedmacros, parametertokens); const Token *tok2 = appendTokens(&tokens, loc, tok->next, macros, expandedmacros, parametertokens);
if (!tok2) { if (!tok2) {
output->push_back(newMacroToken(tok->str(), loc, true)); output->push_back(newMacroToken(tok->str(), loc, true));
return tok->next; return tok->next;
} }
calledMacro.expand(output, loc, tokens.cfront(), macros, expandedmacros); TokenList temp(files);
return tok2->next; calledMacro.expand(&temp, loc, tokens.cfront(), macros, expandedmacros);
return recursiveExpandToken(output, temp, loc, tok2, macros, expandedmacros2, parametertokens);
} }
else if (tok->str() == DEFINED) { else if (tok->str() == DEFINED) {
@ -1879,7 +1894,7 @@ namespace simplecpp {
if (tokensB.empty() && sameline(B,B->next) && B->next->op=='(') { if (tokensB.empty() && sameline(B,B->next) && B->next->op=='(') {
const std::map<TokenString,Macro>::const_iterator it = macros.find(strAB); const std::map<TokenString,Macro>::const_iterator it = macros.find(strAB);
if (it != macros.end() && expandedmacros.find(strAB) == expandedmacros.end() && it->second.functionLike()) { if (it != macros.end() && expandedmacros.find(strAB) == expandedmacros.end() && it->second.functionLike()) {
const Token *tok2 = appendTokens(&tokens, B->next, macros, expandedmacros, parametertokens); const Token *tok2 = appendTokens(&tokens, loc, B->next, macros, expandedmacros, parametertokens);
if (tok2) if (tok2)
nextTok = tok2->next; nextTok = tok2->next;
} }