Skip to content

Commit 9cd5547

Browse files
committed
Better handling of line directive
1 parent 03f9ac8 commit 9cd5547

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

simplecpp.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,23 @@ static bool isStringLiteralPrefix(const std::string &str)
412412
str == "R" || str == "uR" || str == "UR" || str == "LR" || str == "u8R";
413413
}
414414

415+
void simplecpp::TokenList::lineDirective(unsigned int fileIndex, unsigned int line, Location *location)
416+
{
417+
if (fileIndex != location->fileIndex || line >= location->line) {
418+
location->fileIndex = fileIndex;
419+
location->line = line;
420+
return;
421+
}
422+
423+
if (line + 2 >= location->line) {
424+
location->line = line;
425+
while (cback()->op != '#')
426+
deleteToken(back());
427+
deleteToken(back());
428+
return;
429+
}
430+
}
431+
415432
void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filename, OutputList *outputList)
416433
{
417434
std::stack<simplecpp::Location> loc;
@@ -468,23 +485,10 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
468485
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U));
469486
location.line = 1U;
470487
} else if (lastline == "# line %num%") {
471-
const Location loc1 = location;
472-
location.line = std::atol(cback()->str().c_str());
473-
if (location.line < loc1.line)
474-
location.line = loc1.line;
475-
} else if (lastline == "# line %num% %str%") {
476-
const Location loc1 = location;
477-
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U));
478-
location.line = std::atol(cback()->previous->str().c_str());
479-
if (loc1.fileIndex == location.fileIndex && location.line < loc1.line)
480-
location.line = loc1.line;
481-
} else if (lastline == "# %num% %str%") {
482-
const Location loc1 = location;
483-
loc.push(location);
484-
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U));
485-
location.line = std::atol(cback()->previous->str().c_str());
486-
if (loc1.fileIndex == location.fileIndex && location.line < loc1.line)
487-
location.line = loc1.line;
488+
lineDirective(location.fileIndex, std::atol(cback()->str().c_str()), &location);
489+
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
490+
lineDirective(fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U)),
491+
std::atol(cback()->previous->str().c_str()), &location);
488492
}
489493
// #endfile
490494
else if (lastline == "# endfile" && !loc.empty()) {

simplecpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ namespace simplecpp {
265265
void constFoldQuestionOp(Token **tok1);
266266

267267
std::string readUntil(std::istream &istr, const Location &location, const char start, const char end, OutputList *outputList, unsigned int bom);
268+
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);
268269

269270
std::string lastLine(int maxsize=100000) const;
270271

test.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,12 +1027,21 @@ static void location2()
10271027
"#line 40 \"abc.y\"\n"
10281028
"{\n"
10291029
"}\n"
1030-
"\n"
10311030
"{\n"
10321031
"}\n"
10331032
"} }", preprocess(code));
10341033
}
10351034

1035+
static void location3()
1036+
{
1037+
const char *code;
1038+
code = "#line 1 \"x\" \n"
1039+
"a\n"
1040+
"#line 1 \"x\" \n"
1041+
"b\n";
1042+
ASSERT_EQUALS("\n#line 1 \"x\"\na b", preprocess(code));
1043+
}
1044+
10361045
static void missingHeader1()
10371046
{
10381047
const simplecpp::DUI dui;
@@ -1897,6 +1906,7 @@ int main(int argc, char **argv)
18971906

18981907
TEST_CASE(location1);
18991908
TEST_CASE(location2);
1909+
TEST_CASE(location3);
19001910

19011911
TEST_CASE(missingHeader1);
19021912
TEST_CASE(missingHeader2);

0 commit comments

Comments
 (0)