From b6246ecc55f56cb3883f2b9d8f1144d2ed322c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 31 Jul 2016 12:10:30 +0200 Subject: [PATCH] bump simplecpp and update Preprocessor to handle new simplecpp::Output enum constants --- externals/simplecpp/simplecpp.cpp | 43 +++++++++++++++++++++++-------- externals/simplecpp/simplecpp.h | 4 ++- lib/preprocessor.cpp | 10 +++++-- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/externals/simplecpp/simplecpp.cpp b/externals/simplecpp/simplecpp.cpp index 7845cebfe..c3c55b5d1 100644 --- a/externals/simplecpp/simplecpp.cpp +++ b/externals/simplecpp/simplecpp.cpp @@ -989,14 +989,14 @@ private: return ~0U; } - std::vector getMacroParameters(const Token *nameToken, bool def) const { + std::vector getMacroParameters(const Token *nameToken, bool calledInDefine) const { if (!nameToken->next || nameToken->next->op != '(' || !functionLike()) return std::vector(); std::vector parametertokens; parametertokens.push_back(nameToken->next); unsigned int par = 0U; - for (const Token *tok = nameToken->next->next; def ? sameline(tok,nameToken) : (tok != NULL); tok = tok->next) { + for (const Token *tok = nameToken->next->next; calledInDefine ? sameline(tok,nameToken) : (tok != NULL); tok = tok->next) { if (tok->op == '(') ++par; else if (tok->op == ')') { @@ -1056,7 +1056,10 @@ private: return nameToken->next; } - std::vector parametertokens1(getMacroParameters(nameToken, !expandedmacros1.empty())); + const bool calledInDefine = (loc.fileIndex != nameToken->location.fileIndex || + loc.line < nameToken->location.line); + + std::vector parametertokens1(getMacroParameters(nameToken, calledInDefine)); if (functionLike()) { // No arguments => not macro expansion @@ -1639,20 +1642,38 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL const bool systemheader = (rawtok->next->str[0] == '<'); const std::string header(rawtok->next->str.substr(1U, rawtok->next->str.size() - 2U)); const std::string header2 = getFileName(filedata, rawtok->location.file(), header, dui, systemheader); - if (!header2.empty() && pragmaOnce.find(header2) == pragmaOnce.end()) { - includetokenstack.push(gotoNextLine(rawtok)); - const TokenList *includetokens = filedata.find(header2)->second; - rawtok = includetokens ? includetokens->cfront() : 0; - continue; - } else { + if (header2.empty()) { simplecpp::Output output(files); - output.type = Output::MISSING_INCLUDE; + output.type = Output::MISSING_HEADER; output.location = rawtok->location; output.msg = "Header not found: " + rawtok->next->str; if (outputList) outputList->push_back(output); + } else if (includetokenstack.size() >= 400) { + simplecpp::Output out(files); + out.type = Output::INCLUDE_NESTED_TOO_DEEPLY; + out.location = rawtok->location; + out.msg = "#include nested too deeply"; + if (outputList) + outputList->push_back(out); + } else if (pragmaOnce.find(header2) == pragmaOnce.end()) { + includetokenstack.push(gotoNextLine(rawtok)); + const TokenList *includetokens = filedata.find(header2)->second; + rawtok = includetokens ? includetokens->cfront() : 0; + continue; } } else if (rawtok->str == IF || rawtok->str == IFDEF || rawtok->str == IFNDEF || rawtok->str == ELIF) { + if (!sameline(rawtok,rawtok->next)) { + simplecpp::Output out(files); + out.type = Output::SYNTAX_ERROR; + out.location = rawtok->location; + out.msg = "Syntax error in #" + rawtok->str; + if (outputList) + outputList->push_back(out); + output.clear(); + return; + } + bool conditionIsTrue; if (ifstates.top() == ALWAYS_FALSE || (ifstates.top() == ELSE_IS_TRUE && rawtok->str != ELIF)) conditionIsTrue = false; @@ -1691,7 +1712,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL it->second.expand(&value, tok, macros, files); } catch (Macro::Error &err) { Output out(rawtok->location.files); - out.type = Output::ERROR; + out.type = Output::SYNTAX_ERROR; out.location = err.location; out.msg = "failed to expand \'" + tok->str + "\', " + err.what; if (outputList) diff --git a/externals/simplecpp/simplecpp.h b/externals/simplecpp/simplecpp.h index d283c0c15..3fc1ba6c5 100644 --- a/externals/simplecpp/simplecpp.h +++ b/externals/simplecpp/simplecpp.h @@ -156,7 +156,9 @@ struct SIMPLECPP_LIB Output { enum Type { ERROR, /* #error */ WARNING, /* #warning */ - MISSING_INCLUDE + MISSING_HEADER, + INCLUDE_NESTED_TOO_DEEPLY, + SYNTAX_ERROR } type; Location location; std::string msg; diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 095ab627a..23fa41bcc 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -496,13 +496,17 @@ std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std return ""; case simplecpp::Output::WARNING: break; - case simplecpp::Output::MISSING_INCLUDE: { + case simplecpp::Output::MISSING_HEADER: { const std::string::size_type pos1 = it->msg.find_first_of("<\""); const std::string::size_type pos2 = it->msg.find_first_of(">\"", pos1 + 1U); if (pos1 < pos2 && pos2 != std::string::npos) missingInclude(it->location.file(), it->location.line, it->msg.substr(pos1+1, pos2-pos1-1), it->msg[pos1] == '\"' ? UserHeader : SystemHeader); } break; + case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY: + case simplecpp::Output::SYNTAX_ERROR: + error(it->location.file(), it->location.line, it->msg); + return ""; }; } @@ -620,11 +624,13 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string for (simplecpp::OutputList::const_iterator it = outputList.begin(); it != outputList.end(); ++it) { switch (it->type) { case simplecpp::Output::ERROR: + case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY: + case simplecpp::Output::SYNTAX_ERROR: error(it->location.file(), it->location.line, it->msg); return ""; case simplecpp::Output::WARNING: break; - case simplecpp::Output::MISSING_INCLUDE: { + case simplecpp::Output::MISSING_HEADER: { const std::string::size_type pos1 = it->msg.find_first_of("<\""); const std::string::size_type pos2 = it->msg.find_first_of(">\"", pos1 + 1U); if (pos1 < pos2 && pos2 != std::string::npos)