Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 50 additions & 54 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,33 +678,55 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,

if (oldLastToken != cback()) {
oldLastToken = cback();
if (!isLastLinePreprocessor())
const Token * const llTok = isLastLinePreprocessor();
if (!llTok)
continue;
const std::string lastline(lastLine());
if (lastline == "# file %str%") {
const Token *strtok = cback();
while (strtok->comment)
strtok = strtok->previous;
loc.push(location);
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
location.line = 1U;
} else if (lastline == "# line %num%") {
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%") {
const Token *strtok = cback();
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);
const Token * const llNextToken = llTok->next;
if (!llTok->next)
continue;
if (llNextToken->next) {
// #file "file.c"
if (llNextToken->str() == "file" &&
llNextToken->next->str()[0] == '\"')
{
const Token *strtok = cback();
while (strtok->comment)
strtok = strtok->previous;
loc.push(location);
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
location.line = 1U;
}
// #3 "file.c"
// #line 3 "file.c"
else if ((llNextToken->number &&
llNextToken->next->str()[0] == '\"') ||
(llNextToken->str() == "line" &&
llNextToken->next->number &&
llNextToken->next->next &&
llNextToken->next->next->str()[0] == '\"'))
{
const Token *strtok = cback();
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);
}
// #line 3
else if (llNextToken->str() == "line" &&
llNextToken->next->number)
{
const Token *numtok = cback();
while (numtok->comment)
numtok = numtok->previous;
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
}
}
// #endfile
else if (lastline == "# endfile" && !loc.empty()) {
else if (llNextToken->str() == "endfile" && !loc.empty())
{
location = loc.top();
loc.pop();
}
Expand Down Expand Up @@ -1398,34 +1420,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
return ret;
}

std::string simplecpp::TokenList::lastLine(int maxsize) const
{
std::string ret;
int count = 0;
for (const Token *tok = cback(); ; tok = tok->previous) {
if (!sameline(tok, cback())) {
break;
}
if (tok->comment)
continue;
if (++count > maxsize)
return "";
if (!ret.empty())
ret += ' ';
// add tokens in reverse for performance reasons
if (tok->str()[0] == '\"')
ret += "%rts%"; // %str%
else if (tok->number)
ret += "%mun%"; // %num%
else {
ret += tok->str();
std::reverse(ret.end() - tok->str().length(), ret.end());
}
}
std::reverse(ret.begin(), ret.end());
return ret;
}

const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
{
const Token* prevTok = nullptr;
Expand All @@ -1442,10 +1436,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
return prevTok;
}

bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
{
const Token * const prevTok = lastLineTok(maxsize);
return prevTok && prevTok->op == '#';
if (prevTok && prevTok->op == '#')
return prevTok;
return nullptr;
}

unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)
Expand Down
3 changes: 1 addition & 2 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,8 @@ namespace simplecpp {
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);

std::string lastLine(int maxsize=1000) const;
const Token* lastLineTok(int maxsize=1000) const;
bool isLastLinePreprocessor(int maxsize=1000) const;
const Token* isLastLinePreprocessor(int maxsize=1000) const;

unsigned int fileIndex(const std::string &filename);

Expand Down