Skip to content

Commit a765b88

Browse files
committed
Handle '# %num% %str%' locations generated by gcc preprocessor
1 parent 885a27f commit a765b88

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

simplecpp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
439439
if (oldLastToken != cback()) {
440440
oldLastToken = cback();
441441
const std::string lastline(lastLine());
442-
443442
if (lastline == "# file %str%") {
444443
loc.push(location);
445444
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U));
@@ -451,6 +450,10 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
451450
loc.push(location);
452451
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U));
453452
location.line = std::atol(cback()->previous->str().c_str());
453+
} else if (lastline == "# %num% %str%") {
454+
loc.push(location);
455+
location.fileIndex = fileIndex(cback()->str().substr(1U, cback()->str().size() - 2U));
456+
location.line = std::atol(cback()->previous->str().c_str());
454457
}
455458
// #endfile
456459
else if (lastline == "# endfile" && !loc.empty()) {

test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,15 @@ static void ifalt() // using "and", "or", etc
894894
ASSERT_EQUALS("\n1", preprocess(code));
895895
}
896896

897+
static void location1()
898+
{
899+
const char *code;
900+
901+
code = "# 1 \"main.c\"\n\n\n"
902+
"x";
903+
ASSERT_EQUALS("\n#line 3 \"main.c\"\nx", preprocess(code));
904+
}
905+
897906
static void missingHeader1()
898907
{
899908
const simplecpp::DUI dui;
@@ -1626,6 +1635,8 @@ int main(int argc, char **argv)
16261635
TEST_CASE(ifdiv0);
16271636
TEST_CASE(ifalt); // using "and", "or", etc
16281637

1638+
TEST_CASE(location1);
1639+
16291640
TEST_CASE(missingHeader1);
16301641
TEST_CASE(missingHeader2);
16311642
TEST_CASE(missingHeader3);

0 commit comments

Comments
 (0)