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