blob: 8375334ed212ecbab3171a14b52eb0311c9dfb21 [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"
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040012#include "src/sksl/SkSLCompiler.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050013#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050014#include "src/sksl/dsl/priv/DSLWriter.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050015
16#include "tests/Test.h"
17
18#include <limits>
19
20using namespace SkSL::dsl;
21
22#if defined(__GNUC__) || defined(__clang__)
23
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040024class ExpectErrorLineNumber : public SkSL::ErrorReporter {
Ethan Nicholas34c7e112021-02-25 20:50:32 -050025public:
26 ExpectErrorLineNumber(skiatest::Reporter* reporter, const char* msg, int line)
27 : fMsg(msg)
28 , fLine(line)
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040029 , fReporter(reporter)
30 , fOldReporter(&GetErrorReporter()) {
31 SetErrorReporter(this);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050032 }
33
34 ~ExpectErrorLineNumber() override {
35 REPORTER_ASSERT(fReporter, !fMsg);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040036 SetErrorReporter(fOldReporter);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050037 }
38
Ethan Nicholas32724122021-09-07 13:49:07 -040039 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 Nicholasa40ddcd2021-08-06 09:17:18 -040043 REPORTER_ASSERT(fReporter, pos.line() == fLine,
Ethan Nicholas32724122021-09-07 13:49:07 -040044 "Line number mismatch: expected %d, but received %d\n", fLine, pos.line());
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040045 DSLWriter::Compiler().handleError(msg, pos);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050046 fMsg = nullptr;
47 }
48
49private:
50 const char* fMsg;
51 int fLine;
52 skiatest::Reporter* fReporter;
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040053 ErrorReporter* fOldReporter;
Ethan Nicholas34c7e112021-02-25 20:50:32 -050054};
55
56DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers, r, ctxInfo) {
57 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
58 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040059 ExpectErrorLineNumber error(r, "type mismatch: '+' cannot operate on 'float', 'bool'",
Ethan Nicholas961d9442021-03-16 16:37:29 -040060 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040061 (Float(1) + true).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050062 }
63
64 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040065 Var a(kBool_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -040066 DSLWriter::MarkDeclared(a);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040067 ExpectErrorLineNumber error(r, "type mismatch: '=' cannot operate on 'bool', 'float'",
Ethan Nicholas961d9442021-03-16 16:37:29 -040068 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040069 (a = 5.0f).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050070 }
71
72 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040073 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040074 DSLWriter::MarkDeclared(a);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040075 ExpectErrorLineNumber error(r, "expected 'int', but found 'bool'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040076 (a[true]).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050077 }
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 Nicholas4a5e22a2021-08-13 17:29:51 -040082 ExpectErrorLineNumber error(r, "'++' cannot operate on 'int[5]'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040083 (++a).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050084 }
85
86 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040087 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040088 Do(Discard(), 5).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050089 }
90
91 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040092 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040093 For(DSLStatement(), 5, DSLExpression(), Block()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050094 }
95
96 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040097 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040098 If(5, Discard()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050099 }
100
101 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400102 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400103 While(5, Discard()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500104 }
105
106 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400107 ExpectErrorLineNumber error(r, "no match for abs(bool)", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400108 Abs(true).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500109 }
110 End();
111}
112
113#endif // defined(__GNUC__) || defined(__clang__)