Skip to content

Commit 123b4ac

Browse files
committed
add diagnostics to methods
1 parent 03c5dfe commit 123b4ac

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,4 +1806,7 @@ def ext_hlsl_access_specifiers : ExtWarn<
18061806
def err_hlsl_unsupported_component : Error<"invalid component '%0' used; expected 'x', 'y', 'z', or 'w'">;
18071807
def err_hlsl_packoffset_invalid_reg : Error<"invalid resource class specifier '%0' for packoffset, expected 'c'">;
18081808

1809+
// HLSL Root Signature Parser Diagnostics
1810+
def err_hlsl_rootsig_unexpected_eos : Error<"unexpected end to token stream">;
1811+
def err_hlsl_rootsig_unexpected_token_kind : Error<"expected the %select{following|one of the following}0 token kinds '%1'">;
18091812
} // end of Parser diagnostics

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "clang/AST/APValue.h"
1717
#include "clang/Basic/DiagnosticLex.h"
18+
#include "clang/Basic/DiagnosticParse.h"
1819
#include "clang/Lex/LiteralSupport.h"
1920
#include "clang/Lex/Preprocessor.h"
2021

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
#include "clang/Parse/ParseHLSLRootSignature.h"
22

3+
#include "llvm/Support/raw_ostream.h"
4+
35
using namespace llvm::hlsl::root_signature;
46

57
namespace clang {
68
namespace hlsl {
79

10+
// Helper definitions
11+
12+
static std::string FormatTokenKinds(ArrayRef<TokenKind> Kinds) {
13+
std::string TokenString;
14+
llvm::raw_string_ostream Out(TokenString);
15+
bool First = true;
16+
for (auto Kind : Kinds) {
17+
if (!First)
18+
Out << ", ";
19+
switch (Kind) {
20+
case TokenKind::invalid:
21+
break;
22+
case TokenKind::int_literal:
23+
Out << "integer literal";
24+
break;
25+
case TokenKind::bReg:
26+
Out << "b register";
27+
break;
28+
case TokenKind::tReg:
29+
Out << "t register";
30+
break;
31+
case TokenKind::uReg:
32+
Out << "u register";
33+
break;
34+
case TokenKind::sReg:
35+
Out << "s register";
36+
break;
37+
#define PUNCTUATOR(X, Y) \
38+
case TokenKind::pu_##X: \
39+
Out << #Y; \
40+
break;
41+
#define KEYWORD(NAME) \
42+
case TokenKind::kw_##NAME: \
43+
Out << #NAME; \
44+
break;
45+
#define ENUM(NAME, LIT) \
46+
case TokenKind::en_##NAME: \
47+
Out << LIT; \
48+
break;
49+
#include "clang/Parse/HLSLRootSignatureTokenKinds.def"
50+
}
51+
First = false;
52+
}
53+
54+
return TokenString;
55+
}
56+
857
// Lexer Definitions
958

1059
static bool IsNumberChar(char C) {
@@ -230,12 +279,13 @@ bool RootSignatureParser::ParseDescriptorTable() {
230279
}
231280

232281
return true;
233-
234282
}
235283

236284
bool RootSignatureParser::ConsumeNextToken() {
237285
CurTok++;
238286
if (LastTok == CurTok) {
287+
// Report unexpected end of tokens error
288+
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_unexpected_eos);
239289
return true;
240290
}
241291
return false;
@@ -257,6 +307,9 @@ bool RootSignatureParser::EnsureExpectedToken(ArrayRef<TokenKind> AnyExpected) {
257307
if (IsExpectedToken(CurTok->Kind, AnyExpected))
258308
return false;
259309

310+
// Report unexpected token kind error
311+
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_unexpected_token_kind)
312+
<< (unsigned)(AnyExpected.size() != 1) << FormatTokenKinds(AnyExpected);
260313
return true;
261314
}
262315

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,56 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
331331
// Test generated DescriptorTable start has correct default values
332332
ASSERT_TRUE(std::holds_alternative<DescriptorTable>(Elem));
333333
ASSERT_EQ(std::get<DescriptorTable>(Elem).NumClauses, (uint32_t)0);
334+
ASSERT_TRUE(Consumer->IsSatisfied());
335+
}
336+
337+
// Invalid Parser Tests
338+
339+
TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedEOSTest) {
340+
const llvm::StringLiteral Source = R"cc(
341+
DescriptorTable
342+
)cc";
343+
344+
TrivialModuleLoader ModLoader;
345+
auto PP = CreatePP(Source, ModLoader);
346+
auto TokLoc = SourceLocation();
347+
348+
// Test correct diagnostic produced
349+
Consumer->SetExpected(diag::err_hlsl_rootsig_unexpected_eos);
350+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
351+
352+
SmallVector<hlsl::RootSignatureToken> Tokens;
353+
ASSERT_FALSE(Lexer.Lex(Tokens));
354+
355+
SmallVector<RootElement> Elements;
356+
hlsl::RootSignatureParser Parser(Elements, Tokens, Diags);
357+
358+
ASSERT_TRUE(Parser.Parse());
359+
360+
ASSERT_TRUE(Consumer->IsSatisfied());
361+
}
362+
363+
TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedTokenTest) {
364+
const llvm::StringLiteral Source = R"cc(
365+
DescriptorTable()
366+
space
367+
)cc";
368+
369+
TrivialModuleLoader ModLoader;
370+
auto PP = CreatePP(Source, ModLoader);
371+
auto TokLoc = SourceLocation();
372+
373+
// Test correct diagnostic produced
374+
Consumer->SetExpected(diag::err_hlsl_rootsig_unexpected_token_kind);
375+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
376+
377+
SmallVector<hlsl::RootSignatureToken> Tokens;
378+
ASSERT_FALSE(Lexer.Lex(Tokens));
379+
380+
SmallVector<RootElement> Elements;
381+
hlsl::RootSignatureParser Parser(Elements, Tokens, Diags);
382+
383+
ASSERT_TRUE(Parser.Parse());
334384

335385
ASSERT_TRUE(Consumer->IsSatisfied());
336386
}

0 commit comments

Comments
 (0)