Skip to content

Commit 0cca41f

Browse files
committed
Add syntax error for signed line numbers
1 parent 40a815f commit 0cca41f

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

simplecpp.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
737737
if (ppTok->str() == "line")
738738
ppTok = advanceAndSkipComments(ppTok);
739739

740+
if (ppTok && (ppTok->str()[0] == '-' || ppTok->str()[0] == '+')) {
741+
if (outputList) {
742+
simplecpp::Output err{
743+
simplecpp::Output::SYNTAX_ERROR,
744+
location,
745+
"Invalid character in line directive: '" + ppTok->str() + "'."
746+
};
747+
outputList->emplace_back(std::move(err));
748+
}
749+
clear();
750+
return;
751+
}
752+
740753
if (!ppTok || !ppTok->number)
741754
continue;
742755

test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,16 @@ static void lineDirective()
28692869
simplecpp::DUI dui;
28702870
dui.std = std;
28712871

2872+
{
2873+
const char code[] =
2874+
"#line -1\n"
2875+
";\n";
2876+
simplecpp::OutputList outputList;
2877+
makeTokenList(code, dui, &outputList);
2878+
ASSERT_EQUALS("file0,2,syntax_error,Invalid character in line directive: '-'.\n",
2879+
toString(outputList));
2880+
}
2881+
28722882
{
28732883
const char code[] =
28742884
"#line 0\n"
@@ -2919,6 +2929,16 @@ static void lineDirective()
29192929
simplecpp::DUI dui;
29202930
dui.std = std;
29212931

2932+
{
2933+
const char code[] =
2934+
"#line -1\n"
2935+
";\n";
2936+
simplecpp::OutputList outputList;
2937+
makeTokenList(code, dui, &outputList);
2938+
ASSERT_EQUALS("file0,2,syntax_error,Invalid character in line directive: '-'.\n",
2939+
toString(outputList));
2940+
}
2941+
29222942
{
29232943
const char code[] =
29242944
"#line 0\n"
@@ -2983,6 +3003,16 @@ static void lineDirective()
29833003
simplecpp::DUI dui;
29843004
dui.std = "c++26";
29853005

3006+
{
3007+
const char code[] =
3008+
"#line -1\n"
3009+
";\n";
3010+
simplecpp::OutputList outputList;
3011+
makeTokenList(code, dui, &outputList);
3012+
ASSERT_EQUALS("file0,2,syntax_error,Invalid character in line directive: '-'.\n",
3013+
toString(outputList));
3014+
}
3015+
29863016
{
29873017
const char code[] =
29883018
"#line 0\n"

0 commit comments

Comments
 (0)