Skip to content

Commit cff231c

Browse files
committed
Improve diagnostics for non-terminated character literals
During brace matching (before lexing), add an extra diagnostic if one is found During lexing, this would already be diagnosed - I also tweaked that message to accurately report where the missing `'` should have been (it was off by one and including the character where the `'` should have been in the suggested corrected literal, but that's the first character that can't belong in the literal so it should be excluded)
1 parent fb820eb commit cff231c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

source/io.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,13 @@ auto process_cpp2_line(
795795
prev = line[i];
796796
}
797797

798+
if (in_char_literal) {
799+
errors.emplace_back(
800+
source_position(lineno, ssize(line)),
801+
std::string("line ended before character literal was terminated")
802+
);
803+
}
804+
798805
return found_end;
799806
}
800807

source/lex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,9 +1654,10 @@ auto lex_line(
16541654
if (len > 0) {
16551655
j += len;
16561656
if (peek(j) != '\'') {
1657+
assert (j > 1);
16571658
errors.emplace_back(
16581659
source_position(lineno, i),
1659-
"character literal '" + std::string(&line[i+1],j)
1660+
"character literal '" + std::string(&line[i+1],j-1)
16601661
+ "' is missing its closing '"
16611662
);
16621663
}

0 commit comments

Comments
 (0)