Skip to content

Commit 9c16fda

Browse files
committed
improved testing of #line handling
1 parent cb9a9a2 commit 9c16fda

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

simplecpp.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,26 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
700700
if (!isLastLinePreprocessor())
701701
continue;
702702
const std::string lastline(lastLine());
703+
// #file "file.c"
703704
if (lastline == "# file %str%") {
704705
const Token *strtok = cback();
705706
while (strtok->comment)
706707
strtok = strtok->previous;
707708
loc.push(location);
708709
location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U));
709710
location.line = 1U;
710-
} else if (lastline == "# line %num%") {
711+
}
712+
// #line 3
713+
// TODO: add support for "# 3"
714+
else if (lastline == "# line %num%") {
711715
const Token *numtok = cback();
712716
while (numtok->comment)
713717
numtok = numtok->previous;
714718
lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), &location);
715-
} else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
719+
}
720+
// # 3 "file.c" - not supported by Visual Studio
721+
// #line 3 "file.c"
722+
else if (lastline == "# %num% %str%" || lastline == "# line %num% %str%") {
716723
const Token *strtok = cback();
717724
while (strtok->comment)
718725
strtok = strtok->previous;

test.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,77 @@ static void location5()
20612061
"int x ;", preprocess(code));
20622062
}
20632063

2064+
static void location6()
2065+
{
2066+
const char code[] =
2067+
"#line 3\n"
2068+
"__LINE__ __FILE__\n";
2069+
ASSERT_EQUALS("\n"
2070+
"\n"
2071+
"3 \"\"",
2072+
preprocess(code));
2073+
}
2074+
2075+
static void location7()
2076+
{
2077+
const char code[] =
2078+
"#line 3 \"file.c\"\n"
2079+
"__LINE__ __FILE__\n";
2080+
ASSERT_EQUALS("\n"
2081+
"#line 3 \"file.c\"\n"
2082+
"3 \"file.c\"",
2083+
preprocess(code));
2084+
}
2085+
2086+
static void location8()
2087+
{
2088+
const char code[] =
2089+
"# 3\n"
2090+
"__LINE__ __FILE__\n";
2091+
ASSERT_EQUALS("\n"
2092+
"2 \"\"", // TODO: should say 3
2093+
preprocess(code));
2094+
}
2095+
2096+
static void location9()
2097+
{
2098+
const char code[] =
2099+
"# 3 \"file.c\"\n"
2100+
"__LINE__ __FILE__\n";
2101+
ASSERT_EQUALS("\n"
2102+
"#line 3 \"file.c\"\n"
2103+
"3 \"file.c\"",
2104+
preprocess(code));
2105+
}
2106+
2107+
static void location10()
2108+
{
2109+
const char code[] =
2110+
"#line 3\n"
2111+
"__LINE__ __FILE__\n";
2112+
ASSERT_EQUALS("\n"
2113+
"\n" // TODO: should this have the #line marker?
2114+
"3 \"\"",
2115+
preprocess(code));
2116+
}
2117+
2118+
static void location11()
2119+
{
2120+
const char code[] =
2121+
"#line 3 \"file.c\"\n"
2122+
"__LINE__ __FILE__\n"
2123+
"#line 33 \"file2.c\"\n"
2124+
"__LINE__ __FILE__\n";
2125+
ASSERT_EQUALS("\n"
2126+
"#line 3 \"file.c\"\n"
2127+
"3 \"file.c\"\n"
2128+
"#line 33 \"file2.c\"\n"
2129+
"33 \"file2.c\"",
2130+
preprocess(code));
2131+
}
2132+
2133+
// TODO: test #file/#endfile
2134+
20642135
static void missingHeader1()
20652136
{
20662137
const char code[] = "#include \"notexist.h\"\n";
@@ -3489,6 +3560,12 @@ int main(int argc, char **argv)
34893560
TEST_CASE(location3);
34903561
TEST_CASE(location4);
34913562
TEST_CASE(location5);
3563+
TEST_CASE(location6);
3564+
TEST_CASE(location7);
3565+
TEST_CASE(location8);
3566+
TEST_CASE(location9);
3567+
TEST_CASE(location10);
3568+
TEST_CASE(location11);
34923569

34933570
TEST_CASE(missingHeader1);
34943571
TEST_CASE(missingHeader2);

0 commit comments

Comments
 (0)