blob: ecd7c4a0248a8009e67a9e785a5dd5a48163a877 [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
Ethan Nicholasa40ddcd2021-08-06 09:17:18 -040037 void handleError(const char* msg, PositionInfo pos) override {
Ethan Nicholas34c7e112021-02-25 20:50:32 -050038 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
39 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
Ethan Nicholasa40ddcd2021-08-06 09:17:18 -040040 REPORTER_ASSERT(fReporter, pos.line() == fLine,
41 "Line number mismatch: expected %d, but received %d\n", fLine, pos.line());
Ethan Nicholas34c7e112021-02-25 20:50:32 -050042 fMsg = nullptr;
43 }
44
45private:
46 const char* fMsg;
47 int fLine;
48 skiatest::Reporter* fReporter;
49};
50
51DEF_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 Nicholas961d9442021-03-16 16:37:29 -040056 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040057 (Float(1) + true).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050058 }
59
60 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040061 Var a(kBool_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -040062 DSLWriter::MarkDeclared(a);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050063 ExpectErrorLineNumber error(r,
64 "error: type mismatch: '=' cannot operate on 'bool', 'float'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040065 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040066 (a = 5.0f).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050067 }
68
69 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040070 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040071 DSLWriter::MarkDeclared(a);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050072 ExpectErrorLineNumber error(r,
73 "error: expected 'int', but found 'bool'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040074 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040075 (a[true]).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050076 }
77
78 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040079 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040080 DSLWriter::MarkDeclared(a);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050081 ExpectErrorLineNumber error(r,
82 "error: '++' cannot operate on 'int[5]'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040083 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040084 (++a).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050085 }
86
87 {
88 ExpectErrorLineNumber error(r,
89 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040090 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040091 Do(Discard(), 5).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050092 }
93
94 {
95 ExpectErrorLineNumber error(r,
96 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -040097 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040098 For(DSLStatement(), 5, DSLExpression(), Block()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050099 }
100
101 {
102 ExpectErrorLineNumber error(r,
103 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -0400104 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400105 If(5, Discard()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500106 }
107
108 {
109 ExpectErrorLineNumber error(r,
110 "error: expected 'bool', but found 'int'\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -0400111 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400112 While(5, Discard()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500113 }
114
115 {
116 ExpectErrorLineNumber error(r,
117 "error: no match for abs(bool)\n",
Ethan Nicholas961d9442021-03-16 16:37:29 -0400118 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400119 Abs(true).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500120 }
121 End();
122}
123
124#endif // defined(__GNUC__) || defined(__clang__)