Skip to content

Commit d648f4c

Browse files
committed
review: dealing with signed positive integer
- the previous implementation would not have thrown an overflow error as expected. Instead it would have been treated as a negative int - DXC treats all positively signed ints as an unsigned integer, so for compatibility, we will be doing the same. - add better unit tests to demonstrate expected functionality
1 parent 1c95e6d commit d648f4c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ bool RootSignatureLexer::LexNumber(RootSignatureToken &Result) {
4141
// Retrieve the number value to store into the token
4242
Result.Kind = TokenKind::int_literal;
4343

44-
llvm::APSInt X = llvm::APSInt(32, !Signed);
44+
// NOTE: for compabibility with DXC, we will treat any integer with '+' as an
45+
// unsigned integer
46+
llvm::APSInt X = llvm::APSInt(32, !Negative);
4547
if (Literal.GetIntegerValue(X)) {
4648
// Report that the value has overflowed
4749
PP.getDiagnostics().Report(Result.TokLoc,

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ParseHLSLRootSignatureTest : public ::testing::Test {
115115
TEST_F(ParseHLSLRootSignatureTest, ValidLexNumbersTest) {
116116
// This test will check that we can lex different number tokens
117117
const llvm::StringLiteral Source = R"cc(
118-
-42 42 +42
118+
-42 42 +42 +2147483648
119119
)cc";
120120

121121
TrivialModuleLoader ModLoader;
@@ -135,8 +135,30 @@ TEST_F(ParseHLSLRootSignatureTest, ValidLexNumbersTest) {
135135
hlsl::TokenKind::int_literal,
136136
hlsl::TokenKind::int_literal,
137137
hlsl::TokenKind::int_literal,
138+
hlsl::TokenKind::int_literal,
138139
};
139140
CheckTokens(Tokens, Expected);
141+
142+
// Sample negative int
143+
hlsl::RootSignatureToken IntToken = Tokens[0];
144+
ASSERT_TRUE(IntToken.NumLiteral.getInt().isSigned());
145+
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), -42);
146+
147+
// Sample unsigned int
148+
IntToken = Tokens[1];
149+
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
150+
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 42);
151+
152+
// Sample positive int that is treated as unsigned
153+
IntToken = Tokens[2];
154+
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
155+
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 42);
156+
157+
// Sample positive int that would overflow the signed representation but
158+
// is treated as an unsigned integer instead
159+
IntToken = Tokens[3];
160+
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
161+
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 2147483648);
140162
}
141163

142164
TEST_F(ParseHLSLRootSignatureTest, ValidLexAllTokensTest) {

0 commit comments

Comments
 (0)