summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-json-source-map-util.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-04-17 15:09:37 -0400
committerGitHub <noreply@github.com>2023-04-17 15:09:37 -0400
commit90a9f43573ec0777c2ae4fa20c8fdc51a4ae7b3a (patch)
tree360750778be872a086674024a9ce5a68bf4e7cb3 /source/compiler-core/slang-json-source-map-util.cpp
parenta3f622ace1bdef1f1a4150ec85d1328d1a589333 (diff)
Round trip source map (#2810)
* #include an absolute path didn't work - because paths were taken to always be relative. * Make output of obfuscation locs work in a slang-module. * Tidy up detection for writing serialized source locs. * Support for .zip references. Handling of obfuscated source maps read from containers. A test to check obfuscated source map working on a module. * When using obfuscation, always obfuscate locs instead of stripping them. We keep a source map, so we can still produce reasonable errors. * Write out source locs if debug information is enabled. * Check output without sourcemap. * Small fixes. * Small improvements around hash calculation for source map name. * Disable test that fails on x86 gcc linux for now. * Fix issues around obfuscated source map using lines rather than columns. Fix some issues around encoding/decoding. * Make column calculation of source locs take into account utf8/tabs. Don't special case obfuscated source map for lookup for source loc. * Support following multiple source maps. * Small fixes/improvements around SourceMap lookup.
Diffstat (limited to 'source/compiler-core/slang-json-source-map-util.cpp')
-rw-r--r--source/compiler-core/slang-json-source-map-util.cpp90
1 files changed, 59 insertions, 31 deletions
diff --git a/source/compiler-core/slang-json-source-map-util.cpp b/source/compiler-core/slang-json-source-map-util.cpp
index a5a454bd5..3929a3387 100644
--- a/source/compiler-core/slang-json-source-map-util.cpp
+++ b/source/compiler-core/slang-json-source-map-util.cpp
@@ -105,34 +105,31 @@ static SlangResult _decode(UnownedStringSlice& ioEncoded, Index& out)
{
Index v = 0;
- Index shift = 0;
const char* cur = ioEncoded.begin();
const char* end = ioEncoded.end();
- // Must have some chars
- if (cur >= end)
{
- return SLANG_FAIL;
- }
-
- for (; cur < end; ++cur)
- {
- const Index value = g_vlqDecodeTable[*cur];
- if (value < 0)
+ Index shift = 0;
+ Index decodeValue = 0;
+ do
{
- return SLANG_FAIL;
- }
+ // Must have a char to decode
+ if (cur >= end)
+ {
+ return SLANG_FAIL;
+ }
+
+ decodeValue = g_vlqDecodeTable[*cur++];
+ if (decodeValue < 0)
+ {
+ return SLANG_FAIL;
+ }
- v += (value & 0x1f) << shift;
+ v += (decodeValue & 0x1f) << shift;
- // If the continuation bit is not set we are done
- if (( value & 0x20) == 0)
- {
- ++cur;
- break;
+ shift += 5;
}
-
- shift += 5;
+ while (decodeValue & 0x20);
}
// Save out the remaining part
@@ -158,20 +155,16 @@ void _encode(Index v, StringBuilder& out)
do
{
- // Encode it
- const auto nextV = v >> 5;
-
- // Encode 5 bits
- char c = g_vlqEncodeTable[(v & 0x1f)];
-
- // See what bits are remaining
- v = (v >> 5);
-
- // Set the continuation bit's if there is more to encode
- c |= v ? 0x20 : 0;
+ const Index nextV = v >> 5;
+ const Index encodeValue = (v & 0x1f) + (nextV ? 0x20 : 0);
+ // Encode 5 bits, plus continuation bit
+ char c = g_vlqEncodeTable[encodeValue];
+
// Save the char
*cur++ = c;
+
+ v = nextV;
}
while (v);
@@ -461,4 +454,39 @@ SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* conta
return SLANG_OK;
}
+SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink, RefPtr<SourceMap>& outSourceMap)
+{
+ SourceManager sourceManager;
+ sourceManager.initialize(nullptr, nullptr);
+ DiagnosticSink sink(&sourceManager, nullptr);
+
+ sink.setParentSink(parentSink);
+
+ RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);
+
+ JSONValue rootValue;
+ {
+ // Now need to parse as JSON
+ SourceFile* sourceFile = sourceManager.createSourceFileWithBlob(PathInfo::makeUnknown(), blob);
+ SourceView* sourceView = sourceManager.createSourceView(sourceFile, nullptr, SourceLoc());
+
+ JSONLexer lexer;
+ lexer.init(sourceView, &sink);
+
+ JSONBuilder builder(container);
+
+ JSONParser parser;
+ SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &builder, &sink));
+
+ rootValue = builder.getRootValue();
+ }
+
+ RefPtr<SourceMap> sourceMap;
+
+ SLANG_RETURN_ON_FAIL(decode(container, rootValue, &sink, sourceMap));
+
+ outSourceMap = sourceMap;
+ return SLANG_OK;
+}
+
} // namespace Slang