Skip to content

Commit 8e88fa7

Browse files
committed
review comments: fix handling of signed ints
- update Lexer to allow '+' prefix to denote a signed positive integer - add missing errors when we expect an unsigned int for parameters - add tests to show lexing of positive signed integer and unsigned integer
1 parent a328619 commit 8e88fa7

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

clang/lib/Parse/ParseHLSLRootSignature.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ namespace root_signature {
66

77
// Lexer Definitions
88

9-
static bool IsPreprocessorNumberChar(char C) {
9+
static bool IsNumberChar(char C) {
1010
// TODO: extend for float support with or without hexadecimal/exponent
1111
return isdigit(C); // integer support
1212
}
1313

1414
bool RootSignatureLexer::LexNumber(RootSignatureToken &Result) {
1515
// NumericLiteralParser does not handle the sign so we will manually apply it
16-
Result.Signed = Buffer.front() == '-';
16+
bool Negative = Buffer.front() == '-';
17+
Result.Signed = Negative || Buffer.front() == '+';
1718
if (Result.Signed)
1819
AdvanceBuffer();
1920

2021
// Retrieve the possible number
21-
StringRef NumSpelling = Buffer.take_while(IsPreprocessorNumberChar);
22+
StringRef NumSpelling = Buffer.take_while(IsNumberChar);
2223

2324
// Parse the numeric value and do semantic checks on its specification
2425
clang::NumericLiteralParser Literal(NumSpelling, SourceLoc,
@@ -35,7 +36,7 @@ bool RootSignatureLexer::LexNumber(RootSignatureToken &Result) {
3536
if (Literal.GetIntegerValue(X))
3637
return true; // TODO: Report overflow error
3738

38-
X = Result.Signed ? -X : X;
39+
X = Negative ? -X : X;
3940
Result.IntLiteral = (uint32_t)X.getZExtValue();
4041
} else {
4142
return true; // TODO: report unsupported number literal specification
@@ -84,7 +85,7 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
8485
}
8586

8687
// Numeric constant
87-
if (isdigit(C) || C == '-')
88+
if (isdigit(C) || C == '-' || C == '+')
8889
return LexNumber(Result);
8990

9091
// All following tokens require at least one additional character
@@ -101,7 +102,8 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
101102
if (LexNumber(Result))
102103
return true;
103104

104-
// Lex number could also parse a float so ensure it was an unsigned int
105+
// Lex number could also parse a signed int/float so ensure it was an
106+
// unsigned int
105107
if (Result.Kind != TokenKind::int_literal || Result.Signed)
106108
return true; // Return invalid number literal for register error
107109

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ TEST_F(ParseHLSLRootSignatureTest, LexValidTokensTest) {
105105
shader_visibility_pixel
106106
shader_visibility_amplification
107107
shader_visibility_mesh
108+
109+
42 +42
108110
)cc";
109111

110112
TrivialModuleLoader ModLoader;
@@ -121,6 +123,8 @@ TEST_F(ParseHLSLRootSignatureTest, LexValidTokensTest) {
121123
SmallVector<TokenKind> Expected = {
122124
#define TOK(NAME) TokenKind::NAME,
123125
#include "clang/Parse/HLSLRootSignatureTokenKinds.def"
126+
TokenKind::int_literal,
127+
TokenKind::int_literal,
124128
};
125129

126130
CheckTokens(Tokens, Expected);

0 commit comments

Comments
 (0)