Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 8 | #include "include/private/SkSLIRNode.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 9 | #include "include/sksl/DSL.h" |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrDirectContextPriv.h" |
| 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/sksl/SkSLIRGenerator.h" |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 13 | #include "src/sksl/dsl/priv/DSLWriter.h" |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 14 | |
| 15 | #include "tests/Test.h" |
| 16 | |
| 17 | #include <limits> |
| 18 | |
| 19 | using namespace SkSL::dsl; |
| 20 | |
| 21 | #if defined(__GNUC__) || defined(__clang__) |
| 22 | |
| 23 | class ExpectErrorLineNumber : public ErrorHandler { |
| 24 | public: |
| 25 | ExpectErrorLineNumber(skiatest::Reporter* reporter, const char* msg, int line) |
| 26 | : fMsg(msg) |
| 27 | , fLine(line) |
| 28 | , fReporter(reporter) { |
| 29 | SetErrorHandler(this); |
| 30 | } |
| 31 | |
| 32 | ~ExpectErrorLineNumber() override { |
| 33 | REPORTER_ASSERT(fReporter, !fMsg); |
| 34 | SetErrorHandler(nullptr); |
| 35 | } |
| 36 | |
Ethan Nicholas | a40ddcd | 2021-08-06 09:17:18 -0400 | [diff] [blame] | 37 | void handleError(const char* msg, PositionInfo pos) override { |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 38 | REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg), |
| 39 | "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg); |
Ethan Nicholas | a40ddcd | 2021-08-06 09:17:18 -0400 | [diff] [blame] | 40 | REPORTER_ASSERT(fReporter, pos.line() == fLine, |
| 41 | "Line number mismatch: expected %d, but received %d\n", fLine, pos.line()); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 42 | fMsg = nullptr; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | const char* fMsg; |
| 47 | int fLine; |
| 48 | skiatest::Reporter* fReporter; |
| 49 | }; |
| 50 | |
| 51 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers, r, ctxInfo) { |
| 52 | Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler()); |
| 53 | { |
| 54 | ExpectErrorLineNumber error(r, |
| 55 | "error: type mismatch: '+' cannot operate on 'float', 'bool'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 56 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 57 | (Float(1) + true).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 61 | Var a(kBool_Type); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 62 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 63 | ExpectErrorLineNumber error(r, |
| 64 | "error: type mismatch: '=' cannot operate on 'bool', 'float'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 65 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 66 | (a = 5.0f).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 70 | Var a(Array(kInt_Type, 5)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 71 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 72 | ExpectErrorLineNumber error(r, |
| 73 | "error: expected 'int', but found 'bool'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 74 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 75 | (a[true]).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 79 | Var a(Array(kInt_Type, 5)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 80 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 81 | ExpectErrorLineNumber error(r, |
| 82 | "error: '++' cannot operate on 'int[5]'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 83 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 84 | (++a).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | { |
| 88 | ExpectErrorLineNumber error(r, |
| 89 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 90 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 91 | Do(Discard(), 5).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | { |
| 95 | ExpectErrorLineNumber error(r, |
| 96 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 97 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 98 | For(DSLStatement(), 5, DSLExpression(), Block()).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | { |
| 102 | ExpectErrorLineNumber error(r, |
| 103 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 104 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 105 | If(5, Discard()).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | { |
| 109 | ExpectErrorLineNumber error(r, |
| 110 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 111 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 112 | While(5, Discard()).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | { |
| 116 | ExpectErrorLineNumber error(r, |
| 117 | "error: no match for abs(bool)\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 118 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 119 | Abs(true).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 120 | } |
| 121 | End(); |
| 122 | } |
| 123 | |
| 124 | #endif // defined(__GNUC__) || defined(__clang__) |