• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "gmock/gmock.h"
16 #include "src/program.h"
17 
18 #if TINT_BUILD_SPV_READER
19 #include "src/reader/spirv/parser_impl_test_helper.h"
20 #endif
21 
22 #if TINT_BUILD_WGSL_WRITER
23 #include "src/writer/wgsl/generator.h"
24 #endif
25 
26 namespace {
27 
TintInternalCompilerErrorReporter(const tint::diag::List & diagnostics)28 void TintInternalCompilerErrorReporter(const tint::diag::List& diagnostics) {
29   FAIL() << diagnostics.str();
30 }
31 
32 struct Flags {
33   bool spirv_reader_dump_converted = false;
34 
parse__anon6404f1b40111::Flags35   bool parse(int argc, char** argv) {
36     bool errored = false;
37     for (int i = 1; i < argc && !errored; i++) {
38       auto match = [&](std::string name) { return name == argv[i]; };
39 
40       if (match("--dump-spirv")) {
41         spirv_reader_dump_converted = true;
42       } else {
43         std::cout << "Unknown flag '" << argv[i] << "'" << std::endl;
44         return false;
45       }
46     }
47     return true;
48   }
49 };
50 
51 }  // namespace
52 
53 // Entry point for tint unit tests
main(int argc,char ** argv)54 int main(int argc, char** argv) {
55   testing::InitGoogleMock(&argc, argv);
56 
57 #if TINT_BUILD_WGSL_WRITER
58   tint::Program::printer = [](const tint::Program* program) {
59     auto result = tint::writer::wgsl::Generate(program, {});
60     if (!result.error.empty()) {
61       return "error: " + result.error;
62     }
63     return result.wgsl;
64   };
65 #endif  //  TINT_BUILD_WGSL_WRITER
66 
67   Flags flags;
68   if (!flags.parse(argc, argv)) {
69     return -1;
70   }
71 
72 #if TINT_BUILD_SPV_READER
73   if (flags.spirv_reader_dump_converted) {
74     tint::reader::spirv::test::DumpSuccessfullyConvertedSpirv();
75   }
76 #endif  // TINT_BUILD_SPV_READER
77 
78   tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
79 
80   auto res = RUN_ALL_TESTS();
81 
82   return res;
83 }
84