Skip to content

Commit 6936782

Browse files
committed
fix for root elements
1 parent 3dec46d commit 6936782

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,41 @@ RootSignatureParser::RootSignatureParser(
2525
Lexer(Signature->getString()), PP(PP), CurToken(0) {}
2626

2727
bool RootSignatureParser::parse() {
28-
// Iterate as many RootElements as possible
29-
do {
28+
// Iterate as many RootSignatureElements as possible, until we hit the
29+
// end of the stream
30+
while (!peekExpectedToken(TokenKind::end_of_stream)) {
3031
if (tryConsumeExpectedToken(TokenKind::kw_RootFlags)) {
3132
auto Flags = parseRootFlags();
3233
if (!Flags.has_value())
3334
return true;
3435
Elements.push_back(*Flags);
35-
}
36-
37-
if (tryConsumeExpectedToken(TokenKind::kw_RootConstants)) {
36+
} else if (tryConsumeExpectedToken(TokenKind::kw_RootConstants)) {
3837
auto Constants = parseRootConstants();
3938
if (!Constants.has_value())
4039
return true;
4140
Elements.push_back(*Constants);
42-
}
43-
44-
if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
41+
} else if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
4542
auto Table = parseDescriptorTable();
4643
if (!Table.has_value())
4744
return true;
4845
Elements.push_back(*Table);
49-
}
50-
51-
if (tryConsumeExpectedToken(
52-
{TokenKind::kw_CBV, TokenKind::kw_SRV, TokenKind::kw_UAV})) {
46+
} else if (tryConsumeExpectedToken(
47+
{TokenKind::kw_CBV, TokenKind::kw_SRV, TokenKind::kw_UAV})) {
5348
auto Descriptor = parseRootDescriptor();
5449
if (!Descriptor.has_value())
5550
return true;
5651
Elements.push_back(*Descriptor);
57-
}
58-
59-
if (tryConsumeExpectedToken(TokenKind::kw_StaticSampler)) {
52+
} else if (tryConsumeExpectedToken(TokenKind::kw_StaticSampler)) {
6053
auto Sampler = parseStaticSampler();
6154
if (!Sampler.has_value())
6255
return true;
6356
Elements.push_back(*Sampler);
6457
}
65-
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
58+
59+
// ',' denotes another element, otherwise, expected to be at end of stream
60+
if (!tryConsumeExpectedToken(TokenKind::pu_comma))
61+
break;
62+
}
6663

6764
return consumeExpectedToken(TokenKind::end_of_stream,
6865
diag::err_hlsl_unexpected_end_of_params,
@@ -252,9 +249,7 @@ std::optional<DescriptorTable> RootSignatureParser::parseDescriptorTable() {
252249
return std::nullopt;
253250
Elements.push_back(*Clause);
254251
Table.NumClauses++;
255-
}
256-
257-
if (tryConsumeExpectedToken(TokenKind::kw_visibility)) {
252+
} else if (tryConsumeExpectedToken(TokenKind::kw_visibility)) {
258253
if (Visibility.has_value()) {
259254
reportDiag(diag::err_hlsl_rootsig_repeat_param) << CurToken.TokKind;
260255
return std::nullopt;

clang/test/SemaHLSL/RootSignature-err.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ void bad_root_signature_5() {}
3434
// expected-error@+1 {{expected ')' to denote end of parameters, or, another valid parameter of RootConstants}}
3535
[RootSignature(MultiLineRootSignature)]
3636
void bad_root_signature_6() {}
37+
38+
// expected-error@+1 {{expected end of stream to denote end of parameters, or, another valid parameter of RootSignature}}
39+
[RootSignature("RootFlags() RootConstants(b0, num32BitConstants = 1))")]
40+
void bad_root_signature_7() {}

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,4 +1238,29 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidNonZeroFlagsTest) {
12381238
ASSERT_TRUE(Consumer->isSatisfied());
12391239
}
12401240

1241+
TEST_F(ParseHLSLRootSignatureTest, InvalidRootElementMissingCommaTest) {
1242+
// This test will check that an error is produced when there is a missing
1243+
// comma between parameters
1244+
const llvm::StringLiteral Source = R"cc(
1245+
RootFlags()
1246+
RootConstants(num32BitConstants = 1, b0)
1247+
)cc";
1248+
1249+
auto Ctx = createMinimalASTContext();
1250+
StringLiteral *Signature = wrapSource(Ctx, Source);
1251+
1252+
TrivialModuleLoader ModLoader;
1253+
auto PP = createPP(Source, ModLoader);
1254+
1255+
SmallVector<RootElement> Elements;
1256+
hlsl::RootSignatureParser Parser(RootSignatureVersion::V1_1, Elements,
1257+
Signature, *PP);
1258+
1259+
// Test correct diagnostic produced
1260+
Consumer->setExpected(diag::err_hlsl_unexpected_end_of_params);
1261+
ASSERT_TRUE(Parser.parse());
1262+
1263+
ASSERT_TRUE(Consumer->isSatisfied());
1264+
}
1265+
12411266
} // anonymous namespace

0 commit comments

Comments
 (0)