Skip to content

Commit c6536fa

Browse files
committed
optimized handling of file and line preprocessor directives
1 parent 4db4304 commit c6536fa

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
@@ -678,22 +678,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
678678

679679
if (oldLastToken != cback()) {
680680
oldLastToken = cback();
681-
if (!isLastLinePreprocessor())
681+
const Token * const llTok = isLastLinePreprocessor();
682+
if (!llTok)
682683
continue;
683-
const std::string lastline(lastLine());
684-
if (lastline == "# file %str%") {
684+
const Token * const llNextToken = llTok->next;
685+
if (!llTok->next)
686+
continue;
687+
// #file "file.c"
688+
if (llNextToken->str() == "file" &&
689+
llNextToken->next &&
690+
llNextToken->next->str()[0] == '\"')
691+
{
685692
const Token *strtok = cback();
686693
while (strtok->comment)
687694
strtok = strtok->previous;
688695
loc.push(location);
689696
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
690697
location.line = 1U;
691-
} else if (lastline == "# line %num%") {
692-
const Token *numtok = cback();
693-
while (numtok->comment)
694-
numtok = numtok->previous;
695-
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
696-
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
698+
}
699+
// #3 "file.c"
700+
// #line 3 "file.c"
701+
else if ((llNextToken->number &&
702+
llNextToken->next &&
703+
llNextToken->next->str()[0] == '\"') ||
704+
(llNextToken->str() == "line" &&
705+
llNextToken->next &&
706+
llNextToken->next->number &&
707+
llNextToken->next->next &&
708+
llNextToken->next->next->str()[0] == '\"'))
709+
{
697710
const Token *strtok = cback();
698711
while (strtok->comment)
699712
strtok = strtok->previous;
@@ -703,8 +716,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
703716
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
704717
std::atol(numtok->str().c_str()), &location);
705718
}
719+
// #line 3
720+
else if (llNextToken->str() == "line" &&
721+
llNextToken->next &&
722+
llNextToken->next->number)
723+
{
724+
const Token *numtok = cback();
725+
while (numtok->comment)
726+
numtok = numtok->previous;
727+
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
728+
}
706729
// #endfile
707-
else if (lastline == "# endfile" && !loc.empty()) {
730+
else if (llNextToken->str() == "endfile" && !loc.empty())
731+
{
708732
location = loc.top();
709733
loc.pop();
710734
}
@@ -1398,34 +1422,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
13981422
return ret;
13991423
}
14001424

1401-
std::string simplecpp::TokenList::lastLine(int maxsize) const
1402-
{
1403-
std::string ret;
1404-
int count = 0;
1405-
for (const Token *tok = cback(); ; tok = tok->previous) {
1406-
if (!sameline(tok, cback())) {
1407-
break;
1408-
}
1409-
if (tok->comment)
1410-
continue;
1411-
if (++count > maxsize)
1412-
return "";
1413-
if (!ret.empty())
1414-
ret += ' ';
1415-
// add tokens in reverse for performance reasons
1416-
if (tok->str()[0] == '\"')
1417-
ret += "%rts%"; // %str%
1418-
else if (tok->number)
1419-
ret += "%mun%"; // %num%
1420-
else {
1421-
ret += tok->str();
1422-
std::reverse(ret.end() - tok->str().length(), ret.end());
1423-
}
1424-
}
1425-
std::reverse(ret.begin(), ret.end());
1426-
return ret;
1427-
}
1428-
14291425
const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14301426
{
14311427
const Token* prevTok = nullptr;
@@ -1442,10 +1438,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14421438
return prevTok;
14431439
}
14441440

1445-
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1441+
const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
14461442
{
14471443
const Token * const prevTok = lastLineTok(maxsize);
1448-
return prevTok && prevTok->op == '#';
1444+
if (prevTok && prevTok->op == '#')
1445+
return prevTok;
1446+
return nullptr;
14491447
}
14501448

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

368-
std::string lastLine(int maxsize=1000) const;
369368
const Token* lastLineTok(int maxsize=1000) const;
370-
bool isLastLinePreprocessor(int maxsize=1000) const;
369+
const Token* isLastLinePreprocessor(int maxsize=1000) const;
371370

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

0 commit comments

Comments
 (0)