Skip to content

Commit 7876b81

Browse files
authored
optimized lastLine() usage in readfile() (#325)
1 parent e941a2e commit 7876b81

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

simplecpp.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -699,17 +699,20 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
699699

700700
TokenString currentToken;
701701

702-
if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#' && isLastLinePreprocessor() && (lastLine() == "# error" || lastLine() == "# warning")) {
703-
char prev = ' ';
704-
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
705-
currentToken += ch;
706-
prev = ch;
707-
ch = stream.readChar();
702+
if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#') {
703+
const Token* const llTok = lastLineTok();
704+
if (llTok && llTok->op == '#' && llTok->next && (llTok->next->str() == "error" || llTok->next->str() == "warning")) {
705+
char prev = ' ';
706+
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
707+
currentToken += ch;
708+
prev = ch;
709+
ch = stream.readChar();
710+
}
711+
stream.ungetChar();
712+
push_back(new Token(currentToken, location));
713+
location.adjust(currentToken);
714+
continue;
708715
}
709-
stream.ungetChar();
710-
push_back(new Token(currentToken, location));
711-
location.adjust(currentToken);
712-
continue;
713716
}
714717

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

844-
if (newlines > 0 && isLastLinePreprocessor() && lastLine().compare(0,9,"# define ") == 0) {
845-
multiline += newlines;
846-
location.adjust(s);
847-
} else {
848-
location.adjust(currentToken);
847+
if (newlines > 0 ) {
848+
const Token * const llTok = lastLineTok();
849+
if (llTok && llTok->op == '#' && llTok->next && llTok->next->str() == "define" && llTok->next->next) {
850+
multiline += newlines;
851+
location.adjust(s);
852+
continue;
853+
}
849854
}
855+
856+
location.adjust(currentToken);
850857
continue;
851858
}
852859

853860
else {
854861
currentToken += ch;
855862
}
856863

857-
if (*currentToken.begin() == '<' && isLastLinePreprocessor() && lastLine() == "# include") {
858-
currentToken = readUntil(stream, location, '<', '>', outputList);
859-
if (currentToken.size() < 2U)
860-
return;
864+
if (*currentToken.begin() == '<') {
865+
const Token * const llTok = lastLineTok();
866+
if (llTok && llTok->op == '#' && llTok->next && llTok->next->str() == "include") {
867+
currentToken = readUntil(stream, location, '<', '>', outputList);
868+
if (currentToken.size() < 2U)
869+
return;
870+
}
861871
}
862872

863873
push_back(new Token(currentToken, location));
@@ -1377,7 +1387,7 @@ std::string simplecpp::TokenList::lastLine(int maxsize) const
13771387
return ret;
13781388
}
13791389

1380-
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1390+
const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
13811391
{
13821392
const Token* prevTok = nullptr;
13831393
int count = 0;
@@ -1387,10 +1397,16 @@ bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
13871397
if (tok->comment)
13881398
continue;
13891399
if (++count > maxsize)
1390-
return false;
1400+
return nullptr;
13911401
prevTok = tok;
13921402
}
1393-
return prevTok && prevTok->str()[0] == '#';
1403+
return prevTok;
1404+
}
1405+
1406+
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1407+
{
1408+
const Token * const prevTok = lastLineTok(maxsize);
1409+
return prevTok && prevTok->op == '#';
13941410
}
13951411

13961412
unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)

simplecpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ namespace simplecpp {
299299
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
300300

301301
std::string lastLine(int maxsize=1000) const;
302-
bool isLastLinePreprocessor(int maxsize=100000) const;
302+
const Token* lastLineTok(int maxsize=1000) const;
303+
bool isLastLinePreprocessor(int maxsize=1000) const;
303304

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

0 commit comments

Comments
 (0)