Skip to content

Commit 4486aac

Browse files
authored
[Lexer] Avoid casting to signed char in switch statements (#37289)
This cast causes overflows on platforms that use unsigned char as default and at least on linux-aarch64 causes false negatives in the switch statements.
1 parent df89d28 commit 4486aac

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

Diff for: lib/Parse/Lexer.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -2349,7 +2349,7 @@ void Lexer::lexImpl() {
23492349
// Remember the start of the token so we can form the text range.
23502350
const char *TokStart = CurPtr;
23512351

2352-
switch ((signed char)*CurPtr++) {
2352+
switch (*CurPtr++) {
23532353
default: {
23542354
char const *Tmp = CurPtr-1;
23552355
if (advanceIfValidStartOfIdentifier(Tmp, BufferEnd))
@@ -2377,8 +2377,8 @@ void Lexer::lexImpl() {
23772377
llvm_unreachable(
23782378
"Whitespaces should be eaten by lexTrivia as LeadingTrivia");
23792379

2380-
case -1:
2381-
case -2:
2380+
case (char)-1:
2381+
case (char)-2:
23822382
diagnose(CurPtr-1, diag::lex_utf16_bom_marker);
23832383
CurPtr = BufferEnd;
23842384
return formToken(tok::unknown, TokStart);
@@ -2529,7 +2529,7 @@ StringRef Lexer::lexTrivia(bool IsForTrailingTrivia,
25292529
Restart:
25302530
const char *TriviaStart = CurPtr;
25312531

2532-
switch ((signed char)*CurPtr++) {
2532+
switch (*CurPtr++) {
25332533
case '\n':
25342534
if (IsForTrailingTrivia)
25352535
break;
@@ -2598,7 +2598,7 @@ StringRef Lexer::lexTrivia(bool IsForTrailingTrivia,
25982598
}
25992599
break;
26002600
// Start character of tokens.
2601-
case -1: case -2:
2601+
case (char)-1: case (char)-2:
26022602
case '@': case '{': case '[': case '(': case '}': case ']': case ')':
26032603
case ',': case ';': case ':': case '\\': case '$':
26042604
case '0': case '1': case '2': case '3': case '4':
@@ -2865,10 +2865,7 @@ ParsedTrivia TriviaLexer::lexTrivia(StringRef TriviaStr) {
28652865

28662866
const char *TriviaStart = CurPtr;
28672867

2868-
signed char CurChar = (signed char)*CurPtr;
2869-
CurPtr++;
2870-
2871-
switch (CurChar) {
2868+
switch (*CurPtr++) {
28722869
case '\n':
28732870
Pieces.appendOrSquash(TriviaKind::Newline, 1);
28742871
continue;

0 commit comments

Comments
 (0)