Skip to content

Commit dd352c8

Browse files
committed
avoid some redundant checks in preprocessor directive handling
optimized handling of file and line preprocessor directives [skip ci]
1 parent d4a6b39 commit dd352c8

File tree

2 files changed

+51
-56
lines changed

2 files changed

+51
-56
lines changed

simplecpp.cpp

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -678,33 +678,55 @@ 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%") {
685-
const Token *strtok = cback();
686-
while (strtok->comment)
687-
strtok = strtok->previous;
688-
loc.push(location);
689-
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
690-
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%") {
697-
const Token *strtok = cback();
698-
while (strtok->comment)
699-
strtok = strtok->previous;
700-
const Token *numtok = strtok->previous;
701-
while (numtok->comment)
702-
numtok = numtok->previous;
703-
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
704-
std::atol(numtok->str().c_str()), &location);
684+
const Token * const llNextToken = llTok->next;
685+
if (!llTok->next)
686+
continue;
687+
if (llNextToken->next) {
688+
// #file "file.c"
689+
if (llNextToken->str() == "file" &&
690+
llNextToken->next->str()[0] == '\"')
691+
{
692+
const Token *strtok = cback();
693+
while (strtok->comment)
694+
strtok = strtok->previous;
695+
loc.push(location);
696+
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
697+
location.line = 1U;
698+
}
699+
// #3 "file.c"
700+
// #line 3 "file.c"
701+
else if ((llNextToken->number &&
702+
llNextToken->next->str()[0] == '\"') ||
703+
(llNextToken->str() == "line" &&
704+
llNextToken->next->number &&
705+
llNextToken->next->next &&
706+
llNextToken->next->next->str()[0] == '\"'))
707+
{
708+
const Token *strtok = cback();
709+
while (strtok->comment)
710+
strtok = strtok->previous;
711+
const Token *numtok = strtok->previous;
712+
while (numtok->comment)
713+
numtok = numtok->previous;
714+
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
715+
std::atol(numtok->str().c_str()), &location);
716+
}
717+
// #line 3
718+
else if (llNextToken->str() == "line" &&
719+
llNextToken->next->number)
720+
{
721+
const Token *numtok = cback();
722+
while (numtok->comment)
723+
numtok = numtok->previous;
724+
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
725+
}
705726
}
706727
// #endfile
707-
else if (lastline == "# endfile" && !loc.empty()) {
728+
else if (llNextToken->str() == "endfile" && !loc.empty())
729+
{
708730
location = loc.top();
709731
loc.pop();
710732
}
@@ -1398,34 +1420,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
13981420
return ret;
13991421
}
14001422

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-
14291423
const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14301424
{
14311425
const Token* prevTok = nullptr;
@@ -1442,10 +1436,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14421436
return prevTok;
14431437
}
14441438

1445-
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1439+
const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
14461440
{
14471441
const Token * const prevTok = lastLineTok(maxsize);
1448-
return prevTok && prevTok->op == '#';
1442+
if (prevTok && prevTok->op == '#')
1443+
return prevTok;
1444+
return nullptr;
14491445
}
14501446

14511447
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)