Skip to content

Commit 9388aae

Browse files
committed
self-review: define an error token to denote lex errors during peek
- the current use of invalid is not able to distinguish between a lex error that has been reported to diagnostics and an invalid token that should be reported during parsing - an error token is defined as the default token to denote a lex error that has been reported and shouldn't be handled by the parsing
1 parent aa7a73d commit 9388aae

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

clang/include/clang/Parse/HLSLRootSignatureTokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#endif
5151

5252
// General Tokens:
53+
TOK(error)
5354
TOK(invalid)
5455
TOK(end_of_stream)
5556
TOK(int_literal)

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct RootSignatureToken {
3131
#include "clang/Parse/HLSLRootSignatureTokenKinds.def"
3232
};
3333

34-
Kind Kind = Kind::invalid;
34+
Kind Kind = Kind::error;
3535

3636
// Retain the SouceLocation of the token for diagnostics
3737
clang::SourceLocation TokLoc;

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
7171

7272
// All following tokens require at least one additional character
7373
if (Buffer.size() <= 1) {
74-
Result = RootSignatureToken(SourceLoc);
74+
Result = RootSignatureToken(TokenKind::invalid, SourceLoc);
7575
return false;
7676
}
7777

@@ -134,6 +134,9 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
134134
bool RootSignatureLexer::ConsumeToken() {
135135
// If we previously peeked then just copy the value over
136136
if (NextToken && NextToken->Kind != TokenKind::end_of_stream) {
137+
// Propogate error up if error was encountered during previous peek
138+
if (NextToken->Kind == TokenKind::error)
139+
return true;
137140
CurToken = *NextToken;
138141
NextToken = std::nullopt;
139142
return false;
@@ -156,8 +159,11 @@ std::optional<RootSignatureToken> RootSignatureLexer::PeekNextToken() {
156159
RootSignatureToken Result;
157160
if (EndOfBuffer()) {
158161
Result = RootSignatureToken(TokenKind::end_of_stream, SourceLoc);
159-
} else if (LexToken(Result))
160-
return std::nullopt; // propogate lex error up
162+
} else if (LexToken(Result)) { // propogate lex error up
163+
// store error token to prevent further peeking
164+
NextToken = RootSignatureToken();
165+
return std::nullopt;
166+
}
161167
NextToken = Result;
162168
return Result;
163169
}

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class ParseHLSLRootSignatureTest : public ::testing::Test {
9595
SmallVector<hlsl::RootSignatureToken> &Computed,
9696
SmallVector<hlsl::TokenKind> &Expected) {
9797
for (unsigned I = 0, E = Expected.size(); I != E; ++I) {
98-
if (Expected[I] == hlsl::TokenKind::invalid ||
98+
if (Expected[I] == hlsl::TokenKind::error ||
99+
Expected[I] == hlsl::TokenKind::invalid ||
99100
Expected[I] == hlsl::TokenKind::end_of_stream)
100101
continue;
101102
ASSERT_FALSE(Lexer.ConsumeToken());
@@ -282,6 +283,11 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexOverflowedNumberTest) {
282283

283284
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
284285

286+
// We will also test that the error can be produced during peek and the Lexer
287+
// will correctly just return true on the next ConsumeToken without
288+
// reporting another error
289+
auto Result = Lexer.PeekNextToken();
290+
ASSERT_EQ(std::nullopt, Result);
285291
ASSERT_TRUE(Lexer.ConsumeToken());
286292
ASSERT_TRUE(Consumer->IsSatisfied());
287293
}

0 commit comments

Comments
 (0)