Skip to content

Commit 457acaa

Browse files
committed
add diagnostics to methods
1 parent d230139 commit 457acaa

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,4 +1806,6 @@ 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_token_kind : Error<"expected %select{the following|one of the following}0 token kind%select{|s}0: %1">;
18091811
} // 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: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
#include "clang/Parse/ParseHLSLRootSignature.h"
22

3+
#include "llvm/Support/raw_ostream.h"
4+
35
using namespace llvm::hlsl::rootsig;
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::error:
21+
break;
22+
case TokenKind::invalid:
23+
Out << "invalid identifier";
24+
break;
25+
case TokenKind::end_of_stream:
26+
Out << "end of stream";
27+
break;
28+
case TokenKind::int_literal:
29+
Out << "integer literal";
30+
break;
31+
case TokenKind::bReg:
32+
Out << "b register";
33+
break;
34+
case TokenKind::tReg:
35+
Out << "t register";
36+
break;
37+
case TokenKind::uReg:
38+
Out << "u register";
39+
break;
40+
case TokenKind::sReg:
41+
Out << "s register";
42+
break;
43+
#define PUNCTUATOR(X, Y) \
44+
case TokenKind::pu_##X: \
45+
Out << #Y; \
46+
break;
47+
#define KEYWORD(NAME) \
48+
case TokenKind::kw_##NAME: \
49+
Out << #NAME; \
50+
break;
51+
#define ENUM(NAME, LIT) \
52+
case TokenKind::en_##NAME: \
53+
Out << LIT; \
54+
break;
55+
#include "clang/Parse/HLSLRootSignatureTokenKinds.def"
56+
}
57+
First = false;
58+
}
59+
60+
return TokenString;
61+
}
62+
863
// Lexer Definitions
964

1065
static bool IsNumberChar(char C) {
@@ -220,7 +275,6 @@ bool RootSignatureParser::ParseDescriptorTable() {
220275
}
221276

222277
return true;
223-
224278
}
225279

226280
// Is given token one of the expected kinds
@@ -239,6 +293,9 @@ bool RootSignatureParser::EnsureExpectedToken(ArrayRef<TokenKind> AnyExpected) {
239293
if (IsExpectedToken(CurToken.Kind, AnyExpected))
240294
return false;
241295

296+
// Report unexpected token kind error
297+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_unexpected_token_kind)
298+
<< (unsigned)(AnyExpected.size() != 1) << FormatTokenKinds(AnyExpected);
242299
return true;
243300
}
244301

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,68 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
357357
// Test generated DescriptorTable start has correct default values
358358
ASSERT_TRUE(std::holds_alternative<DescriptorTable>(Elem));
359359
ASSERT_EQ(std::get<DescriptorTable>(Elem).NumClauses, (uint32_t)0);
360+
ASSERT_TRUE(Consumer->IsSatisfied());
361+
}
362+
363+
// Invalid Parser Tests
364+
365+
TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedTokenTest) {
366+
const llvm::StringLiteral Source = R"cc(
367+
DescriptorTable()
368+
space
369+
)cc";
370+
371+
TrivialModuleLoader ModLoader;
372+
auto PP = CreatePP(Source, ModLoader);
373+
auto TokLoc = SourceLocation();
374+
375+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
376+
SmallVector<RootElement> Elements;
377+
hlsl::RootSignatureParser Parser(Elements, Lexer, Diags);
378+
379+
// Test correct diagnostic produced
380+
Consumer->SetExpected(diag::err_hlsl_rootsig_unexpected_token_kind);
381+
ASSERT_TRUE(Parser.Parse());
382+
383+
ASSERT_TRUE(Consumer->IsSatisfied());
384+
}
385+
386+
TEST_F(ParseHLSLRootSignatureTest, InvalidParseInvalidTokenTest) {
387+
const llvm::StringLiteral Source = R"cc(
388+
notAnIdentifier
389+
)cc";
390+
391+
TrivialModuleLoader ModLoader;
392+
auto PP = CreatePP(Source, ModLoader);
393+
auto TokLoc = SourceLocation();
394+
395+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
396+
SmallVector<RootElement> Elements;
397+
hlsl::RootSignatureParser Parser(Elements, Lexer, Diags);
398+
399+
// Test correct diagnostic produced - invalid token
400+
Consumer->SetExpected(diag::err_hlsl_rootsig_unexpected_token_kind);
401+
ASSERT_TRUE(Parser.Parse());
402+
403+
ASSERT_TRUE(Consumer->IsSatisfied());
404+
}
405+
406+
TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedEndOfStreamTest) {
407+
const llvm::StringLiteral Source = R"cc(
408+
DescriptorTable(
409+
)cc";
410+
411+
TrivialModuleLoader ModLoader;
412+
auto PP = CreatePP(Source, ModLoader);
413+
auto TokLoc = SourceLocation();
414+
415+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
416+
SmallVector<RootElement> Elements;
417+
hlsl::RootSignatureParser Parser(Elements, Lexer, Diags);
418+
419+
// Test correct diagnostic produced - end of stream
420+
Consumer->SetExpected(diag::err_hlsl_rootsig_unexpected_token_kind);
421+
ASSERT_TRUE(Parser.Parse());
360422

361423
ASSERT_TRUE(Consumer->IsSatisfied());
362424
}

0 commit comments

Comments
 (0)