From 688d5fa6eb2c7f5281e50ace1401737479911ebc Mon Sep 17 00:00:00 2001 From: "T. Foley" Date: Sun, 6 Jun 2021 09:27:19 -0700 Subject: Include a "stack trace" with nested-import errors (#1872) * Include a "stack trace" with nested-import errors When errors occur in nested `#include` files it is often helpful to have a "stack trace" / traceback of the `#include` chain that led from a root translation unit to the file with an error. This change implements a similar feature for `import`s. It is worth noting that `import`s don't really *require* this kind of compiler support the way `#include`s do because the intention is that the meaning of an `import`ed file does not depend on the order or nesting of `import`s. As such, when trying to *fix* an error in an `import`ed file, you usually don't care how it came to be `import`ed into your shaders. The use case here is somebody adapting a large body of Slang code to use in a different codebase, such that they have certain `.slang` files they don't actually intend to have compile correctly, and they want to be able to diagnose how they came to include those files when/if they cause problems. The actual feature implementation is pretty simple because we already track a stack of active `import`s so that we can detect and diagnose recursive `import`s. This change simply changes the disagnostics when there is an error in imported code so that instead of just noting the inner-most `import` site it lists all the `import` sites that were active at the time. The change includes a test case to confirm that the behavior works (at least for the case of a parse error). * fixup: test outputs Co-authored-by: Yong He Co-authored-by: jsmall-nvidia --- source/slang/slang.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 9d2d766b9..f8af39fcb 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -2397,6 +2397,15 @@ Module* Linkage::loadModule(String const& name) sink); } +void Linkage::_diagnoseErrorInImportedModule( + DiagnosticSink* sink) +{ + for(auto info = m_modulesBeingImported; info; info = info->next) + { + sink->diagnose(info->importLoc, Diagnostics::errorInImportedModule, info->name); + } + sink->diagnose(SourceLoc(), Diagnostics::complationCeased); +} RefPtr Linkage::loadModule( Name* name, @@ -2418,7 +2427,9 @@ RefPtr Linkage::loadModule( ModuleBeingImportedRAII moduleBeingImported( this, - module); + module, + name, + srcLoc); // Create with the 'friendly' name SourceFile* sourceFile = getSourceManager()->createSourceFileWithBlob(filePathInfo, sourceBlob); @@ -2431,7 +2442,7 @@ RefPtr Linkage::loadModule( if( errorCountAfter != errorCountBefore ) { - sink->diagnose(srcLoc, Diagnostics::errorInImportedModule); + _diagnoseErrorInImportedModule(sink); } if (errorCountAfter) { @@ -2449,7 +2460,7 @@ RefPtr Linkage::loadModule( if (errorCountAfter != errorCountBefore) { - sink->diagnose(srcLoc, Diagnostics::errorInImportedModule); + _diagnoseErrorInImportedModule(sink); // Something went wrong during the parsing, so we should bail out. return nullptr; } -- cgit v1.2.3