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" |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLCompiler.h" |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 13 | #include "src/sksl/SkSLIRGenerator.h" |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 14 | #include "src/sksl/dsl/priv/DSLWriter.h" |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 15 | |
| 16 | #include "tests/Test.h" |
| 17 | |
| 18 | #include <limits> |
| 19 | |
| 20 | using namespace SkSL::dsl; |
| 21 | |
| 22 | #if defined(__GNUC__) || defined(__clang__) |
| 23 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 24 | class ExpectErrorLineNumber : public SkSL::ErrorReporter { |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 25 | public: |
| 26 | ExpectErrorLineNumber(skiatest::Reporter* reporter, const char* msg, int line) |
| 27 | : fMsg(msg) |
| 28 | , fLine(line) |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 29 | , fReporter(reporter) |
| 30 | , fOldReporter(&GetErrorReporter()) { |
| 31 | SetErrorReporter(this); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | ~ExpectErrorLineNumber() override { |
| 35 | REPORTER_ASSERT(fReporter, !fMsg); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 36 | SetErrorReporter(fOldReporter); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 39 | void handleError(skstd::string_view msg, SkSL::PositionInfo pos) override { |
| 40 | REPORTER_ASSERT(fReporter, msg == fMsg, |
| 41 | "Error mismatch: expected:\n%sbut received:\n%.*s", fMsg, (int)msg.length(), |
| 42 | msg.data()); |
Ethan Nicholas | a40ddcd | 2021-08-06 09:17:18 -0400 | [diff] [blame] | 43 | REPORTER_ASSERT(fReporter, pos.line() == fLine, |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 44 | "Line number mismatch: expected %d, but received %d\n", fLine, pos.line()); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 45 | DSLWriter::Compiler().handleError(msg, pos); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 46 | fMsg = nullptr; |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | const char* fMsg; |
| 51 | int fLine; |
| 52 | skiatest::Reporter* fReporter; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 53 | ErrorReporter* fOldReporter; |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers, r, ctxInfo) { |
| 57 | Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler()); |
| 58 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 59 | ExpectErrorLineNumber error(r, "type mismatch: '+' cannot operate on 'float', 'bool'", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 60 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 61 | (Float(1) + true).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 65 | Var a(kBool_Type); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 66 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 67 | ExpectErrorLineNumber error(r, "type mismatch: '=' cannot operate on 'bool', 'float'", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 68 | __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 69 | (a = 5.0f).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 73 | Var a(Array(kInt_Type, 5)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 74 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 75 | ExpectErrorLineNumber error(r, "expected 'int', but found 'bool'", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 76 | (a[true]).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 80 | Var a(Array(kInt_Type, 5)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 81 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 82 | ExpectErrorLineNumber error(r, "'++' cannot operate on 'int[5]'", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 83 | (++a).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 87 | ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 88 | Do(Discard(), 5).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 92 | ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 93 | For(DSLStatement(), 5, DSLExpression(), Block()).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 97 | ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 98 | If(5, Discard()).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 102 | ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 103 | While(5, Discard()).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 107 | ExpectErrorLineNumber error(r, "no match for abs(bool)", __LINE__ + 1); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 108 | Abs(true).release(); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 109 | } |
| 110 | End(); |
| 111 | } |
| 112 | |
| 113 | #endif // defined(__GNUC__) || defined(__clang__) |