Skip to content

Commit 9351946

Browse files
committed
update RootSignatureToken to hold the offset into the signature string
1 parent d84d287 commit 9351946

File tree

7 files changed

+141
-130
lines changed

7 files changed

+141
-130
lines changed

clang/include/clang/Lex/LexHLSLRootSignature.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ struct RootSignatureToken {
3131

3232
Kind TokKind = Kind::invalid;
3333

34-
// Retain the SouceLocation of the token for diagnostics
35-
clang::SourceLocation TokLoc;
34+
// Retain the location offset of the token in the Signature
35+
// string
36+
uint32_t LocOffset;
3637

3738
// Retain spelling of an numeric constant to be parsed later
3839
StringRef NumSpelling;
3940

4041
// Constructors
41-
RootSignatureToken(clang::SourceLocation TokLoc) : TokLoc(TokLoc) {}
42-
RootSignatureToken(Kind TokKind, clang::SourceLocation TokLoc)
43-
: TokKind(TokKind), TokLoc(TokLoc) {}
42+
RootSignatureToken(uint32_t LocOffset) : LocOffset(LocOffset) {}
43+
RootSignatureToken(Kind TokKind, uint32_t LocOffset)
44+
: TokKind(TokKind), LocOffset(LocOffset) {}
4445
};
4546

4647
inline const DiagnosticBuilder &
@@ -61,8 +62,7 @@ operator<<(const DiagnosticBuilder &DB, const RootSignatureToken::Kind Kind) {
6162

6263
class RootSignatureLexer {
6364
public:
64-
RootSignatureLexer(StringRef Signature, clang::SourceLocation SourceLoc)
65-
: Buffer(Signature), SourceLoc(SourceLoc) {}
65+
RootSignatureLexer(StringRef Signature) : Buffer(Signature) {}
6666

6767
/// Consumes and returns the next token.
6868
RootSignatureToken consumeToken();
@@ -76,23 +76,21 @@ class RootSignatureLexer {
7676
}
7777

7878
private:
79-
// Internal buffer to iterate over
79+
// Internal buffer state
8080
StringRef Buffer;
81+
uint32_t LocOffset = 0;
8182

8283
// Current peek state
8384
std::optional<RootSignatureToken> NextToken = std::nullopt;
8485

85-
// Passed down parameters from Sema
86-
clang::SourceLocation SourceLoc;
87-
8886
/// Consumes the buffer and returns the lexed token.
8987
RootSignatureToken lexToken();
9088

9189
/// Advance the buffer by the specified number of characters.
9290
/// Updates the SourceLocation appropriately.
9391
void advanceBuffer(unsigned NumCharacters = 1) {
9492
Buffer = Buffer.drop_front(NumCharacters);
95-
SourceLoc = SourceLoc.getLocWithOffset(NumCharacters);
93+
LocOffset += NumCharacters;
9694
}
9795
};
9896

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ class RootSignatureParser {
189189
bool tryConsumeExpectedToken(RootSignatureToken::Kind Expected);
190190
bool tryConsumeExpectedToken(ArrayRef<RootSignatureToken::Kind> Expected);
191191

192+
/// Convert the token's offset in the signature string to its SourceLocation
193+
///
194+
/// This allows to currently retrieve the location for multi-token
195+
/// StringLiterals
196+
SourceLocation getTokenLocation(RootSignatureToken Tok);
197+
192198
private:
193199
llvm::dxbc::RootSignatureVersion Version;
194200
SmallVector<llvm::hlsl::rootsig::RootElement> &Elements;

clang/lib/Lex/LexHLSLRootSignature.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ RootSignatureToken RootSignatureLexer::lexToken() {
2727
advanceBuffer(Buffer.take_while(isspace).size());
2828

2929
if (isEndOfBuffer())
30-
return RootSignatureToken(TokenKind::end_of_stream, SourceLoc);
30+
return RootSignatureToken(TokenKind::end_of_stream, LocOffset);
3131

3232
// Record where this token is in the text for usage in parser diagnostics
33-
RootSignatureToken Result(SourceLoc);
33+
RootSignatureToken Result(LocOffset);
3434

3535
char C = Buffer.front();
3636

@@ -62,7 +62,7 @@ RootSignatureToken RootSignatureLexer::lexToken() {
6262

6363
// All following tokens require at least one additional character
6464
if (Buffer.size() <= 1) {
65-
Result = RootSignatureToken(TokenKind::invalid, SourceLoc);
65+
Result = RootSignatureToken(TokenKind::invalid, LocOffset);
6666
return Result;
6767
}
6868

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,10 +4950,8 @@ void Parser::ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
49504950
// If we haven't found an already defined DeclIdent then parse the root
49514951
// signature string and construct the in-memory elements
49524952
if (!Found) {
4953-
// Offset location 1 to account for '"'
4954-
SourceLocation SignatureLoc = Signature->getExprLoc().getLocWithOffset(1);
49554953
// Invoke the root signature parser to construct the in-memory constructs
4956-
hlsl::RootSignatureLexer Lexer(Signature->getString(), SignatureLoc);
4954+
hlsl::RootSignatureLexer Lexer(Signature->getString());
49574955
SmallVector<llvm::hlsl::rootsig::RootElement> RootElements;
49584956
hlsl::RootSignatureParser Parser(getLangOpts().HLSLRootSigVer, RootElements,
49594957
Lexer, Signature, PP);

0 commit comments

Comments
 (0)