|
| 1 | +//=== ParseHLSLRootSignatureTest.cpp - Parse Root Signature tests ---------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/Basic/Diagnostic.h" |
| 10 | +#include "clang/Basic/DiagnosticOptions.h" |
| 11 | +#include "clang/Basic/FileManager.h" |
| 12 | +#include "clang/Basic/LangOptions.h" |
| 13 | +#include "clang/Basic/SourceLocation.h" |
| 14 | +#include "clang/Basic/SourceManager.h" |
| 15 | +#include "clang/Basic/TargetInfo.h" |
| 16 | +#include "clang/Lex/HeaderSearch.h" |
| 17 | +#include "clang/Lex/HeaderSearchOptions.h" |
| 18 | +#include "clang/Lex/Lexer.h" |
| 19 | +#include "clang/Lex/ModuleLoader.h" |
| 20 | +#include "clang/Lex/Preprocessor.h" |
| 21 | +#include "clang/Lex/PreprocessorOptions.h" |
| 22 | + |
| 23 | +#include "clang/Parse/ParseHLSLRootSignature.h" |
| 24 | +#include "gtest/gtest.h" |
| 25 | + |
| 26 | +using namespace clang; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +// Diagnostic helper for helper tests |
| 31 | +class ExpectedDiagConsumer : public DiagnosticConsumer { |
| 32 | + virtual void anchor() {} |
| 33 | + |
| 34 | + void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 35 | + const Diagnostic &Info) override { |
| 36 | + if (!FirstDiag || !ExpectedDiagID.has_value()) { |
| 37 | + Satisfied = false; |
| 38 | + return; |
| 39 | + } |
| 40 | + FirstDiag = false; |
| 41 | + |
| 42 | + Satisfied = ExpectedDiagID.value() == Info.getID(); |
| 43 | + } |
| 44 | + |
| 45 | + bool FirstDiag = true; |
| 46 | + bool Satisfied = false; |
| 47 | + std::optional<unsigned> ExpectedDiagID; |
| 48 | + |
| 49 | +public: |
| 50 | + void SetNoDiag() { |
| 51 | + Satisfied = true; |
| 52 | + ExpectedDiagID = std::nullopt; |
| 53 | + } |
| 54 | + |
| 55 | + void SetExpected(unsigned DiagID) { |
| 56 | + Satisfied = false; |
| 57 | + ExpectedDiagID = DiagID; |
| 58 | + } |
| 59 | + |
| 60 | + bool IsSatisfied() { return Satisfied; } |
| 61 | +}; |
| 62 | + |
| 63 | +// The test fixture. |
| 64 | +class ParseHLSLRootSignatureTest : public ::testing::Test { |
| 65 | +protected: |
| 66 | + ParseHLSLRootSignatureTest() |
| 67 | + : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()), |
| 68 | + Consumer(new ExpectedDiagConsumer()), |
| 69 | + Diags(DiagID, new DiagnosticOptions, Consumer), |
| 70 | + SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) { |
| 71 | + TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; |
| 72 | + Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); |
| 73 | + } |
| 74 | + |
| 75 | + std::unique_ptr<Preprocessor> CreatePP(StringRef Source, |
| 76 | + TrivialModuleLoader &ModLoader) { |
| 77 | + std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 78 | + llvm::MemoryBuffer::getMemBuffer(Source); |
| 79 | + SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); |
| 80 | + |
| 81 | + HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, |
| 82 | + Diags, LangOpts, Target.get()); |
| 83 | + std::unique_ptr<Preprocessor> PP = std::make_unique<Preprocessor>( |
| 84 | + std::make_shared<PreprocessorOptions>(), Diags, LangOpts, SourceMgr, |
| 85 | + HeaderInfo, ModLoader, |
| 86 | + /*IILookup =*/nullptr, |
| 87 | + /*OwnsHeaderSearch =*/false); |
| 88 | + PP->Initialize(*Target); |
| 89 | + PP->EnterMainSourceFile(); |
| 90 | + return PP; |
| 91 | + } |
| 92 | + |
| 93 | + void CheckTokens(SmallVector<hlsl::RootSignatureToken> &Computed, |
| 94 | + SmallVector<hlsl::TokenKind> &Expected) { |
| 95 | + ASSERT_EQ(Computed.size(), Expected.size()); |
| 96 | + for (unsigned I = 0, E = Expected.size(); I != E; ++I) { |
| 97 | + ASSERT_EQ(Computed[I].Kind, Expected[I]); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + FileSystemOptions FileMgrOpts; |
| 102 | + FileManager FileMgr; |
| 103 | + IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
| 104 | + ExpectedDiagConsumer *Consumer; |
| 105 | + DiagnosticsEngine Diags; |
| 106 | + SourceManager SourceMgr; |
| 107 | + LangOptions LangOpts; |
| 108 | + std::shared_ptr<TargetOptions> TargetOpts; |
| 109 | + IntrusiveRefCntPtr<TargetInfo> Target; |
| 110 | +}; |
| 111 | + |
| 112 | +// Valid Lexing Tests |
| 113 | + |
| 114 | +TEST_F(ParseHLSLRootSignatureTest, ValidLexAllTokensTest) { |
| 115 | + // This test will check that we can lex all defined tokens as defined in |
| 116 | + // HLSLRootSignatureTokenKinds.def, plus some additional integer variations |
| 117 | + const llvm::StringLiteral Source = R"cc( |
| 118 | + (),|= |
| 119 | + )cc"; |
| 120 | + |
| 121 | + TrivialModuleLoader ModLoader; |
| 122 | + auto PP = CreatePP(Source, ModLoader); |
| 123 | + auto TokLoc = SourceLocation(); |
| 124 | + |
| 125 | + // Test no diagnostics produced |
| 126 | + Consumer->SetNoDiag(); |
| 127 | + |
| 128 | + hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP); |
| 129 | + |
| 130 | + SmallVector<hlsl::RootSignatureToken> Tokens = { |
| 131 | + hlsl::RootSignatureToken( |
| 132 | + SourceLocation()) // invalid token for completeness |
| 133 | + }; |
| 134 | + ASSERT_FALSE(Lexer.Lex(Tokens)); |
| 135 | + ASSERT_TRUE(Consumer->IsSatisfied()); |
| 136 | + |
| 137 | + SmallVector<hlsl::TokenKind> Expected = { |
| 138 | +#define TOK(NAME) hlsl::TokenKind::NAME, |
| 139 | +#include "clang/Parse/HLSLRootSignatureTokenKinds.def" |
| 140 | + }; |
| 141 | + |
| 142 | + CheckTokens(Tokens, Expected); |
| 143 | +} |
| 144 | + |
| 145 | +// Invalid Lexing Tests |
| 146 | + |
| 147 | +TEST_F(ParseHLSLRootSignatureTest, InvalidLexIdentifierTest) { |
| 148 | + // This test will check that the lexing fails due to no valid token |
| 149 | + const llvm::StringLiteral Source = R"cc( |
| 150 | + notAToken |
| 151 | + )cc"; |
| 152 | + |
| 153 | + TrivialModuleLoader ModLoader; |
| 154 | + auto PP = CreatePP(Source, ModLoader); |
| 155 | + auto TokLoc = SourceLocation(); |
| 156 | + |
| 157 | + // Test correct diagnostic produced |
| 158 | + Consumer->SetExpected(diag::err_hlsl_invalid_token); |
| 159 | + |
| 160 | + hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP); |
| 161 | + |
| 162 | + SmallVector<hlsl::RootSignatureToken> Tokens; |
| 163 | + ASSERT_TRUE(Lexer.Lex(Tokens)); |
| 164 | + ASSERT_TRUE(Consumer->IsSatisfied()); |
| 165 | +} |
| 166 | + |
| 167 | +} // anonymous namespace |
0 commit comments