Skip to content

Commit 6b6f453

Browse files
committed
improved testing of #line handling
1 parent 538c5c4 commit 6b6f453

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

simplecpp.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,19 +688,26 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
688688
if (!isLastLinePreprocessor())
689689
continue;
690690
const std::string lastline(lastLine());
691+
// #file "file.c"
691692
if (lastline == "# file %str%") {
692693
const Token *strtok = cback();
693694
while (strtok->comment)
694695
strtok = strtok->previous;
695696
loc.push(location);
696697
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
697698
location.line = 1U;
698-
} else if (lastline == "# line %num%") {
699+
}
700+
// #line 3
701+
// TODO: add support for "# 3"
702+
else if (lastline == "# line %num%") {
699703
const Token *numtok = cback();
700704
while (numtok->comment)
701705
numtok = numtok->previous;
702706
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
703-
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
707+
}
708+
// # 3 "file.c" - not supported by Visual Studio
709+
// #line 3 "file.c"
710+
else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
704711
const Token *strtok = cback();
705712
while (strtok->comment)
706713
strtok = strtok->previous;
@@ -710,7 +717,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
710717
lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")),
711718
std::atol(numtok->str().c_str()), &location);
712719
}
713-
// #endfile
720+
// #endfile - only supported by Visual Studio(?)
714721
else if (lastline == "# endfile" && !loc.empty()) {
715722
location = loc.top();
716723
loc.pop();

test.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,39 @@ static void location5()
20142014
"int x ;", preprocess(code));
20152015
}
20162016

2017+
static void location6()
2018+
{
2019+
const char code[] =
2020+
"#line 3\n"
2021+
"__LINE__ __FILE__\n";
2022+
ASSERT_EQUALS("\n\n3 \"\"", preprocess(code));
2023+
}
2024+
2025+
static void location7()
2026+
{
2027+
const char code[] =
2028+
"#line 3 \"file.c\"\n"
2029+
"__LINE__ __FILE__\n";
2030+
ASSERT_EQUALS("\n#line 3 \"file.c\"\n3 \"file.c\"", preprocess(code));
2031+
}
2032+
2033+
static void location8()
2034+
{
2035+
const char code[] =
2036+
"# 3\n"
2037+
"__LINE__ __FILE__\n";
2038+
ASSERT_EQUALS("\n\n3 \"\"", preprocess(code)); // TODO
2039+
}
2040+
2041+
static void location9()
2042+
{
2043+
const char code[] =
2044+
"# 3 \"file.c\"\n"
2045+
"__LINE__ __FILE__\n";
2046+
ASSERT_EQUALS("\n#line 3 \"file.c\"\n3 \"file.c\"", preprocess(code));
2047+
}
2048+
2049+
20172050
static void missingHeader1()
20182051
{
20192052
const char code[] = "#include \"notexist.h\"\n";
@@ -3351,6 +3384,10 @@ int main(int argc, char **argv)
33513384
TEST_CASE(location3);
33523385
TEST_CASE(location4);
33533386
TEST_CASE(location5);
3387+
TEST_CASE(location6);
3388+
TEST_CASE(location7);
3389+
TEST_CASE(location8);
3390+
TEST_CASE(location9);
33543391

33553392
TEST_CASE(missingHeader1);
33563393
TEST_CASE(missingHeader2);

0 commit comments

Comments
 (0)