Skip to content

Commit 262eeed

Browse files
author
Finn Plummer
committed
review: remove api of consumeExpectedToken for multiple token kinds
- using `consumeExpectedToken` with multiple expected tokens leads to poor handling of diagnostic messaging and would largely be used in cases where more specific errors could be done - instead we should limit the api to just using `consumeExpectedToken` with only a specific token and to only be used when it is very clear that only one specific token should follow after another
1 parent 78c3837 commit 262eeed

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

clang/include/clang/Lex/LexHLSLRootSignature.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_CLANG_LEX_LEXHLSLROOTSIGNATURE_H
1414
#define LLVM_CLANG_LEX_LEXHLSLROOTSIGNATURE_H
1515

16+
#include "clang/Basic/Diagnostic.h"
1617
#include "clang/Basic/SourceLocation.h"
1718

1819
#include "llvm/ADT/SmallVector.h"
@@ -43,6 +44,18 @@ struct RootSignatureToken {
4344
};
4445
using TokenKind = enum RootSignatureToken::Kind;
4546

47+
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
48+
const TokenKind Kind) {
49+
switch (Kind) {
50+
#define TOK(X, SPELLING) \
51+
case TokenKind::X: \
52+
DB << SPELLING; \
53+
break;
54+
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
55+
}
56+
return DB;
57+
}
58+
4659
class RootSignatureLexer {
4760
public:
4861
RootSignatureLexer(StringRef Signature, clang::SourceLocation SourceLoc)

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ class RootSignatureParser {
8383
bool consumeExpectedToken(TokenKind Expected,
8484
unsigned DiagID = diag::err_expected,
8585
TokenKind Context = TokenKind::invalid);
86-
bool consumeExpectedToken(ArrayRef<TokenKind> AnyExpected,
87-
unsigned DiagID = diag::err_expected,
88-
TokenKind Context = TokenKind::invalid);
8986

9087
/// Peek if the next token is of the expected kind and if it is then consume
9188
/// it.

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@ using namespace llvm::hlsl::rootsig;
1515
namespace clang {
1616
namespace hlsl {
1717

18-
static std::string FormatTokenKinds(ArrayRef<TokenKind> Kinds) {
19-
std::string TokenString;
20-
llvm::raw_string_ostream Out(TokenString);
21-
bool First = true;
22-
for (auto Kind : Kinds) {
23-
if (!First)
24-
Out << ", ";
25-
switch (Kind) {
26-
#define TOK(X, SPELLING) \
27-
case TokenKind::X: \
28-
Out << SPELLING; \
29-
break;
30-
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
31-
}
32-
First = false;
33-
}
34-
35-
return TokenString;
36-
}
37-
3818
RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
3919
RootSignatureLexer &Lexer,
4020
Preprocessor &PP)
@@ -141,24 +121,18 @@ bool RootSignatureParser::peekExpectedToken(ArrayRef<TokenKind> AnyExpected) {
141121
bool RootSignatureParser::consumeExpectedToken(TokenKind Expected,
142122
unsigned DiagID,
143123
TokenKind Context) {
144-
return consumeExpectedToken(ArrayRef{Expected}, DiagID, Context);
145-
}
146-
147-
bool RootSignatureParser::consumeExpectedToken(ArrayRef<TokenKind> AnyExpected,
148-
unsigned DiagID,
149-
TokenKind Context) {
150-
if (tryConsumeExpectedToken(AnyExpected))
124+
if (tryConsumeExpectedToken(Expected))
151125
return false;
152126

153127
// Report unexpected token kind error
154128
DiagnosticBuilder DB = getDiags().Report(CurToken.TokLoc, DiagID);
155129
switch (DiagID) {
156130
case diag::err_expected:
157-
DB << FormatTokenKinds(AnyExpected);
131+
DB << Expected;
158132
break;
159133
case diag::err_expected_either:
160134
case diag::err_expected_after:
161-
DB << FormatTokenKinds(AnyExpected) << FormatTokenKinds({Context});
135+
DB << Expected << Context;
162136
break;
163137
default:
164138
break;

0 commit comments

Comments
 (0)