Skip to content

Commit 18995e0

Browse files
committed
optimized handling of file and line prerocessor directives [skip ci]
1 parent 399f336 commit 18995e0

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

688688
if (oldLastToken != cback()) {
689689
oldLastToken = cback();
690-
if (!isLastLinePreprocessor())
690+
const Token * const llTok = isLastLinePreprocessor();
691+
if (!llTok)
691692
continue;
692-
const std::string lastline(lastLine());
693-
if (lastline == "# file %str%") {
693+
const Token * const llNextToken = llTok->next;
694+
if (!llTok->next)
695+
continue;
696+
// #file "file.c"
697+
if (llNextToken->str() == "file" &&
698+
llNextToken->next &&
699+
llNextToken->next->str()[0] == '\"')
700+
{
694701
const Token *strtok = cback();
695702
while (strtok->comment)
696703
strtok = strtok->previous;
697704
loc.push(location);
698705
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
699706
location.line = 1U;
700-
} else if (lastline == "# line %num%") {
701-
const Token *numtok = cback();
702-
while (numtok->comment)
703-
numtok = numtok->previous;
704-
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
705-
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
707+
}
708+
// #3 "file.c"
709+
// #line 3 "file.c"
710+
else if ((llNextToken->number &&
711+
llNextToken->next &&
712+
llNextToken->next->str()[0] == '\"') ||
713+
(llNextToken->str() == "line" &&
714+
llNextToken->next &&
715+
llNextToken->next->number &&
716+
llNextToken->next->next &&
717+
llNextToken->next->next->str()[0] == '\"'))
718+
{
706719
const Token *strtok = cback();
707720
while (strtok->comment)
708721
strtok = strtok->previous;
@@ -712,8 +725,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
712725
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
713726
std::atol(numtok->str().c_str()), &location);
714727
}
728+
// #line 3
729+
else if (llNextToken->str() == "line" &&
730+
llNextToken->next &&
731+
llNextToken->next->number)
732+
{
733+
const Token *numtok = cback();
734+
while (numtok->comment)
735+
numtok = numtok->previous;
736+
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
737+
}
715738
// #endfile
716-
else if (lastline == "# endfile" && !loc.empty()) {
739+
else if (llNextToken->str() == "endfile" && !loc.empty())
740+
{
717741
location = loc.top();
718742
loc.pop();
719743
}
@@ -1407,34 +1431,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14071431
return ret;
14081432
}
14091433

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

1454-
bool simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
1450+
const Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize) const
14551451
{
14561452
const Token * const prevTok = lastLineTok(maxsize);
1457-
return prevTok && prevTok->op == '#';
1453+
if (prevTok && prevTok->op == '#')
1454+
return prevTok;
1455+
return nullptr;
14581456
}
14591457

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

313-
std::string lastLine(int maxsize=1000) const;
314313
const Token* lastLineTok(int maxsize=1000) const;
315-
bool isLastLinePreprocessor(int maxsize=1000) const;
314+
const Token* isLastLinePreprocessor(int maxsize=1000) const;
316315

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

0 commit comments

Comments
 (0)