Skip to content

Commit 4190a38

Browse files
committed
optimized handling of file and line preprocessor directives
1 parent 3deeb91 commit 4190a38

2 files changed

Lines changed: 39 additions & 42 deletions

File tree

simplecpp.cpp

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

695695
if (oldLastToken != cback()) {
696696
oldLastToken = cback();
697-
if (!isLastLinePreprocessor())
697+
const Token * const llTok = isLastLinePreprocessor();
698+
if (!llTok)
698699
continue;
699-
const std::string lastline(lastLine());
700-
if (lastline == "# file %str%") {
700+
const Token * const llNextToken = llTok->next;
701+
if (!llTok->next)
702+
continue;
703+
// #file "file.c"
704+
if (llNextToken->str() == "file" &&
705+
llNextToken->next &&
706+
llNextToken->next->str()[0] == '\"')
707+
{
701708
const Token *strtok = cback();
702709
while (strtok->comment)
703710
strtok = strtok->previous;
704711
loc.push(location);
705712
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
706713
location.line = 1U;
707-
} else if (lastline == "# line %num%") {
708-
const Token *numtok = cback();
709-
while (numtok->comment)
710-
numtok = numtok->previous;
711-
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
712-
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
714+
}
715+
// #3 "file.c"
716+
// #line 3 "file.c"
717+
else if ((llNextToken->number &&
718+
llNextToken->next &&
719+
llNextToken->next->str()[0] == '\"') ||
720+
(llNextToken->str() == "line" &&
721+
llNextToken->next &&
722+
llNextToken->next->number &&
723+
llNextToken->next->next &&
724+
llNextToken->next->next->str()[0] == '\"'))
725+
{
713726
const Token *strtok = cback();
714727
while (strtok->comment)
715728
strtok = strtok->previous;
@@ -719,8 +732,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
719732
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
720733
std::atol(numtok->str().c_str()), &location);
721734
}
735+
// #line 3
736+
else if (llNextToken->str() == "line" &&
737+
llNextToken->next &&
738+
llNextToken->next->number)
739+
{
740+
const Token *numtok = cback();
741+
while (numtok->comment)
742+
numtok = numtok->previous;
743+
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
744+
}
722745
// #endfile
723-
else if (lastline == "# endfile" && !loc.empty()) {
746+
else if (llNextToken->str() == "endfile" && !loc.empty())
747+
{
724748
location = loc.top();
725749
loc.pop();
726750
}
@@ -1414,34 +1438,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14141438
return ret;
14151439
}
14161440

1417-
std::string simplecpp::TokenList::lastLine(int maxsize) const
1418-
{
1419-
std::string ret;
1420-
int count = 0;
1421-
for (const Token *tok = cback(); ; tok = tok->previous) {
1422-
if (!sameline(tok, cback())) {
1423-
break;
1424-
}
1425-
if (tok->comment)
1426-
continue;
1427-
if (++count > maxsize)
1428-
return "";
1429-
if (!ret.empty())
1430-
ret += ' ';
1431-
// add tokens in reverse for performance reasons
1432-
if (tok->str()[0] == '\"')
1433-
ret += "%rts%"; // %str%
1434-
else if (tok->number)
1435-
ret += "%mun%"; // %num%
1436-
else {
1437-
ret += tok->str();
1438-
std::reverse(ret.end() - tok->str().length(), ret.end());
1439-
}
1440-
}
1441-
std::reverse(ret.begin(), ret.end());
1442-
return ret;
1443-
}
1444-
14451441
const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14461442
{
14471443
const Token* prevTok = nullptr;
@@ -1458,10 +1454,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14581454
return prevTok;
14591455
}
14601456

1461-
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1457+
const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
14621458
{
14631459
const Token * const prevTok = lastLineTok(maxsize);
1464-
return prevTok && prevTok->op == '#';
1460+
if (prevTok && prevTok->op == '#')
1461+
return prevTok;
1462+
return nullptr;
14651463
}
14661464

14671465
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
@@ -361,9 +361,8 @@ namespace simplecpp {
361361
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
362362
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
363363

364-
std::string lastLine(int maxsize=1000) const;
365364
const Token* lastLineTok(int maxsize=1000) const;
366-
bool isLastLinePreprocessor(int maxsize=1000) const;
365+
const Token* isLastLinePreprocessor(int maxsize=1000) const;
367366

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

0 commit comments

Comments
 (0)