blob: f73a938e0d7de2e1879953963d1971acf7a7cde7 [file] [log] [blame]
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001/*
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 Nicholas24c17722021-03-09 13:10:59 -05008#include "include/private/SkSLIRNode.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -05009#include "include/sksl/DSL.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050010#include "src/gpu/GrDirectContextPriv.h"
11#include "src/gpu/GrGpu.h"
12#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050013#include "src/sksl/dsl/priv/DSLWriter.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050014
15#include "tests/Test.h"
16
17#include <limits>
18
19using namespace SkSL::dsl;
20
21#if defined(__GNUC__) || defined(__clang__)
22
23class ExpectErrorLineNumber : public ErrorHandler {
24public:
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
46private:
47 const char* fMsg;
48 int fLine;
49 skiatest::Reporter* fReporter;
50};
51
52DEF_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 Nicholas961d9442021-03-16 16:37:29 -040057 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050058 DSLExpression x = (Float(1) + true);
59 }
60
61 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040062 Var a(kBool_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -040063 DSLWriter::MarkDeclared(a);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050064 ExpectErrorLineNumber error(r,
65 "error: type mismatch: '=' cannot operate on 'bool', 'float'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040066 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050067 DSLExpression x = (a = 5.0f);
68 }
69
70 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040071 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040072 DSLWriter::MarkDeclared(a);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050073 ExpectErrorLineNumber error(r,
74 "error: expected 'int', but found 'bool'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040075 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050076 DSLExpression x = (a[true]);
77 }
78
79 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040080 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040081 DSLWriter::MarkDeclared(a);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050082 ExpectErrorLineNumber error(r,
83 "error: '++' cannot operate on 'int[5]'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040084 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050085 DSLExpression x = ++a;
86 }
87
88 {
89 ExpectErrorLineNumber error(r,
90 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040091 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050092 DSLStatement x = Do(Discard(), 5);
93 }
94
95 {
96 ExpectErrorLineNumber error(r,
97 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040098 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050099 DSLStatement x = For(DSLStatement(), 5, DSLExpression(), DSLStatement());
100 }
101
102 {
103 ExpectErrorLineNumber error(r,
104 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -0400105 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500106 DSLStatement x = If(5, Discard());
107 }
108
109 {
110 ExpectErrorLineNumber error(r,
111 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -0400112 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500113 DSLStatement x = While(5, Discard());
114 }
115
116 {
117 ExpectErrorLineNumber error(r,
118 "error: no match for abs(bool)\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -0400119 __LINE__ + 1);
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500120 DSLStatement x = Abs(true);
121 }
122 End();
123}
124
125#endif // defined(__GNUC__) || defined(__clang__)