DxcTestUtils.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcTestUtils.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // This file provides utility functions for testing dxc APIs. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include <string>
  13. #include <vector>
  14. #include "dxc/dxcapi.h"
  15. #include "dxc/Support/dxcapi.use.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. namespace hlsl {
  18. namespace options {
  19. class DxcOpts;
  20. class MainArgs;
  21. }
  22. }
  23. /// Use this class to run a FileCheck invocation in memory.
  24. class FileCheckForTest {
  25. public:
  26. FileCheckForTest();
  27. std::string CheckFilename;
  28. /// File to check (defaults to stdin)
  29. std::string InputFilename;
  30. /// Prefix to use from check file (defaults to 'CHECK')
  31. std::vector<std::string> CheckPrefixes;
  32. /// Do not treat all horizontal whitespace as equivalent
  33. bool NoCanonicalizeWhiteSpace;
  34. /// Add an implicit negative check with this pattern to every
  35. /// positive check. This can be used to ensure that no instances of
  36. /// this pattern occur which are not matched by a positive pattern
  37. std::vector<std::string> ImplicitCheckNot;
  38. /// Allow the input file to be empty. This is useful when making
  39. /// checks that some error message does not occur, for example.
  40. bool AllowEmptyInput;
  41. /// String to read in place of standard input.
  42. std::string InputForStdin;
  43. /// Output stream.
  44. std::string test_outs;
  45. /// Output stream.
  46. std::string test_errs;
  47. int Run();
  48. };
  49. class FileRunCommandPart {
  50. private:
  51. void RunFileChecker(const FileRunCommandPart *Prior);
  52. void RunStdErrChecker(const FileRunCommandPart *Prior);
  53. void RunDxc(const FileRunCommandPart *Prior);
  54. void RunDxv(const FileRunCommandPart *Prior);
  55. void RunOpt(const FileRunCommandPart *Prior);
  56. void RunD3DReflect(const FileRunCommandPart *Prior);
  57. void RunTee(const FileRunCommandPart *Prior);
  58. void RunXFail(const FileRunCommandPart *Prior);
  59. public:
  60. FileRunCommandPart(const FileRunCommandPart&) = default;
  61. FileRunCommandPart(const std::string &command, const std::string &arguments, LPCWSTR commandFileName);
  62. FileRunCommandPart(FileRunCommandPart && other);
  63. void Run(const FileRunCommandPart *Prior);
  64. void ReadOptsForDxc(hlsl::options::MainArgs &argStrings, hlsl::options::DxcOpts &Opts);
  65. std::string Command; // Command to run, eg %dxc
  66. std::string Arguments; // Arguments to command
  67. LPCWSTR CommandFileName; // File name replacement for %s
  68. dxc::DxcDllSupport *DllSupport; // DLL support to use for Run().
  69. // These fields are set after an invocation to Run().
  70. CComPtr<IDxcOperationResult> OpResult; // The operation result, if any.
  71. int RunResult; // The exit code for the operation.
  72. std::string StdOut; // Standard output text.
  73. std::string StdErr; // Standard error text.
  74. };
  75. void ParseCommandParts(LPCSTR commands, LPCWSTR fileName, std::vector<FileRunCommandPart> &parts);
  76. void ParseCommandPartsFromFile(LPCWSTR fileName, std::vector<FileRunCommandPart> &parts);
  77. class FileRunTestResult {
  78. public:
  79. std::string ErrorMessage;
  80. int RunResult;
  81. static FileRunTestResult RunFromFileCommands(LPCWSTR fileName);
  82. static FileRunTestResult RunFromFileCommands(LPCWSTR fileName, dxc::DxcDllSupport &dllSupport);
  83. };
  84. inline std::string BlobToUtf8(_In_ IDxcBlob *pBlob) {
  85. if (pBlob == nullptr)
  86. return std::string();
  87. return std::string((char *)pBlob->GetBufferPointer(), pBlob->GetBufferSize());
  88. }
  89. void AssembleToContainer(dxc::DxcDllSupport &dllSupport, IDxcBlob *pModule, IDxcBlob **pContainer);
  90. std::wstring BlobToUtf16(_In_ IDxcBlob *pBlob);
  91. void CheckOperationSucceeded(IDxcOperationResult *pResult, IDxcBlob **ppBlob);
  92. bool CheckOperationResultMsgs(IDxcOperationResult *pResult,
  93. llvm::ArrayRef<LPCSTR> pErrorMsgs,
  94. bool maySucceedAnyway, bool bRegex);
  95. bool CheckOperationResultMsgs(IDxcOperationResult *pResult,
  96. const LPCSTR *pErrorMsgs, size_t errorMsgCount,
  97. bool maySucceedAnyway, bool bRegex);
  98. bool CheckMsgs(const LPCSTR pText, size_t TextCount, const LPCSTR *pErrorMsgs,
  99. size_t errorMsgCount, bool bRegex);
  100. bool CheckNotMsgs(const LPCSTR pText, size_t TextCount, const LPCSTR *pErrorMsgs,
  101. size_t errorMsgCount, bool bRegex);
  102. void GetDxilPart(dxc::DxcDllSupport &dllSupport, IDxcBlob *pProgram, IDxcBlob **pDxilPart);
  103. std::string DisassembleProgram(dxc::DxcDllSupport &dllSupport, IDxcBlob *pProgram);
  104. void SplitPassList(LPWSTR pPassesBuffer, std::vector<LPCWSTR> &passes);
  105. void MultiByteStringToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, UINT32 codePoint, _Outptr_ IDxcBlob **ppBlob);
  106. void MultiByteStringToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, UINT32 codePoint, _Outptr_ IDxcBlobEncoding **ppBlob);
  107. void Utf8ToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, _Outptr_ IDxcBlob **ppBlob);
  108. void Utf8ToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, _Outptr_ IDxcBlobEncoding **ppBlob);
  109. void Utf8ToBlob(dxc::DxcDllSupport &dllSupport, const char *pVal, _Outptr_ IDxcBlobEncoding **ppBlob);
  110. void Utf16ToBlob(dxc::DxcDllSupport &dllSupport, const std::wstring &val, _Outptr_ IDxcBlob **ppBlob);
  111. void Utf16ToBlob(dxc::DxcDllSupport &dllSupport, const std::wstring &val, _Outptr_ IDxcBlobEncoding **ppBlob);
  112. void VerifyCompileOK(dxc::DxcDllSupport &dllSupport, LPCSTR pText,
  113. LPWSTR pTargetProfile, LPCWSTR pArgs,
  114. _Outptr_ IDxcBlob **ppResult);
  115. void VerifyCompileOK(dxc::DxcDllSupport &dllSupport, LPCSTR pText,
  116. LPWSTR pTargetProfile, std::vector<LPCWSTR> &args,
  117. _Outptr_ IDxcBlob **ppResult);
  118. class VersionSupportInfo {
  119. private:
  120. bool m_CompilerIsDebugBuild;
  121. public:
  122. bool m_InternalValidator;
  123. unsigned m_DxilMajor, m_DxilMinor;
  124. unsigned m_ValMajor, m_ValMinor;
  125. VersionSupportInfo();
  126. // Initialize version info structure. TODO: add device shader model support
  127. void Initialize(dxc::DxcDllSupport &dllSupport);
  128. // Return true if IR sensitive test should be skipped, and log comment
  129. bool SkipIRSensitiveTest();
  130. // Return true if test requiring DXIL of given version should be skipped, and log comment
  131. bool SkipDxilVersion(unsigned major, unsigned minor);
  132. // Return true if out-of-memory test should be skipped, and log comment
  133. bool SkipOutOfMemoryTest();
  134. };