Skip to content

Commit aa7a73d

Browse files
committed
review: punt parsing of a signed integer to the parser
1 parent e58c89a commit aa7a73d

File tree

4 files changed

+18
-53
lines changed

4 files changed

+18
-53
lines changed

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,6 @@ Error<"expected 'begin' or 'end'">;
10201020

10211021
// HLSL Root Signature Lexing Errors
10221022
let CategoryName = "Root Signature Lexical Issue" in {
1023-
def err_hlsl_expected_number_literal: Error<"expected numberic literal">;
10241023
def err_hlsl_number_literal_overflow :
10251024
Error<"integer literal '%0' is too large to be represented in a 32-bit integer type">;
10261025
def err_hlsl_invalid_register_literal: Error<"expected unsigned integer literal as part of register">;

clang/include/clang/Parse/HLSLRootSignatureTokenKinds.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ PUNCTUATOR(r_paren, ')')
6666
PUNCTUATOR(comma, ',')
6767
PUNCTUATOR(or, '|')
6868
PUNCTUATOR(equal, '=')
69+
PUNCTUATOR(plus, '+')
70+
PUNCTUATOR(minus, '-')
6971

7072
// RootElement Keywords:
7173
KEYWORD(DescriptorTable)

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,9 @@ static bool IsNumberChar(char C) {
1111
}
1212

1313
bool RootSignatureLexer::LexNumber(RootSignatureToken &Result) {
14-
// NumericLiteralParser does not handle the sign so we will manually apply it
15-
bool Negative = Buffer.front() == '-';
16-
bool Signed = Negative || Buffer.front() == '+';
17-
if (Signed)
18-
AdvanceBuffer();
19-
2014
// Retrieve the possible number
2115
StringRef NumSpelling = Buffer.take_while(IsNumberChar);
2216

23-
// Catch when there is a '+' or '-' specified but no literal value after.
24-
// This is invalid but the NumericLiteralParser will accept this as valid.
25-
if (NumSpelling.empty()) {
26-
PP.getDiagnostics().Report(Result.TokLoc,
27-
diag::err_hlsl_expected_number_literal);
28-
return true;
29-
}
30-
3117
// Parse the numeric value and do semantic checks on its specification
3218
clang::NumericLiteralParser Literal(NumSpelling, SourceLoc,
3319
PP.getSourceManager(), PP.getLangOpts(),
@@ -40,20 +26,17 @@ bool RootSignatureLexer::LexNumber(RootSignatureToken &Result) {
4026
assert(Literal.isIntegerLiteral() && "IsNumberChar will only support digits");
4127

4228
// Retrieve the number value to store into the token
43-
Result.Kind = TokenKind::int_literal;
4429

45-
// NOTE: for compabibility with DXC, we will treat any integer with '+' as an
46-
// unsigned integer
47-
llvm::APSInt X = llvm::APSInt(32, !Negative);
30+
llvm::APSInt X = llvm::APSInt(32, true);
4831
if (Literal.GetIntegerValue(X)) {
4932
// Report that the value has overflowed
5033
PP.getDiagnostics().Report(Result.TokLoc,
5134
diag::err_hlsl_number_literal_overflow)
52-
<< (unsigned)!Signed << NumSpelling;
35+
<< NumSpelling;
5336
return true;
5437
}
5538

56-
X = Negative ? -X : X;
39+
Result.Kind = TokenKind::int_literal;
5740
Result.NumLiteral = APValue(X);
5841

5942
AdvanceBuffer(NumSpelling.size());
@@ -83,7 +66,7 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
8366
}
8467

8568
// Numeric constant
86-
if (isdigit(C) || C == '-' || C == '+')
69+
if (isdigit(C))
8770
return LexNumber(Result);
8871

8972
// All following tokens require at least one additional character

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,32 @@ TEST_F(ParseHLSLRootSignatureTest, ValidLexNumbersTest) {
136136

137137
SmallVector<hlsl::RootSignatureToken> Tokens;
138138
SmallVector<hlsl::TokenKind> Expected = {
139-
hlsl::TokenKind::int_literal,
140-
hlsl::TokenKind::int_literal,
141-
hlsl::TokenKind::int_literal,
139+
hlsl::TokenKind::pu_minus, hlsl::TokenKind::int_literal,
140+
hlsl::TokenKind::int_literal, hlsl::TokenKind::pu_plus,
141+
hlsl::TokenKind::int_literal, hlsl::TokenKind::pu_plus,
142142
hlsl::TokenKind::int_literal,
143143
};
144144
CheckTokens(Lexer, Tokens, Expected);
145145
ASSERT_TRUE(Consumer->IsSatisfied());
146146

147-
// Sample negative int
148-
hlsl::RootSignatureToken IntToken = Tokens[0];
149-
ASSERT_TRUE(IntToken.NumLiteral.getInt().isSigned());
150-
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), -42);
147+
// // Sample negative: int component
148+
hlsl::RootSignatureToken IntToken = Tokens[1];
149+
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
150+
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 42);
151151

152152
// Sample unsigned int
153-
IntToken = Tokens[1];
153+
IntToken = Tokens[2];
154154
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
155155
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 42);
156156

157-
// Sample positive int that is treated as unsigned
158-
IntToken = Tokens[2];
157+
// Sample positive: int component
158+
IntToken = Tokens[4];
159159
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
160160
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 42);
161161

162162
// Sample positive int that would overflow the signed representation but
163163
// is treated as an unsigned integer instead
164-
IntToken = Tokens[3];
164+
IntToken = Tokens[6];
165165
ASSERT_FALSE(IntToken.NumLiteral.getInt().isSigned());
166166
ASSERT_EQ(IntToken.NumLiteral.getInt().getExtValue(), 2147483648);
167167
}
@@ -174,7 +174,7 @@ TEST_F(ParseHLSLRootSignatureTest, ValidLexAllTokensTest) {
174174
175175
b0 t43 u987 s234
176176
177-
(),|=
177+
(),|=+-
178178
179179
DescriptorTable
180180
@@ -286,25 +286,6 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexOverflowedNumberTest) {
286286
ASSERT_TRUE(Consumer->IsSatisfied());
287287
}
288288

289-
TEST_F(ParseHLSLRootSignatureTest, InvalidLexEmptyNumberTest) {
290-
// This test will check that the lexing fails due to no integer being provided
291-
const llvm::StringLiteral Source = R"cc(
292-
-
293-
)cc";
294-
295-
TrivialModuleLoader ModLoader;
296-
auto PP = CreatePP(Source, ModLoader);
297-
auto TokLoc = SourceLocation();
298-
299-
// Test correct diagnostic produced
300-
Consumer->SetExpected(diag::err_hlsl_expected_number_literal);
301-
302-
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
303-
304-
ASSERT_TRUE(Lexer.ConsumeToken());
305-
ASSERT_TRUE(Consumer->IsSatisfied());
306-
}
307-
308289
TEST_F(ParseHLSLRootSignatureTest, InvalidLexRegNumberTest) {
309290
// This test will check that the lexing fails due to no integer being provided
310291
const llvm::StringLiteral Source = R"cc(

0 commit comments

Comments
 (0)