@@ -91,12 +91,19 @@ class ParseHLSLRootSignatureTest : public ::testing::Test {
91
91
return PP;
92
92
}
93
93
94
- void CheckTokens (SmallVector<hlsl::RootSignatureToken> &Computed,
94
+ void CheckTokens (hlsl::RootSignatureLexer &Lexer,
95
+ SmallVector<hlsl::RootSignatureToken> &Computed,
95
96
SmallVector<hlsl::TokenKind> &Expected) {
96
- ASSERT_EQ (Computed.size (), Expected.size ());
97
97
for (unsigned I = 0 , E = Expected.size (); I != E; ++I) {
98
- ASSERT_EQ (Computed[I].Kind , Expected[I]);
98
+ if (Expected[I] == hlsl::TokenKind::invalid ||
99
+ Expected[I] == hlsl::TokenKind::end_of_stream)
100
+ continue ;
101
+ ASSERT_FALSE (Lexer.ConsumeToken ());
102
+ hlsl::RootSignatureToken Result = Lexer.GetCurToken ();
103
+ ASSERT_EQ (Result.Kind , Expected[I]);
104
+ Computed.push_back (Result);
99
105
}
106
+ ASSERT_TRUE (Lexer.EndOfBuffer ());
100
107
}
101
108
102
109
FileSystemOptions FileMgrOpts;
@@ -128,16 +135,14 @@ TEST_F(ParseHLSLRootSignatureTest, ValidLexNumbersTest) {
128
135
hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
129
136
130
137
SmallVector<hlsl::RootSignatureToken> Tokens;
131
- ASSERT_FALSE (Lexer.Lex (Tokens));
132
- ASSERT_TRUE (Consumer->IsSatisfied ());
133
-
134
138
SmallVector<hlsl::TokenKind> Expected = {
135
139
hlsl::TokenKind::int_literal,
136
140
hlsl::TokenKind::int_literal,
137
141
hlsl::TokenKind::int_literal,
138
142
hlsl::TokenKind::int_literal,
139
143
};
140
- CheckTokens (Tokens, Expected);
144
+ CheckTokens (Lexer, Tokens, Expected);
145
+ ASSERT_TRUE (Consumer->IsSatisfied ());
141
146
142
147
// Sample negative int
143
148
hlsl::RootSignatureToken IntToken = Tokens[0 ];
@@ -204,23 +209,77 @@ TEST_F(ParseHLSLRootSignatureTest, ValidLexAllTokensTest) {
204
209
205
210
hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
206
211
207
- SmallVector<hlsl::RootSignatureToken> Tokens = {
208
- hlsl::RootSignatureToken (
209
- SourceLocation ()) // invalid token for completeness
210
- };
211
- ASSERT_FALSE (Lexer.Lex (Tokens));
212
- ASSERT_TRUE (Consumer->IsSatisfied ());
213
-
212
+ SmallVector<hlsl::RootSignatureToken> Tokens;
214
213
SmallVector<hlsl::TokenKind> Expected = {
215
214
#define TOK (NAME ) hlsl::TokenKind::NAME,
216
215
#include " clang/Parse/HLSLRootSignatureTokenKinds.def"
217
216
};
218
217
219
- CheckTokens (Tokens, Expected);
218
+ CheckTokens (Lexer, Tokens, Expected);
219
+ ASSERT_TRUE (Consumer->IsSatisfied ());
220
+ }
221
+
222
+ TEST_F (ParseHLSLRootSignatureTest, ValidLexPeekTest) {
223
+ // This test will check that we can lex all defined tokens as defined in
224
+ // HLSLRootSignatureTokenKinds.def, plus some additional integer variations
225
+ const llvm::StringLiteral Source = R"cc(
226
+ )1
227
+ )cc" ;
228
+
229
+ TrivialModuleLoader ModLoader;
230
+ auto PP = CreatePP (Source, ModLoader);
231
+ auto TokLoc = SourceLocation ();
232
+
233
+ // Test no diagnostics produced
234
+ Consumer->SetNoDiag ();
235
+
236
+ hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
237
+ // Test basic peek
238
+ auto Res = Lexer.PeekNextToken ();
239
+ ASSERT_TRUE (Res.has_value ());
240
+ ASSERT_EQ (Res->Kind , hlsl::TokenKind::pu_r_paren);
241
+
242
+ // Ensure it doesn't peek past one element
243
+ Res = Lexer.PeekNextToken ();
244
+ ASSERT_TRUE (Res.has_value ());
245
+ ASSERT_EQ (Res->Kind , hlsl::TokenKind::pu_r_paren);
246
+
247
+ ASSERT_FALSE (Lexer.ConsumeToken ());
248
+
249
+ // Invoke after reseting the NextToken
250
+ Res = Lexer.PeekNextToken ();
251
+ ASSERT_TRUE (Res.has_value ());
252
+ ASSERT_EQ (Res->Kind , hlsl::TokenKind::int_literal);
253
+
254
+ // Ensure we can still consume the second token
255
+ ASSERT_FALSE (Lexer.ConsumeToken ());
256
+
257
+ // Ensure no error raised when peeking past end of stream
258
+ Res = Lexer.PeekNextToken ();
259
+ ASSERT_TRUE (Res.has_value ());
260
+ ASSERT_EQ (Res->Kind , hlsl::TokenKind::end_of_stream);
261
+
262
+ ASSERT_TRUE (Consumer->IsSatisfied ());
220
263
}
221
264
222
265
// Invalid Lexing Tests
223
266
267
+ TEST_F (ParseHLSLRootSignatureTest, InvalidParseUnexpectedEOSTest) {
268
+ const llvm::StringLiteral Source = R"cc( )cc" ;
269
+
270
+ TrivialModuleLoader ModLoader;
271
+ auto PP = CreatePP (Source, ModLoader);
272
+ auto TokLoc = SourceLocation ();
273
+
274
+ hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
275
+
276
+ // Test correct diagnostic produced
277
+ Consumer->SetExpected (diag::err_hlsl_rootsig_unexpected_eos);
278
+ ASSERT_TRUE (Lexer.ConsumeToken ());
279
+
280
+ ASSERT_TRUE (Consumer->IsSatisfied ());
281
+ }
282
+
224
283
TEST_F (ParseHLSLRootSignatureTest, InvalidLexOverflowedNumberTest) {
225
284
// This test will check that the lexing fails due to an integer overflow
226
285
const llvm::StringLiteral Source = R"cc(
@@ -236,8 +295,7 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexOverflowedNumberTest) {
236
295
237
296
hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
238
297
239
- SmallVector<hlsl::RootSignatureToken> Tokens;
240
- ASSERT_TRUE (Lexer.Lex (Tokens));
298
+ ASSERT_TRUE (Lexer.ConsumeToken ());
241
299
ASSERT_TRUE (Consumer->IsSatisfied ());
242
300
}
243
301
@@ -256,8 +314,7 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexEmptyNumberTest) {
256
314
257
315
hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
258
316
259
- SmallVector<hlsl::RootSignatureToken> Tokens;
260
- ASSERT_TRUE (Lexer.Lex (Tokens));
317
+ ASSERT_TRUE (Lexer.ConsumeToken ());
261
318
ASSERT_TRUE (Consumer->IsSatisfied ());
262
319
}
263
320
@@ -276,9 +333,8 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexRegNumberTest) {
276
333
277
334
hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
278
335
279
- SmallVector<hlsl::RootSignatureToken> Tokens;
280
- ASSERT_TRUE (Lexer.Lex (Tokens));
281
- // FIXME(#120472): This should be TRUE once we can lex a floating
336
+ ASSERT_TRUE (Lexer.ConsumeToken ());
337
+ // FIXME(#126565): This should be TRUE once we can lex a float
282
338
ASSERT_FALSE (Consumer->IsSatisfied ());
283
339
}
284
340
@@ -297,8 +353,7 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexIdentifierTest) {
297
353
298
354
hlsl::RootSignatureLexer Lexer (Source, TokLoc, *PP);
299
355
300
- SmallVector<hlsl::RootSignatureToken> Tokens;
301
- ASSERT_TRUE (Lexer.Lex (Tokens));
356
+ ASSERT_TRUE (Lexer.ConsumeToken ());
302
357
ASSERT_TRUE (Consumer->IsSatisfied ());
303
358
}
304
359
0 commit comments