Skip to content

Commit

Permalink
Catch all exceptions from loadModule* and link API calls.
Browse files Browse the repository at this point in the history
This closes #5950.
  • Loading branch information
aleino-nv committed Jan 22, 2025
1 parent 151bdb8 commit 9d18854
Showing 1 changed file with 73 additions and 9 deletions.
82 changes: 73 additions & 9 deletions source/slang/slang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,30 @@ SLANG_NO_THROW slang::ITarget* SLANG_MCALL Linkage::getTargetByIndex(SlangInt in
}
#endif

static void outputExceptionDiagnostic(
const AbortCompilationException& exception,
DiagnosticSink& sink,
slang::IBlob** outDiagnostics)
{
sink.diagnoseRaw(Severity::Error, exception.Message.getUnownedSlice());
sink.getBlobIfNeeded(outDiagnostics);
}

static void outputExceptionDiagnostic(
const Exception& exception,
DiagnosticSink& sink,
slang::IBlob** outDiagnostics)
{
sink.diagnoseRaw(Severity::Internal, exception.Message.getUnownedSlice());
sink.getBlobIfNeeded(outDiagnostics);
}

static void outputExceptionDiagnostic(DiagnosticSink& sink, slang::IBlob** outDiagnostics)
{
sink.diagnoseRaw(Severity::Fatal, "An unknown exception occurred");
sink.getBlobIfNeeded(outDiagnostics);
}

SLANG_NO_THROW slang::IModule* SLANG_MCALL
Linkage::loadModule(const char* moduleName, slang::IBlob** outDiagnostics)
{
Expand All @@ -1247,9 +1271,19 @@ Linkage::loadModule(const char* moduleName, slang::IBlob** outDiagnostics)

return asExternal(module);
}
catch (const AbortCompilationException&)
catch (const AbortCompilationException& e)
{
sink.getBlobIfNeeded(outDiagnostics);
outputExceptionDiagnostic(e, sink, outDiagnostics);
return nullptr;
}
catch (const Exception& e)
{
outputExceptionDiagnostic(e, sink, outDiagnostics);
return nullptr;
}
catch (...)
{
outputExceptionDiagnostic(sink, outDiagnostics);
return nullptr;
}
}
Expand Down Expand Up @@ -1310,9 +1344,19 @@ slang::IModule* Linkage::loadModuleFromBlob(
sink.getBlobIfNeeded(outDiagnostics);
return asExternal(module);
}
catch (const AbortCompilationException&)
catch (const AbortCompilationException& e)
{
sink.getBlobIfNeeded(outDiagnostics);
outputExceptionDiagnostic(e, sink, outDiagnostics);
return nullptr;
}
catch (const Exception& e)
{
outputExceptionDiagnostic(e, sink, outDiagnostics);
return nullptr;
}
catch (...)
{
outputExceptionDiagnostic(sink, outDiagnostics);
return nullptr;
}
}
Expand Down Expand Up @@ -5021,12 +5065,32 @@ ComponentType::link(slang::IComponentType** outLinkedComponentType, ISlangBlob**
//
SLANG_UNUSED(outDiagnostics);

auto linked = fillRequirements(this);
if (!linked)
return SLANG_FAIL;
DiagnosticSink sink(getLinkage()->getSourceManager(), Lexer::sourceLocationLexer);

*outLinkedComponentType = ComPtr<slang::IComponentType>(linked).detach();
return SLANG_OK;
try
{
auto linked = fillRequirements(this);
if (!linked)
return SLANG_FAIL;

*outLinkedComponentType = ComPtr<slang::IComponentType>(linked).detach();
return SLANG_OK;
}
catch (const AbortCompilationException& e)
{
outputExceptionDiagnostic(e, sink, outDiagnostics);
return SLANG_FAIL;
}
catch (const Exception& e)
{
outputExceptionDiagnostic(e, sink, outDiagnostics);
return SLANG_FAIL;
}
catch (...)
{
outputExceptionDiagnostic(sink, outDiagnostics);
return SLANG_FAIL;
}
}

SLANG_NO_THROW SlangResult SLANG_MCALL ComponentType::linkWithOptions(
Expand Down

0 comments on commit 9d18854

Please sign in to comment.