Unify DSL and SkSL error handling paths
Change-Id: I9772da0c0a61d5fdb3114cd836053275bfb359df
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/436156
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/src/sksl/SkSLCompiler.cpp b/src/sksl/SkSLCompiler.cpp
index d78b0cd..27557c6 100644
--- a/src/sksl/SkSLCompiler.cpp
+++ b/src/sksl/SkSLCompiler.cpp
@@ -82,7 +82,7 @@
class AutoSource {
public:
- AutoSource(Compiler* compiler, const String* source)
+ AutoSource(Compiler* compiler, const char* source)
: fCompiler(compiler) {
SkASSERT(!fCompiler->fSource);
fCompiler->fSource = source;
@@ -343,7 +343,7 @@
std::vector<std::unique_ptr<ProgramElement>> elements;
std::vector<const ProgramElement*> sharedElements;
dsl::StartModule(this, kind, settings, baseModule);
- AutoSource as(this, source);
+ AutoSource as(this, source->c_str());
IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true,
*source);
SkASSERT(ir.fSharedElements.empty());
@@ -473,9 +473,10 @@
return DSLParser(this, settings, kind, text).program();
#else
auto textPtr = std::make_unique<String>(std::move(text));
- AutoSource as(this, textPtr.get());
+ AutoSource as(this, textPtr->c_str());
dsl::Start(this, kind, settings);
+ dsl::SetErrorHandler(this);
IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false,
*textPtr);
// Ideally, we would just use dsl::ReleaseProgram and not have to do any manual mucking about
@@ -884,7 +885,7 @@
bool Compiler::toSPIRV(Program& program, OutputStream& out) {
TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toSPIRV");
- AutoSource as(this, program.fSource.get());
+ AutoSource as(this, program.fSource->c_str());
ProgramSettings settings;
settings.fDSLUseMemoryPool = false;
dsl::Start(this, program.fConfig->fKind, settings);
@@ -942,7 +943,7 @@
bool Compiler::toGLSL(Program& program, OutputStream& out) {
TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toGLSL");
- AutoSource as(this, program.fSource.get());
+ AutoSource as(this, program.fSource->c_str());
GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
bool result = cg.generateCode();
return result;
@@ -968,7 +969,7 @@
bool Compiler::toMetal(Program& program, OutputStream& out) {
TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toMetal");
- AutoSource as(this, program.fSource.get());
+ AutoSource as(this, program.fSource->c_str());
MetalCodeGenerator cg(fContext.get(), &program, this, &out);
bool result = cg.generateCode();
return result;
@@ -985,37 +986,15 @@
#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
-Position Compiler::position(int offset) {
- if (fSource && offset >= 0) {
- int line = 1;
- int column = 1;
- for (int i = 0; i < offset; i++) {
- if ((*fSource)[i] == '\n') {
- ++line;
- column = 1;
- }
- else {
- ++column;
- }
- }
- return Position(line, column);
- } else {
- return Position(-1, -1);
- }
-}
-
-void Compiler::error(int offset, String msg) {
- if (strstr(msg.c_str(), POISON_TAG)) {
+void Compiler::handleError(const char* msg, dsl::PositionInfo* pos) {
+ if (strstr(msg, POISON_TAG)) {
// don't report errors on poison values
return;
}
fErrorCount++;
- Position pos = this->position(offset);
fErrorTextLength.push_back(fErrorText.length());
- if (!msg.starts_with("error: ")) {
- fErrorText += "error: ";
- }
- fErrorText += (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n";
+ fErrorText += "error: " + (pos && pos->line() >= 1 ? to_string(pos->line()) + ": " : "") + msg +
+ "\n";
}
void Compiler::setErrorCount(int c) {