Skip to content

Commit

Permalink
optimized lastLine() usage in readfile() (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Nov 7, 2023
1 parent e941a2e commit 7876b81
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
60 changes: 38 additions & 22 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,20 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,

TokenString currentToken;

if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#' && isLastLinePreprocessor() && (lastLine() == "# error" || lastLine() == "# warning")) {
char prev = ' ';
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
currentToken += ch;
prev = ch;
ch = stream.readChar();
if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#') {
const Token* const llTok = lastLineTok();
if (llTok && llTok->op == '#' && llTok->next && (llTok->next->str() == "error" || llTok->next->str() == "warning")) {
char prev = ' ';
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
currentToken += ch;
prev = ch;
ch = stream.readChar();
}
stream.ungetChar();
push_back(new Token(currentToken, location));
location.adjust(currentToken);
continue;
}
stream.ungetChar();
push_back(new Token(currentToken, location));
location.adjust(currentToken);
continue;
}

// number or name
Expand Down Expand Up @@ -841,23 +844,30 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
else
back()->setstr(prefix + s);

if (newlines > 0 && isLastLinePreprocessor() && lastLine().compare(0,9,"# define ") == 0) {
multiline += newlines;
location.adjust(s);
} else {
location.adjust(currentToken);
if (newlines > 0 ) {
const Token * const llTok = lastLineTok();
if (llTok && llTok->op == '#' && llTok->next && llTok->next->str() == "define" && llTok->next->next) {
multiline += newlines;
location.adjust(s);
continue;
}
}

location.adjust(currentToken);
continue;
}

else {
currentToken += ch;
}

if (*currentToken.begin() == '<' && isLastLinePreprocessor() && lastLine() == "# include") {
currentToken = readUntil(stream, location, '<', '>', outputList);
if (currentToken.size() < 2U)
return;
if (*currentToken.begin() == '<') {
const Token * const llTok = lastLineTok();
if (llTok && llTok->op == '#' && llTok->next && llTok->next->str() == "include") {
currentToken = readUntil(stream, location, '<', '>', outputList);
if (currentToken.size() < 2U)
return;
}
}

push_back(new Token(currentToken, location));
Expand Down Expand Up @@ -1377,7 +1387,7 @@ std::string simplecpp::TokenList::lastLine(int maxsize) const
return ret;
}

bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
{
const Token* prevTok = nullptr;
int count = 0;
Expand All @@ -1387,10 +1397,16 @@ bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
if (tok->comment)
continue;
if (++count > maxsize)
return false;
return nullptr;
prevTok = tok;
}
return prevTok && prevTok->str()[0] == '#';
return prevTok;
}

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

unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)
Expand Down
3 changes: 2 additions & 1 deletion simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ namespace simplecpp {
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);

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

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

Expand Down

0 comments on commit 7876b81

Please sign in to comment.