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 | |
| 37 | void handleError(const char* msg, PositionInfo* pos) override { |
| 38 | REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg), |
| 39 | "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg); |
| 40 | REPORTER_ASSERT(fReporter, pos); |
| 41 | REPORTER_ASSERT(fReporter, pos->line() == fLine, |
| 42 | "Line number mismatch: expected %d, but received %d\n", fLine, pos->line()); |
| 43 | fMsg = nullptr; |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | const char* fMsg; |
| 48 | int fLine; |
| 49 | skiatest::Reporter* fReporter; |
| 50 | }; |
| 51 | |
| 52 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers, r, ctxInfo) { |
| 53 | Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler()); |
| 54 | { |
| 55 | ExpectErrorLineNumber error(r, |
| 56 | "error: type mismatch: '+' cannot operate on 'float', 'bool'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 57 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 58 | DSLExpression x = (Float(1) + true); |
| 59 | } |
| 60 | |
| 61 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 62 | Var a(kBool_Type); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 63 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 64 | ExpectErrorLineNumber error(r, |
| 65 | "error: type mismatch: '=' cannot operate on 'bool', 'float'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 66 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 67 | DSLExpression x = (a = 5.0f); |
| 68 | } |
| 69 | |
| 70 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 71 | Var a(Array(kInt_Type, 5)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 72 | DSLWriter::MarkDeclared(a); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 73 | ExpectErrorLineNumber error(r, |
| 74 | "error: expected 'int', but found 'bool'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 75 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 76 | DSLExpression x = (a[true]); |
| 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 | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 82 | ExpectErrorLineNumber error(r, |
| 83 | "error: '++' cannot operate on 'int[5]'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 84 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 85 | DSLExpression x = ++a; |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | ExpectErrorLineNumber error(r, |
| 90 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 91 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 92 | DSLStatement x = Do(Discard(), 5); |
| 93 | } |
| 94 | |
| 95 | { |
| 96 | ExpectErrorLineNumber error(r, |
| 97 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 98 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 99 | DSLStatement x = For(DSLStatement(), 5, DSLExpression(), DSLStatement()); |
| 100 | } |
| 101 | |
| 102 | { |
| 103 | ExpectErrorLineNumber error(r, |
| 104 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 105 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 106 | DSLStatement x = If(5, Discard()); |
| 107 | } |
| 108 | |
| 109 | { |
| 110 | ExpectErrorLineNumber error(r, |
| 111 | "error: expected 'bool', but found 'int'\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 112 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 113 | DSLStatement x = While(5, Discard()); |
| 114 | } |
| 115 | |
| 116 | { |
| 117 | ExpectErrorLineNumber error(r, |
| 118 | "error: no match for abs(bool)\n", |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 119 | __LINE__ + 1); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 120 | DSLStatement x = Abs(true); |
| 121 | } |
| 122 | End(); |
| 123 | } |
| 124 | |
| 125 | #endif // defined(__GNUC__) || defined(__clang__) |