blob: cd87dea90ce41ddcb70765a803d0cb787dfa43e5 [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 Nicholasc8452722021-10-07 10:47:32 -040014#include "src/sksl/SkSLThreadContext.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050015#include "src/sksl/dsl/priv/DSLWriter.h"
Ethan Nicholas34c7e112021-02-25 20:50:32 -050016
17#include "tests/Test.h"
18
19#include <limits>
20
21using namespace SkSL::dsl;
22
23#if defined(__GNUC__) || defined(__clang__)
24
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040025class ExpectErrorLineNumber : public SkSL::ErrorReporter {
Ethan Nicholas34c7e112021-02-25 20:50:32 -050026public:
27 ExpectErrorLineNumber(skiatest::Reporter* reporter, const char* msg, int line)
28 : fMsg(msg)
29 , fLine(line)
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040030 , fReporter(reporter)
31 , fOldReporter(&GetErrorReporter()) {
32 SetErrorReporter(this);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050033 }
34
35 ~ExpectErrorLineNumber() override {
36 REPORTER_ASSERT(fReporter, !fMsg);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040037 SetErrorReporter(fOldReporter);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050038 }
39
Ethan Nicholas32724122021-09-07 13:49:07 -040040 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 Nicholasa40ddcd2021-08-06 09:17:18 -040044 REPORTER_ASSERT(fReporter, pos.line() == fLine,
Ethan Nicholas32724122021-09-07 13:49:07 -040045 "Line number mismatch: expected %d, but received %d\n", fLine, pos.line());
Ethan Nicholasc8452722021-10-07 10:47:32 -040046 SkSL::ThreadContext::Compiler().handleError(msg, pos);
Ethan Nicholas34c7e112021-02-25 20:50:32 -050047 fMsg = nullptr;
48 }
49
50private:
51 const char* fMsg;
52 int fLine;
53 skiatest::Reporter* fReporter;
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040054 ErrorReporter* fOldReporter;
Ethan Nicholas34c7e112021-02-25 20:50:32 -050055};
56
57DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLErrorLineNumbers, r, ctxInfo) {
58 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
59 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040060 ExpectErrorLineNumber error(r, "type mismatch: '+' cannot operate on 'float', 'bool'",
Ethan Nicholas961d9442021-03-16 16:37:29 -040061 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040062 (Float(1) + true).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050063 }
64
65 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040066 Var a(kBool_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -040067 DSLWriter::MarkDeclared(a);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040068 ExpectErrorLineNumber error(r, "type mismatch: '=' cannot operate on 'bool', 'float'",
Ethan Nicholas961d9442021-03-16 16:37:29 -040069 __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040070 (a = 5.0f).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050071 }
72
73 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040074 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040075 DSLWriter::MarkDeclared(a);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040076 ExpectErrorLineNumber error(r, "expected 'int', but found 'bool'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040077 (a[true]).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050078 }
79
80 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040081 Var a(Array(kInt_Type, 5));
Ethan Nicholas961d9442021-03-16 16:37:29 -040082 DSLWriter::MarkDeclared(a);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040083 ExpectErrorLineNumber error(r, "'++' cannot operate on 'int[5]'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040084 (++a).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050085 }
86
87 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040088 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040089 Do(Discard(), 5).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050090 }
91
92 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040093 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040094 For(DSLStatement(), 5, DSLExpression(), Block()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -050095 }
96
97 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040098 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -040099 If(5, Discard()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500100 }
101
102 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400103 ExpectErrorLineNumber error(r, "expected 'bool', but found 'int'", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400104 While(5, Discard()).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500105 }
106
107 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400108 ExpectErrorLineNumber error(r, "no match for abs(bool)", __LINE__ + 1);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400109 Abs(true).release();
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500110 }
111 End();
112}
113
114#endif // defined(__GNUC__) || defined(__clang__)