diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2023-03-27 13:57:42 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-27 10:57:42 -0700 |
| commit | ca1f93a916ce6b984cba402c8d3710988f2b618f (patch) | |
| tree | f39b638210d38efdc7e2bb746c06b881323d0f53 /source/slang | |
| parent | 2179480e28bdd46c71cec269a8f55ba93aa54f53 (diff) | |
Using SourceMap for location output (#2736)
* #include an absolute path didn't work - because paths were taken to always be relative.
* WIP using SourceMap with SourceManager.
* Add a test to check obfuscation map is working.
---------
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/slang')
| -rw-r--r-- | source/slang/slang-compiler.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-emit.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-ir-obfuscate-loc.cpp | 17 |
3 files changed, 17 insertions, 8 deletions
diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp index ff38bbbde..839a4e67c 100644 --- a/source/slang/slang-compiler.cpp +++ b/source/slang/slang-compiler.cpp @@ -14,6 +14,8 @@ #include "../compiler-core/slang-lexer.h" +#include "../compiler-core/slang-json-source-map-util.h" + // Artifact #include "../compiler-core/slang-artifact-desc-util.h" #include "../compiler-core/slang-artifact-representation-impl.h" @@ -1907,7 +1909,7 @@ namespace Slang JSONValue jsonValue; - SLANG_RETURN_ON_FAIL(sourceMap->encode(jsonContainer, sink, jsonValue)); + SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::encode(sourceMap, jsonContainer, sink, jsonValue)); // Convert into a string JSONWriter writer(JSONWriter::IndentationStyle::Allman); diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index fe72efcc7..6a0f46e0a 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -82,6 +82,8 @@ #include "../compiler-core/slang-artifact-impl.h" #include "../compiler-core/slang-artifact-associated-impl.h" +#include "../compiler-core/slang-json-source-map-util.h" + #include <assert.h> Slang::String get_slang_cpp_host_prelude(); @@ -1148,7 +1150,7 @@ SlangResult CodeGenContext::emitEntryPointsSourceFromIR(ComPtr<IArtifact>& outAr RefPtr<JSONContainer> jsonContainer(new JSONContainer(&sourceMapSourceManager)); JSONValue jsonValue; - SLANG_RETURN_ON_FAIL(sourceMap->encode(jsonContainer, &sourceMapSink, jsonValue)); + SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::encode(sourceMap, jsonContainer, &sourceMapSink, jsonValue)); // Okay now convert this into a text file and then a blob diff --git a/source/slang/slang-ir-obfuscate-loc.cpp b/source/slang/slang-ir-obfuscate-loc.cpp index 79e855633..3ff2f3713 100644 --- a/source/slang/slang-ir-obfuscate-loc.cpp +++ b/source/slang/slang-ir-obfuscate-loc.cpp @@ -153,6 +153,8 @@ SlangResult obfuscateModuleLocs(IRModule* module, SourceManager* sourceManager) // Create the view we are going to use from the obfusctated "file". SourceView* obfuscatedView = sourceManager->createSourceView(obfuscatedFile, nullptr, SourceLoc()); + const auto obfuscatedRange = obfuscatedView->getRange(); + // Okay now we want to produce a map from these locs to a new source location { // Create a "bag" and put all of the indices in it. @@ -160,13 +162,11 @@ SlangResult obfuscateModuleLocs(IRModule* module, SourceManager* sourceManager) bag.setCount(uniqueLocCount); - const SourceLoc baseLoc = obfuscatedView->getRange().begin; - { SourceLoc* dst = bag.getBuffer(); for (Index i = 0; i < uniqueLocCount; ++i) { - dst[i] = baseLoc + i; + dst[i] = obfuscatedRange.begin + i; } } @@ -227,7 +227,6 @@ SlangResult obfuscateModuleLocs(IRModule* module, SourceManager* sourceManager) { const auto& pair = locPairs[i]; - // First find the view if (curView == nullptr || !curView->getRange().contains(pair.originalLoc)) @@ -282,9 +281,11 @@ SlangResult obfuscateModuleLocs(IRModule* module, SourceManager* sourceManager) entry.sourceFileIndex = sourceFileIndex; - // i is the generated column - entry.generatedColumn = i; + // Calculate the column offset, from the pair obfuscated loc + entry.generatedColumn = Index(obfuscatedRange.getOffset(pair.obfuscatedLoc)); + // We need to subtract 1, because handleLoc locations are 1 indexed, but SourceMap + // entry is 0 indexed. entry.sourceColumn = handleLoc.column - 1; entry.sourceLine = handleLoc.line - 1; @@ -293,6 +294,10 @@ SlangResult obfuscateModuleLocs(IRModule* module, SourceManager* sourceManager) } } + // Associate the sourceMap with the obfuscated file + obfuscatedFile->setObfuscatedSourceMap(sourceMap); + + // Set the obfuscated map onto the module module->setObfuscatedSourceMap(sourceMap); return SLANG_OK; |
