From cd8715a7760189c54b36c0c250efbe1db5b8635c Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 1 Sep 2022 09:35:18 -0400 Subject: Passing source to Downstream compilation as artifacts (#2382) * #include an absolute path didn't work - because paths were taken to always be relative. * Make DownstreamCompileOptions use POD types. * CharSliceAllocator -> SliceAllocator Added SliceConverter CharSliceCaster -> SliceCaster * First attempt at zero terminating around blobs. * Fix clang warning. * Add SlangTerminatedChars Make Blob implementations support it. Make most blobs 'terminated'. * Fix bug setting up sourceFiles for CommandLineDownstreamCompiler. * Traffic in TerminatedCharSlice for sourceFiles. Use ArtifactDesc to generate temporary file names for source. * Fix typo in testing for shared library/C++. * Working with source being passed as artifacts to DownstreamCompiler. * Use artifacts in SourceManager/SourceFile. * Support infering extension from the original file extension. --- source/compiler-core/slang-slice-allocator.cpp | 46 +++++++++++++++++++++----- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'source/compiler-core/slang-slice-allocator.cpp') diff --git a/source/compiler-core/slang-slice-allocator.cpp b/source/compiler-core/slang-slice-allocator.cpp index 9985f6b19..9a0620ced 100644 --- a/source/compiler-core/slang-slice-allocator.cpp +++ b/source/compiler-core/slang-slice-allocator.cpp @@ -5,9 +5,9 @@ namespace Slang { -/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceConverter !!!!!!!!!!!!!!!!!!!!!!!!!!! */ +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceUtil !!!!!!!!!!!!!!!!!!!!!!!!!!! */ -/* static */ List SliceConverter::toList(const Slice& in) +/* static */ List SliceUtil::toList(const Slice& in) { List list; const auto count = in.count; @@ -20,20 +20,21 @@ namespace Slang { return list; } -/* static */TerminatedCharSlice SliceConverter::toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob) +/* static */const char* SliceUtil::getTerminated(ISlangBlob* blob, TerminatedCharSlice& outSlice) { const auto size = blob->getBufferSize(); - if (size == 0) { - return TerminatedCharSlice(); + outSlice = TerminatedCharSlice(); + return outSlice.begin(); } // If there is a 0 at the end byte, we are zero terminated const char* chars = (const char*)blob->getBufferPointer(); if (chars[size - 1] == 0) { - return TerminatedCharSlice(chars, Count(size - 1)); + outSlice = TerminatedCharSlice(chars, Count(size - 1)); + return chars; } // See if it has a castable interface @@ -42,15 +43,44 @@ namespace Slang { { if (castable->castAs(SlangTerminatedChars::getTypeGuid())) { - return TerminatedCharSlice(chars, Count(size)); + outSlice = TerminatedCharSlice(chars, Count(size)); + return chars; } } + return nullptr; +} + +/* static */TerminatedCharSlice SliceUtil::toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob) +{ + TerminatedCharSlice slice; + if (SliceUtil::getTerminated(blob, slice)) + { + return slice; + } + const auto size = blob->getBufferSize(); // We are out of options, we just have to allocate with zero termination which allocateString does - auto dst = allocator.getArena().allocateString(chars, Count(size)); + auto dst = allocator.getArena().allocateString((const char*)blob->getBufferPointer(), Count(size)); return TerminatedCharSlice(dst, Count(size)); } +/* static */TerminatedCharSlice SliceUtil::toTerminatedCharSlice(StringBuilder& storage, ISlangBlob* blob) +{ + TerminatedCharSlice slice; + if (SliceUtil::getTerminated(blob, slice)) + { + return slice; + } + + const auto size = blob->getBufferSize(); + auto chars = (const char*)blob->getBufferPointer(); + + storage.Clear(); + storage.append(UnownedStringSlice(chars, size)); + + return TerminatedCharSlice(storage.getBuffer(), Count(size)); +} + /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceAllocator !!!!!!!!!!!!!!!!!!!!!!!!!!! */ TerminatedCharSlice SliceAllocator::allocate(const char* in) -- cgit v1.2.3