2021-08-13 21:29:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_ERROR_REPORTER
|
|
|
|
#define SKSL_ERROR_REPORTER
|
|
|
|
|
2021-09-07 17:49:07 +00:00
|
|
|
#include "include/core/SkStringView.h"
|
2021-08-13 21:29:51 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2021-09-07 17:49:07 +00:00
|
|
|
#include "include/private/SkSLString.h"
|
2021-08-13 21:29:51 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
#ifndef __has_builtin
|
|
|
|
#define __has_builtin(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class PositionInfo {
|
|
|
|
public:
|
|
|
|
PositionInfo(const char* file = nullptr, int line = -1)
|
|
|
|
: fFile(file)
|
|
|
|
, fLine(line) {}
|
|
|
|
|
|
|
|
#if __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
|
|
|
|
static PositionInfo Capture(const char* file = __builtin_FILE(), int line = __builtin_LINE()) {
|
|
|
|
return PositionInfo(file, line);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static PositionInfo Capture() { return PositionInfo(); }
|
|
|
|
#endif // __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
|
|
|
|
|
|
|
|
const char* file_name() const {
|
|
|
|
return fFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
int line() {
|
|
|
|
return fLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-31 11:40:24 +00:00
|
|
|
const char* fFile = nullptr;
|
|
|
|
int32_t fLine = -1;
|
2021-08-13 21:29:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which is notified in the event of an error.
|
|
|
|
*/
|
|
|
|
class ErrorReporter {
|
|
|
|
public:
|
|
|
|
ErrorReporter() {}
|
|
|
|
|
|
|
|
virtual ~ErrorReporter() {
|
|
|
|
SkASSERT(fPendingErrors.empty());
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:49:07 +00:00
|
|
|
void error(skstd::string_view msg, PositionInfo position);
|
2021-08-13 21:29:51 +00:00
|
|
|
|
|
|
|
/**
|
2021-09-27 15:20:34 +00:00
|
|
|
* Reports an error message at the given line of the source text. Errors reported
|
|
|
|
* with a line of -1 will be queued until line number information can be determined.
|
2021-08-13 21:29:51 +00:00
|
|
|
*/
|
2021-09-27 15:20:34 +00:00
|
|
|
void error(int line, skstd::string_view msg);
|
2021-08-13 21:29:51 +00:00
|
|
|
|
|
|
|
const char* source() const { return fSource; }
|
|
|
|
|
|
|
|
void setSource(const char* source) { fSource = source; }
|
|
|
|
|
|
|
|
void reportPendingErrors(PositionInfo pos) {
|
2021-09-07 17:49:07 +00:00
|
|
|
for (const String& msg : fPendingErrors) {
|
|
|
|
this->handleError(msg, pos);
|
2021-08-13 21:29:51 +00:00
|
|
|
}
|
|
|
|
fPendingErrors.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int errorCount() const {
|
|
|
|
return fErrorCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetErrorCount() {
|
|
|
|
fErrorCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
2021-09-07 17:49:07 +00:00
|
|
|
* Called when an error is reported.
|
2021-08-13 21:29:51 +00:00
|
|
|
*/
|
2021-09-07 17:49:07 +00:00
|
|
|
virtual void handleError(skstd::string_view msg, PositionInfo position) = 0;
|
2021-08-13 21:29:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
PositionInfo position(int offset) const;
|
|
|
|
|
|
|
|
const char* fSource = nullptr;
|
2021-09-07 17:49:07 +00:00
|
|
|
std::vector<String> fPendingErrors;
|
2021-08-13 21:29:51 +00:00
|
|
|
int fErrorCount = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error reporter for tests that need an SkSL context; aborts immediately if an error is reported.
|
|
|
|
*/
|
|
|
|
class TestingOnly_AbortErrorReporter : public ErrorReporter {
|
|
|
|
public:
|
2021-09-07 17:49:07 +00:00
|
|
|
void handleError(skstd::string_view msg, PositionInfo pos) override {
|
|
|
|
SK_ABORT("%.*s", (int)msg.length(), msg.data());
|
|
|
|
}
|
2021-08-13 21:29:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|