Skip to content

Commit c5b816b

Browse files
committed
optimized handling of file and line preprocessor directives [skip ci]
1 parent f247a78 commit c5b816b

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

simplecpp.cpp

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -685,22 +685,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
685685

686686
if (oldLastToken != cback()) {
687687
oldLastToken = cback();
688-
if (!isLastLinePreprocessor())
688+
const Token * const llTok = isLastLinePreprocessor();
689+
if (!llTok)
689690
continue;
690-
const std::string lastline(lastLine());
691-
if (lastline == "# file %str%") {
691+
const Token * const llNextToken = llTok->next;
692+
if (!llTok->next)
693+
continue;
694+
// #file "file.c"
695+
if (llNextToken->str() == "file" &&
696+
llNextToken->next &&
697+
llNextToken->next->str()[0] == '\"')
698+
{
692699
const Token *strtok = cback();
693700
while (strtok->comment)
694701
strtok = strtok->previous;
695702
loc.push(location);
696703
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
697704
location.line = 1U;
698-
} else if (lastline == "# line %num%") {
699-
const Token *numtok = cback();
700-
while (numtok->comment)
701-
numtok = numtok->previous;
702-
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
703-
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
705+
}
706+
// #3 "file.c"
707+
// #line 3 "file.c"
708+
else if ((llNextToken->number &&
709+
llNextToken->next &&
710+
llNextToken->next->str()[0] == '\"') ||
711+
(llNextToken->str() == "line" &&
712+
llNextToken->next &&
713+
llNextToken->next->number &&
714+
llNextToken->next->next &&
715+
llNextToken->next->next->str()[0] == '\"'))
716+
{
704717
const Token *strtok = cback();
705718
while (strtok->comment)
706719
strtok = strtok->previous;
@@ -710,8 +723,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
710723
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
711724
std::atol(numtok->str().c_str()), &location);
712725
}
726+
// #line 3
727+
else if (llNextToken->str() == "line" &&
728+
llNextToken->next &&
729+
llNextToken->next->number)
730+
{
731+
const Token *numtok = cback();
732+
while (numtok->comment)
733+
numtok = numtok->previous;
734+
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
735+
}
713736
// #endfile
714-
else if (lastline == "# endfile" && !loc.empty()) {
737+
else if (llNextToken->str() == "endfile" && !loc.empty())
738+
{
715739
location = loc.top();
716740
loc.pop();
717741
}
@@ -1405,34 +1429,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14051429
return ret;
14061430
}
14071431

1408-
std::string simplecpp::TokenList::lastLine(int maxsize) const
1409-
{
1410-
std::string ret;
1411-
int count = 0;
1412-
for (const Token *tok = cback(); ; tok = tok->previous) {
1413-
if (!sameline(tok, cback())) {
1414-
break;
1415-
}
1416-
if (tok->comment)
1417-
continue;
1418-
if (++count > maxsize)
1419-
return "";
1420-
if (!ret.empty())
1421-
ret += ' ';
1422-
// add tokens in reverse for performance reasons
1423-
if (tok->str()[0] == '\"')
1424-
ret += "%rts%"; // %str%
1425-
else if (tok->number)
1426-
ret += "%mun%"; // %num%
1427-
else {
1428-
ret += tok->str();
1429-
std::reverse(ret.end() - tok->str().length(), ret.end());
1430-
}
1431-
}
1432-
std::reverse(ret.begin(), ret.end());
1433-
return ret;
1434-
}
1435-
14361432
const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14371433
{
14381434
const Token* prevTok = nullptr;
@@ -1449,10 +1445,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14491445
return prevTok;
14501446
}
14511447

1452-
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1448+
const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
14531449
{
14541450
const Token * const prevTok = lastLineTok(maxsize);
1455-
return prevTok && prevTok->op == '#';
1451+
if (prevTok && prevTok->op == '#')
1452+
return prevTok;
1453+
return nullptr;
14561454
}
14571455

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

simplecpp.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,8 @@ namespace simplecpp {
309309
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
310310
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
311311

312-
std::string lastLine(int maxsize=1000) const;
313312
const Token* lastLineTok(int maxsize=1000) const;
314-
bool isLastLinePreprocessor(int maxsize=1000) const;
313+
const Token* isLastLinePreprocessor(int maxsize=1000) const;
315314

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

0 commit comments

Comments
 (0)