Skip to content

[NFC][HLSL][RootSignature] Make the Lexer adhere to naming conventions #134136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions clang/include/clang/Lex/LexHLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ class RootSignatureLexer {
: Buffer(Signature), SourceLoc(SourceLoc) {}

/// Consumes and returns the next token.
RootSignatureToken ConsumeToken();
RootSignatureToken consumeToken();

/// Returns the token that proceeds CurToken
RootSignatureToken PeekNextToken();
RootSignatureToken peekNextToken();

bool EndOfBuffer() {
AdvanceBuffer(Buffer.take_while(isspace).size());
bool isEndOfBuffer() {
advanceBuffer(Buffer.take_while(isspace).size());
return Buffer.empty();
}

Expand All @@ -82,11 +82,11 @@ class RootSignatureLexer {
clang::SourceLocation SourceLoc;

/// Consumes the buffer and returns the lexed token.
RootSignatureToken LexToken();
RootSignatureToken lexToken();

/// Advance the buffer by the specified number of characters.
/// Updates the SourceLocation appropriately.
void AdvanceBuffer(unsigned NumCharacters = 1) {
void advanceBuffer(unsigned NumCharacters = 1) {
Buffer = Buffer.drop_front(NumCharacters);
SourceLoc = SourceLoc.getLocWithOffset(NumCharacters);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Parse/ParseHLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class RootSignatureParser {
bool parseDescriptorTableClause();

/// Invoke the Lexer to consume a token and update CurToken with the result
void consumeNextToken() { CurToken = Lexer.ConsumeToken(); }
void consumeNextToken() { CurToken = Lexer.consumeToken(); }

/// Return true if the next token one of the expected kinds
bool peekExpectedToken(RootSignatureToken::Kind Expected);
Expand Down
30 changes: 15 additions & 15 deletions clang/lib/Lex/LexHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ using TokenKind = RootSignatureToken::Kind;

// Lexer Definitions

static bool IsNumberChar(char C) {
static bool isNumberChar(char C) {
// TODO(#126565): extend for float support exponents
return isdigit(C); // integer support
}

RootSignatureToken RootSignatureLexer::LexToken() {
RootSignatureToken RootSignatureLexer::lexToken() {
// Discard any leading whitespace
AdvanceBuffer(Buffer.take_while(isspace).size());
advanceBuffer(Buffer.take_while(isspace).size());

if (EndOfBuffer())
if (isEndOfBuffer())
return RootSignatureToken(TokenKind::end_of_stream, SourceLoc);

// Record where this token is in the text for usage in parser diagnostics
Expand All @@ -37,7 +37,7 @@ RootSignatureToken RootSignatureLexer::LexToken() {
#define PUNCTUATOR(X, Y) \
case Y: { \
Result.TokKind = TokenKind::pu_##X; \
AdvanceBuffer(); \
advanceBuffer(); \
return Result; \
}
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
Expand All @@ -48,8 +48,8 @@ RootSignatureToken RootSignatureLexer::LexToken() {
// Integer literal
if (isdigit(C)) {
Result.TokKind = TokenKind::int_literal;
Result.NumSpelling = Buffer.take_while(IsNumberChar);
AdvanceBuffer(Result.NumSpelling.size());
Result.NumSpelling = Buffer.take_while(isNumberChar);
advanceBuffer(Result.NumSpelling.size());
return Result;
}

Expand Down Expand Up @@ -82,11 +82,11 @@ RootSignatureToken RootSignatureLexer::LexToken() {
llvm_unreachable("Switch for an expected token was not provided");
}

AdvanceBuffer();
advanceBuffer();

// Lex the integer literal
Result.NumSpelling = Buffer.take_while(IsNumberChar);
AdvanceBuffer(Result.NumSpelling.size());
Result.NumSpelling = Buffer.take_while(isNumberChar);
advanceBuffer(Result.NumSpelling.size());

return Result;
}
Expand All @@ -103,26 +103,26 @@ RootSignatureToken RootSignatureLexer::LexToken() {

// Then attempt to retreive a string from it
Result.TokKind = Switch.Default(TokenKind::invalid);
AdvanceBuffer(TokSpelling.size());
advanceBuffer(TokSpelling.size());
return Result;
}

RootSignatureToken RootSignatureLexer::ConsumeToken() {
RootSignatureToken RootSignatureLexer::consumeToken() {
// If we previously peeked then just return the previous value over
if (NextToken && NextToken->TokKind != TokenKind::end_of_stream) {
RootSignatureToken Result = *NextToken;
NextToken = std::nullopt;
return Result;
}
return LexToken();
return lexToken();
}

RootSignatureToken RootSignatureLexer::PeekNextToken() {
RootSignatureToken RootSignatureLexer::peekNextToken() {
// Already peeked from the current token
if (NextToken)
return *NextToken;

NextToken = LexToken();
NextToken = lexToken();
return *NextToken;
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Parse/ParseHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool RootSignatureParser::peekExpectedToken(TokenKind Expected) {
}

bool RootSignatureParser::peekExpectedToken(ArrayRef<TokenKind> AnyExpected) {
RootSignatureToken Result = Lexer.PeekNextToken();
RootSignatureToken Result = Lexer.peekNextToken();
return llvm::is_contained(AnyExpected, Result.TokKind);
}

Expand Down
26 changes: 13 additions & 13 deletions clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ class LexHLSLRootSignatureTest : public ::testing::Test {
protected:
LexHLSLRootSignatureTest() {}

void CheckTokens(hlsl::RootSignatureLexer &Lexer,
void checkTokens(hlsl::RootSignatureLexer &Lexer,
SmallVector<hlsl::RootSignatureToken> &Computed,
SmallVector<TokenKind> &Expected) {
for (unsigned I = 0, E = Expected.size(); I != E; ++I) {
// Skip these to help with the macro generated test
if (Expected[I] == TokenKind::invalid ||
Expected[I] == TokenKind::end_of_stream)
continue;
hlsl::RootSignatureToken Result = Lexer.ConsumeToken();
hlsl::RootSignatureToken Result = Lexer.consumeToken();
ASSERT_EQ(Result.TokKind, Expected[I]);
Computed.push_back(Result);
}
hlsl::RootSignatureToken EndOfStream = Lexer.ConsumeToken();
hlsl::RootSignatureToken EndOfStream = Lexer.consumeToken();
ASSERT_EQ(EndOfStream.TokKind, TokenKind::end_of_stream);
ASSERT_TRUE(Lexer.EndOfBuffer());
ASSERT_TRUE(Lexer.isEndOfBuffer());
}
};

Expand All @@ -55,7 +55,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexNumbersTest) {
TokenKind::pu_plus, TokenKind::int_literal, TokenKind::pu_plus,
TokenKind::int_literal,
};
CheckTokens(Lexer, Tokens, Expected);
checkTokens(Lexer, Tokens, Expected);

// Sample negative: int component
hlsl::RootSignatureToken IntToken = Tokens[1];
Expand Down Expand Up @@ -119,7 +119,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
};

CheckTokens(Lexer, Tokens, Expected);
checkTokens(Lexer, Tokens, Expected);
}

TEST_F(LexHLSLRootSignatureTest, ValidCaseInsensitiveKeywordsTest) {
Expand Down Expand Up @@ -149,7 +149,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidCaseInsensitiveKeywordsTest) {
TokenKind::kw_offset,
};

CheckTokens(Lexer, Tokens, Expected);
checkTokens(Lexer, Tokens, Expected);
}

TEST_F(LexHLSLRootSignatureTest, ValidLexPeekTest) {
Expand All @@ -161,26 +161,26 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexPeekTest) {
hlsl::RootSignatureLexer Lexer(Source, TokLoc);

// Test basic peek
hlsl::RootSignatureToken Res = Lexer.PeekNextToken();
hlsl::RootSignatureToken Res = Lexer.peekNextToken();
ASSERT_EQ(Res.TokKind, TokenKind::pu_r_paren);

// Ensure it doesn't peek past one element
Res = Lexer.PeekNextToken();
Res = Lexer.peekNextToken();
ASSERT_EQ(Res.TokKind, TokenKind::pu_r_paren);

Res = Lexer.ConsumeToken();
Res = Lexer.consumeToken();
ASSERT_EQ(Res.TokKind, TokenKind::pu_r_paren);

// Invoke after reseting the NextToken
Res = Lexer.PeekNextToken();
Res = Lexer.peekNextToken();
ASSERT_EQ(Res.TokKind, TokenKind::int_literal);

// Ensure we can still consume the second token
Res = Lexer.ConsumeToken();
Res = Lexer.consumeToken();
ASSERT_EQ(Res.TokKind, TokenKind::int_literal);

// Ensure end of stream token
Res = Lexer.PeekNextToken();
Res = Lexer.peekNextToken();
ASSERT_EQ(Res.TokKind, TokenKind::end_of_stream);
}

Expand Down