Skip to content

Commit df1bd4e

Browse files
committed
Fix issues with pragma parsing
1 parent 876398f commit df1bd4e

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

source/parsing/Lexer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ bool Lexer::isNextTokenOnSameLine() {
289289
case '\r':
290290
case '\n':
291291
return false;
292+
case '`':
293+
// Only macro usages are considered to be on the same line; directives are not.
294+
mark();
295+
advance();
296+
return lexDirective().directiveKind() == SyntaxKind::MacroUsage;
292297
default:
293298
return true;
294299
}

source/parsing/Preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ std::pair<Trivia, Trivia> Preprocessor::handlePragmaDirective(Token directive) {
10441044
wantComma = true;
10451045

10461046
if (!succeeded) {
1047-
while (peekSameLine())
1047+
while (peekSameLine() && peek().kind != TokenKind::EndOfFile)
10481048
skipped.push_back(consume());
10491049

10501050
ok = false;

source/parsing/Token.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ bool Token::isOnSameLine() const {
413413
break;
414414
}
415415
}
416-
return true;
416+
return kind != TokenKind::EndOfFile;
417417
}
418418

419419
Token Token::withTrivia(BumpAllocator& alloc, std::span<Trivia const> trivia) const {

tests/unittests/parsing/PreprocessorTests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,3 +2852,21 @@ endmodule
28522852
compilation.addSyntaxTree(tree);
28532853
NO_COMPILATION_ERRORS;
28542854
}
2855+
2856+
TEST_CASE("Pragma number parsing regression 1") {
2857+
auto& text = R"(
2858+
`pragma D's 11111111111111111110's 1.`pragma I's
2859+
)";
2860+
2861+
// Just checking no crash.
2862+
preprocess(text);
2863+
}
2864+
2865+
TEST_CASE("Pragma number parsing regression 2") {
2866+
auto& text = R"(
2867+
`pragma D .`p
2868+
)";
2869+
2870+
// Just checking no crash.
2871+
preprocess(text);
2872+
}

0 commit comments

Comments
 (0)