bump simplecpp

This commit is contained in:
Daniel Marjamäki 2021-12-16 21:18:05 +01:00
parent 0e1590b43c
commit c02dd5bf23
2 changed files with 37 additions and 13 deletions

View File

@ -501,14 +501,26 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
oldLastToken = cback(); oldLastToken = cback();
const std::string lastline(lastLine()); const std::string lastline(lastLine());
if (lastline == "# file %str%") { if (lastline == "# file %str%") {
const Token *strtok = cback();
while (strtok->comment)
strtok = strtok->previous;
loc.push(location); loc.push(location);
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U)); location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
location.line = 1U; location.line = 1U;
} else if (lastline == "# line %num%") { } else if (lastline == "# line %num%") {
lineDirective(location.fileIndex, std::atol(cback()->str().c_str()), &location); const Token *numtok = cback();
while (numtok->comment)
numtok = numtok->previous;
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") { } else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
lineDirective(fileIndex(replaceAll(cback()->str().substr(1U, cback()->str().size() - 2U),"\\\\","\\")), const Token *strtok = cback();
std::atol(cback()->previous->str().c_str()), &location); while (strtok->comment)
strtok = strtok->previous;
const Token *numtok = strtok->previous;
while (numtok->comment)
numtok = numtok->previous;
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
std::atol(numtok->str().c_str()), &location);
} }
// #endfile // #endfile
else if (lastline == "# endfile" && !loc.empty()) { else if (lastline == "# endfile" && !loc.empty()) {
@ -1393,10 +1405,12 @@ namespace simplecpp {
}; };
private: private:
/** Create new token where Token::macro is set for replaced tokens */ /** Create new token where Token::macro is set for replaced tokens */
Token *newMacroToken(const TokenString &str, const Location &loc, bool replaced) const { Token *newMacroToken(const TokenString &str, const Location &loc, bool replaced, const Token *expandedFromToken=NULL) const {
Token *tok = new Token(str,loc); Token *tok = new Token(str,loc);
if (replaced) if (replaced)
tok->macro = nameTokDef->str(); tok->macro = nameTokDef->str();
if (expandedFromToken)
tok->setExpandedFrom(expandedFromToken, this);
return tok; return tok;
} }
@ -1614,12 +1628,12 @@ namespace simplecpp {
throw invalidHashHash(tok->location, name()); throw invalidHashHash(tok->location, name());
TokenList new_output(files); TokenList new_output(files);
if (!expandArg(&new_output, tok, parametertokens2)) if (!expandArg(&new_output, tok, parametertokens2))
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros))); output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));
else if (new_output.empty()) // placemarker token else if (new_output.empty()) // placemarker token
output->push_back(newMacroToken("", loc, isReplaced(expandedmacros))); output->push_back(newMacroToken("", loc, isReplaced(expandedmacros)));
else else
for (const Token *tok2 = new_output.cfront(); tok2; tok2 = tok2->next) for (const Token *tok2 = new_output.cfront(); tok2; tok2 = tok2->next)
output->push_back(newMacroToken(tok2->str(), loc, isReplaced(expandedmacros))); output->push_back(newMacroToken(tok2->str(), loc, isReplaced(expandedmacros), tok2));
tok = tok->next; tok = tok->next;
} else { } else {
tok = expandToken(output, loc, tok, macros, expandedmacros, parametertokens2); tok = expandToken(output, loc, tok, macros, expandedmacros, parametertokens2);
@ -1710,7 +1724,7 @@ namespace simplecpp {
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) {
output->push_back(newMacroToken(tok->str(), loc, true)); output->push_back(newMacroToken(tok->str(), loc, true, tok));
return tok->next; return tok->next;
} }
@ -1734,14 +1748,14 @@ namespace simplecpp {
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros2, parametertokens); 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, tok));
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, loc, 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, tok));
return tok->next; return tok->next;
} }
TokenList temp(files); TokenList temp(files);
@ -1779,7 +1793,7 @@ namespace simplecpp {
} }
} }
output->push_back(newMacroToken(tok->str(), loc, true)); output->push_back(newMacroToken(tok->str(), loc, true, tok));
return tok->next; return tok->next;
} }
@ -1811,10 +1825,10 @@ namespace simplecpp {
return true; return true;
for (const Token *partok = parametertokens[argnr]->next; partok != parametertokens[argnr + 1U];) { for (const Token *partok = parametertokens[argnr]->next; partok != parametertokens[argnr + 1U];) {
const std::map<TokenString, Macro>::const_iterator it = macros.find(partok->str()); const std::map<TokenString, Macro>::const_iterator it = macros.find(partok->str());
if (it != macros.end() && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end()))
partok = it->second.expand(output, loc, partok, macros, expandedmacros); partok = it->second.expand(output, loc, partok, macros, expandedmacros);
else { else {
output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros))); output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros), partok));
output->back()->macro = partok->macro; output->back()->macro = partok->macro;
partok = partok->next; partok = partok->next;
} }

View File

@ -149,11 +149,21 @@ namespace simplecpp {
return tok; return tok;
} }
void setExpandedFrom(const Token *tok, const void* m) {
mExpandedFrom = tok->mExpandedFrom;
mExpandedFrom.insert(m);
}
bool isExpandedFrom(const void* m) const {
return mExpandedFrom.find(m) != mExpandedFrom.end();
}
void printAll() const; void printAll() const;
void printOut() const; void printOut() const;
private: private:
TokenString string; TokenString string;
std::set<const void*> mExpandedFrom;
// Not implemented - prevent assignment // Not implemented - prevent assignment
Token &operator=(const Token &tok); Token &operator=(const Token &tok);
}; };