diff options
| author | skallweitNV <64953474+skallweitNV@users.noreply.github.com> | 2022-11-30 22:26:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-30 13:26:55 -0800 |
| commit | 976f578585a4d4ed24e37d0c45a94a8e6afcff19 (patch) | |
| tree | 60897092962bfd562d026b81a29096efbfc85816 | |
| parent | 09684224d5ab63f530d66c0be65fa50e6fc5290b (diff) | |
Cleanup DigestBuilder and MD5HashGen (#2539)
* Cleanup DigestBuilder and MD5HashGen
* Fix templates
Co-authored-by: Yong He <yonghe@outlook.com>
| -rw-r--r-- | build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj | 1 | ||||
| -rw-r--r-- | build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj.filters | 3 | ||||
| -rw-r--r-- | source/core/slang-digest-builder.h | 88 | ||||
| -rw-r--r-- | source/core/slang-digest-util.cpp | 6 | ||||
| -rw-r--r-- | source/core/slang-md5.cpp | 94 | ||||
| -rw-r--r-- | source/core/slang-md5.h | 27 | ||||
| -rw-r--r-- | source/slang/slang-compiler.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 168 | ||||
| -rw-r--r-- | tools/slang-unit-test/unit-test-digest-builder.cpp | 72 | ||||
| -rw-r--r-- | tools/slang-unit-test/unit-test-md5.cpp | 104 |
10 files changed, 317 insertions, 250 deletions
diff --git a/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj b/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj index 79617d702..f2981b504 100644 --- a/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj +++ b/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj @@ -276,6 +276,7 @@ <ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-com-host-callable.cpp" />
<ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-command-line-args.cpp" />
<ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-compression.cpp" />
+ <ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-digest-builder.cpp" />
<ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-digest-utils.cpp" />
<ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-file-system.cpp" />
<ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-find-type-by-name.cpp" />
diff --git a/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj.filters b/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj.filters index 20e28f2f8..93d7e7325 100644 --- a/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj.filters +++ b/build/visual-studio/slang-unit-test-tool/slang-unit-test-tool.vcxproj.filters @@ -29,6 +29,9 @@ <ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-compression.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-digest-builder.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="..\..\..\tools\slang-unit-test\unit-test-digest-utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
diff --git a/source/core/slang-digest-builder.h b/source/core/slang-digest-builder.h index 47c0cd8f7..c613b2239 100644 --- a/source/core/slang-digest-builder.h +++ b/source/core/slang-digest-builder.h @@ -1,6 +1,8 @@ #pragma once #include "slang-md5.h" #include "../../slang.h" +#include "../core/slang-string.h" +#include "../core/slang-list.h" namespace Slang { @@ -15,10 +17,90 @@ namespace Slang hashGen.init(&context); } - template <typename T> - void addToDigest(T item) + template<typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value, int>::type = 0> + DigestBuilder& operator<<(const T value) { - hashGen.update(&context, item); + append(value); + return *this; + } + + DigestBuilder& operator<<(const String& str) + { + append(str); + return *this; + } + + DigestBuilder& operator<<(const StringSlice& str) + { + append(str); + return *this; + } + + DigestBuilder& operator<<(const UnownedStringSlice& str) + { + append(str); + return *this; + } + + DigestBuilder& operator<<(ISlangBlob* blob) + { + append(blob); + return *this; + } + + DigestBuilder& operator<<(const slang::Digest& digest) + { + append(digest); + return *this; + } + + template<typename T, typename std::enable_if<std::is_pod<T>::value, int>::type = 0> + DigestBuilder& operator<<(const List<T>& list) + { + append(list); + return *this; + } + + void append(const void* data, SlangInt size) + { + hashGen.update(&context, data, size); + } + + template<typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value, int>::type = 0> + void append(const T value) + { + append(&value, sizeof(T)); + } + + void append(const String& str) + { + append(str.getBuffer(), str.getLength()); + } + + void append(const StringSlice& str) + { + append(str.begin(), str.getLength()); + } + + void append(const UnownedStringSlice& str) + { + append(str.begin(), str.getLength()); + } + + void append(ISlangBlob* blob) + { + append(blob->getBufferPointer(), blob->getBufferSize()); + } + + void append(const slang::Digest& digest) + { + append(&digest, sizeof(digest)); + } + + template<typename T, typename std::enable_if<std::is_pod<T>::value, int>::type = 0> + void append(const List<T>& list) + { + append(list.getBuffer(), list.getCount() * sizeof(T)); } Digest finalize() diff --git a/source/core/slang-digest-util.cpp b/source/core/slang-digest-util.cpp index d235f2cf0..8252dce65 100644 --- a/source/core/slang-digest-util.cpp +++ b/source/core/slang-digest-util.cpp @@ -12,15 +12,15 @@ namespace Slang /*static*/ Digest DigestUtil::computeDigestForStringSlice(UnownedStringSlice text) { DigestBuilder builder; - builder.addToDigest(text); + builder.append(text); return builder.finalize(); } /*static*/ Digest DigestUtil::combine(const Digest& digestA, const Digest& digestB) { DigestBuilder builder; - builder.addToDigest(digestA); - builder.addToDigest(digestB); + builder.append(digestA); + builder.append(digestB); return builder.finalize(); } diff --git a/source/core/slang-md5.cpp b/source/core/slang-md5.cpp index 7371b21d2..049ebae95 100644 --- a/source/core/slang-md5.cpp +++ b/source/core/slang-md5.cpp @@ -34,11 +34,11 @@ * optimizations are not included to reduce source code size and avoid * compile-time configuration. */ - + #ifndef HAVE_OPENSSL - + #include <string.h> - + #include "slang-md5.h" namespace Slang @@ -55,7 +55,7 @@ namespace Slang #define H(x, y, z) (((x) ^ (y)) ^ (z)) #define H2(x, y, z) ((x) ^ ((y) ^ (z))) #define I(x, y, z) ((y) ^ ((x) | ~(z))) - + /* * The MD5 transformation for all four rounds. */ @@ -63,7 +63,7 @@ namespace Slang (a) += f((b), (c), (d)) + (x) + (t); \ (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ (a) += (b); - + /* * SET reads 4 input bytes in little-endian byte order and stores them in a * properly aligned word in host byte order. @@ -94,7 +94,7 @@ namespace Slang #define GET(n) \ (ctx->block[(n)]) #endif - + /* * This processes one or more 64-byte data blocks, but does NOT update the bit * counters. There are no alignment requirements. @@ -104,20 +104,20 @@ namespace Slang const unsigned char* ptr; MD5_u32plus a, b, c, d; MD5_u32plus saved_a, saved_b, saved_c, saved_d; - + ptr = (const unsigned char*)data; - + a = ctx->a; b = ctx->b; c = ctx->c; d = ctx->d; - + do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; - + /* Round 1 */ STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) @@ -135,7 +135,7 @@ namespace Slang STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) - + /* Round 2 */ STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) @@ -153,7 +153,7 @@ namespace Slang STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) - + /* Round 3 */ STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11) @@ -171,7 +171,7 @@ namespace Slang STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23) - + /* Round 4 */ STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) @@ -189,88 +189,68 @@ namespace Slang STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) - + a += saved_a; b += saved_b; c += saved_c; d += saved_d; - + ptr += 64; } while (size -= 64); - + ctx->a = a; ctx->b = b; ctx->c = c; ctx->d = d; - + return ptr; } - + void MD5HashGen::init(MD5Context* ctx) { ctx->a = 0x67452301; ctx->b = 0xefcdab89; ctx->c = 0x98badcfe; ctx->d = 0x10325476; - + ctx->lo = 0; ctx->hi = 0; } - void MD5HashGen::update(MD5Context* ctx, UnownedStringSlice string) - { - update(ctx, string.begin(), string.getLength()); - } - - void MD5HashGen::update(MD5Context* ctx, String str) - { - update(ctx, str.getBuffer(), str.getLength()); - } - - void MD5HashGen::update(MD5Context* ctx, const slang::Digest& hash) - { - update(ctx, hash.values, sizeof(hash.values)); - } - - void MD5HashGen::update(MD5Context* ctx, ISlangBlob* blob) - { - update(ctx, blob->getBufferPointer(), blob->getBufferSize()); - } - void MD5HashGen::update(MD5Context* ctx, const void* data, SlangInt size) { MD5_u32plus saved_lo; SlangInt used, available; - + saved_lo = ctx->lo; if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) ctx->hi++; ctx->hi += (MD5_u32plus)size >> 29; - + used = saved_lo & 0x3f; - + if (used) { available = 64 - used; - + if (size < available) { memcpy(&ctx->buffer[used], data, size); return; } - + memcpy(&ctx->buffer[used], data, available); data = (const unsigned char*)data + available; size -= available; body(ctx, ctx->buffer, 64); } - + if (size >= 64) { data = body(ctx, data, size & ~(SlangInt)0x3f); size &= 0x3f; } - + memcpy(ctx->buffer, data, size); } - + #define OUTUINT(dst, src) \ (dst)[0] = (uint32_t)src; \ @@ -279,39 +259,39 @@ namespace Slang (dst)[1] = (unsigned char)((src) >> 8); \ (dst)[2] = (unsigned char)((src) >> 16); \ (dst)[3] = (unsigned char)((src) >> 24); - + void MD5HashGen::finalize(MD5Context* ctx, slang::Digest* result) { unsigned long used, available; - + used = ctx->lo & 0x3f; - + ctx->buffer[used++] = 0x80; - + available = 64 - used; - + if (available < 8) { memset(&ctx->buffer[used], 0, available); body(ctx, ctx->buffer, 64); used = 0; available = 64; } - + memset(&ctx->buffer[used], 0, available - 8); - + ctx->lo <<= 3; OUTCHAR(&ctx->buffer[56], ctx->lo) OUTCHAR(&ctx->buffer[60], ctx->hi) - + body(ctx, ctx->buffer, 64); OUTUINT(&result->values[0], ctx->a) OUTUINT(&result->values[1], ctx->b) OUTUINT(&result->values[2], ctx->c) OUTUINT(&result->values[3], ctx->d) - + memset(ctx, 0, sizeof(*ctx)); } } - + #endif diff --git a/source/core/slang-md5.h b/source/core/slang-md5.h index 2e99fb667..d40102b69 100644 --- a/source/core/slang-md5.h +++ b/source/core/slang-md5.h @@ -22,7 +22,7 @@ * * See md5.c for more information. */ - + #ifdef HAVE_OPENSSL #include <openssl/md5.h> #elif !defined(_MD5_H) @@ -51,34 +51,13 @@ namespace Slang public: void init(MD5Context* ctx); - // Helper update function for raw values (e.g. ints, uints) - template<typename T, - typename = typename std::enable_if<std::is_enum<T>::value || std::is_arithmetic<T>::value>::type> - void update(MD5Context* ctx, const T& val) - { - update(ctx, &val, sizeof(T)); - } - // Helper update function for Slang::List - template<typename T> - void update(MD5Context* ctx, const List<T>& list) - { - update(ctx, list.getBuffer(), list.getCount()); - } - // Helper update function for UnownedStringSlice - void update(MD5Context* ctx, UnownedStringSlice string); - // Helper update function for Slang::String - void update(MD5Context* ctx, String str); - // Helper update function for Checksums - void update(MD5Context* ctx, const slang::Digest& checksum); - // Helper update function for ISlangBlob - void update(MD5Context* ctx, ISlangBlob* blob); + void update(MD5Context* ctx, const void* data, SlangInt size); void finalize(MD5Context* ctx, slang::Digest* result); private: static const void* body(MD5Context* ctx, const void* data, SlangInt size); - void update(MD5Context* ctx, const void* data, SlangInt size); }; } - + #endif diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp index b02421c46..a5e2003c2 100644 --- a/source/slang/slang-compiler.cpp +++ b/source/slang/slang-compiler.cpp @@ -310,8 +310,8 @@ namespace Slang //TODO: Implement some kind of hashInto for Val then replace this auto subtypeWitness = m_subtypeWitness->toString(); - builder.addToDigest(subtypeWitness); - builder.addToDigest(m_conformanceIdOverride); + builder.append(subtypeWitness); + builder.append(m_conformanceIdOverride); } List<Module*> const& TypeConformance::getModuleDependencies() diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 5add881d6..f16f1b186 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -60,7 +60,7 @@ extern Slang::String get_slang_hlsl_prelude(); namespace Slang { -/* static */const BaseTypeInfo BaseTypeInfo::s_info[Index(BaseType::CountOf)] = +/* static */const BaseTypeInfo BaseTypeInfo::s_info[Index(BaseType::CountOf)] = { { 0, 0, uint8_t(BaseType::Void) }, { uint8_t(sizeof(bool)), 0, uint8_t(BaseType::Bool) }, @@ -142,7 +142,7 @@ void Session::init() m_completionTokenName = getNamePool()->getName("#?"); m_sharedLibraryLoader = DefaultSharedLibraryLoader::getSingleton(); - + // Set up shared AST builder m_sharedASTBuilder = new SharedASTBuilder; m_sharedASTBuilder->init(this); @@ -218,11 +218,11 @@ void Session::_initCodeGenTransitionMap() // Set up the default ways to do compilations between code gen targets auto& map = m_codeGenTransitionMap; - + // TODO(JS): There currently isn't a 'downstream compiler' for direct spirv output. If we did // it would presumably a transition from SlangIR to SPIRV. - // For C and C++ we default to use the 'genericCCpp' compiler + // For C and C++ we default to use the 'genericCCpp' compiler { const CodeGenTarget sources[] = { CodeGenTarget::CSource, CodeGenTarget::CPPSource }; for (auto source : sources) @@ -236,7 +236,7 @@ void Session::_initCodeGenTransitionMap() } } - + // Add all the straightforward transitions map.addTransition(CodeGenTarget::CUDASource, CodeGenTarget::PTX, PassThroughMode::NVRTC); map.addTransition(CodeGenTarget::HLSL, CodeGenTarget::DXBytecode, PassThroughMode::Fxc); @@ -317,7 +317,7 @@ SlangResult Session::compileStdLib(slang::CompileStdLibFlags compileFlags) // For all the modules add their doc output to docStrings for (Module* stdlibModule : stdlibModules) - { + { RefPtr<ASTMarkup> markup(new ASTMarkup); ASTMarkupUtil::extract(stdlibModule->getModuleDecl(), sourceManager, &sink, markup); @@ -433,9 +433,9 @@ SlangResult Session::_readBuiltinModule(ISlangFileSystem* fileSystem, Scope* sco // Load the riff container SLANG_RETURN_ON_FAIL(RiffUtil::read(&stream, riffContainer)); } - + // Load up the module - + SerialContainerData containerData; Linkage* linkage = getBuiltinLinkage(); @@ -471,7 +471,7 @@ SlangResult Session::_readBuiltinModule(ISlangFileSystem* fileSystem, Scope* sco { registerBuiltinDecls(this, moduleDecl); } - + module->setModuleDecl(moduleDecl); } @@ -524,7 +524,7 @@ static T makeFromSizeVersioned(const uint8_t* src) SLANG_COMPILE_TIME_ASSERT(sizeof(((T*)src)->structureSize) == sizeof(size_t)); // The structureSize field *must* be the first element of T - // Ideally would use SLANG_COMPILE_TIME_ASSERT, but that doesn't work on gcc. + // Ideally would use SLANG_COMPILE_TIME_ASSERT, but that doesn't work on gcc. // Can't just assert, because determined to be a constant expression { auto offset = SLANG_OFFSET_OF(T, structureSize); @@ -538,7 +538,7 @@ static T makeFromSizeVersioned(const uint8_t* src) const size_t dstSize = sizeof(T); // If they are the same size, and appropriate alignment we can just cast and return - if (srcSize == dstSize && + if (srcSize == dstSize && (size_t(src) & (SLANG_ALIGN_OF(T) - 1)) == 0) { return *(const T*)src; @@ -638,7 +638,7 @@ SLANG_NO_THROW void SLANG_MCALL Session::setDownstreamCompilerPath( { PassThroughMode passThrough = PassThroughMode(inPassThrough); SLANG_ASSERT(int(passThrough) > int(PassThroughMode::None) && int(passThrough) < int(PassThroughMode::CountOf)); - + if (m_downstreamCompilerPaths[int(passThrough)] != path) { // Make access redetermine compiler @@ -747,7 +747,7 @@ SlangPassThrough Session::getDownstreamCompilerForTransition(SlangCompileTarget const auto desc = ArtifactDescUtil::makeDescForCompileTarget(inTarget); // Special case host-callable - if ((desc.kind == ArtifactKind::HostCallable) && + if ((desc.kind == ArtifactKind::HostCallable) && (source == CodeGenTarget::CSource || source == CodeGenTarget::CPPSource)) { // We prefer LLVM if it's available @@ -1213,7 +1213,7 @@ SLANG_NO_THROW slang::TypeReflection* SLANG_MCALL Linkage::getContainerType( containerTypeReflection = type; break; } - + m_containerTypes.Add(key, containerTypeReflection); } @@ -1329,45 +1329,45 @@ void Linkage::updateDependencyBasedHash( { // Add the Slang compiler version to the hash auto version = String(getBuildTagString()); - builder.addToDigest(version); + builder.append(version); // Add the search directory paths to the hash auto searchDirectoryList = getSearchDirectories().searchDirectories; for (auto& searchDir : searchDirectoryList) { auto searchPath = searchDir.path; - builder.addToDigest(searchPath); + builder.append(searchPath); } // Add the preprocessor definitions to the hash for (auto& key : preprocessorDefinitions) { - builder.addToDigest(key.Key); - builder.addToDigest(key.Value); + builder.append(key.Key); + builder.append(key.Value); } // Add the target specified by targetIndex auto targetReq = targets[targetIndex]; - builder.addToDigest(targetReq->getTarget()); - builder.addToDigest(targetReq->getTargetFlags()); - builder.addToDigest(targetReq->getFloatingPointMode()); - builder.addToDigest(targetReq->getLineDirectiveMode()); - builder.addToDigest(targetReq->shouldDumpIntermediates()); - builder.addToDigest(targetReq->getForceGLSLScalarBufferLayout()); - builder.addToDigest(targetReq->shouldTrackLiveness()); + builder.append(targetReq->getTarget()); + builder.append(targetReq->getTargetFlags()); + builder.append(targetReq->getFloatingPointMode()); + builder.append(targetReq->getLineDirectiveMode()); + builder.append(targetReq->shouldDumpIntermediates()); + builder.append(targetReq->getForceGLSLScalarBufferLayout()); + builder.append(targetReq->shouldTrackLiveness()); auto targetProfile = targetReq->getTargetProfile(); - builder.addToDigest(targetProfile.getStage()); - builder.addToDigest(targetProfile.getVersion()); - builder.addToDigest(targetProfile.getFamily()); + builder.append(targetProfile.getStage()); + builder.append(targetProfile.getVersion()); + builder.append(targetProfile.getFamily()); auto targetProfileName = String(targetProfile.getName()); - builder.addToDigest(targetProfileName); + builder.append(targetProfileName); auto cookedCapabilities = targetReq->getTargetCaps().getExpandedAtoms(); for (auto& capability : cookedCapabilities) { - builder.addToDigest(capability); + builder.append(capability); } // Add the downstream compiler version (if it exists) to the hash @@ -1378,7 +1378,7 @@ void Linkage::updateDependencyBasedHash( ComPtr<ISlangBlob> versionString; if (SLANG_SUCCEEDED(downstreamCompiler->getVersionString(versionString.writeRef()))) { - builder.addToDigest(versionString); + builder.append(versionString); } } } @@ -1597,18 +1597,18 @@ SlangResult TranslationUnitRequest::requireSourceFiles() IArtifact* artifact = m_sourceArtifacts[i]; PathInfo pathInfo = PathInfo::makeUnknown(); - + if (auto extRep = findRepresentation<IExtFileArtifactRepresentation>(artifact)) { auto extFileSystem = extRep->getFileSystem(); - // TODO(JS): - // Ideally we'd confirm that the file system was the same, such we could know that the unique + // TODO(JS): + // Ideally we'd confirm that the file system was the same, such we could know that the unique // identity is appropriate for the current file system. // // We just assume compatibility for the moment, because repro will be a different file system // but we need to use the unique identity that is there. - + //if (extFileSystem == linkageFileSystem) { // Get the unique identity @@ -1665,7 +1665,7 @@ SlangResult TranslationUnitRequest::requireSourceFiles() pathInfo.getName()); return blobRes; } - + auto sourceFile = sourceManager->createSourceFileWithBlob(pathInfo, blob); _addSourceFile(sourceFile); } @@ -1699,10 +1699,10 @@ void TranslationUnitRequest::_addSourceFile(SourceFile* sourceFile) } } -List<SourceFile*> const& TranslationUnitRequest::getSourceFiles() -{ +List<SourceFile*> const& TranslationUnitRequest::getSourceFiles() +{ SLANG_ASSERT(m_sourceArtifacts.getCount() == m_sourceFiles.getCount()); - return m_sourceFiles; + return m_sourceFiles; } EndToEndCompileRequest::~EndToEndCompileRequest() @@ -1741,7 +1741,7 @@ void EndToEndCompileRequest::setWriter(WriterChannel chan, ISlangWriter* writer) // For diagnostic output, if the user passes in nullptr, we set on m_sink.writer as that enables buffering on DiagnosticSink if (chan == WriterChannel::Diagnostic) { - m_sink.writer = writer; + m_sink.writer = writer; } } @@ -1771,10 +1771,10 @@ Expr* Linkage::parseTermString(String typeStr, Scope* scope) // Create a SourceManager on the stack, so any allocations for 'SourceFile'/'SourceView' etc will be cleaned up SourceManager localSourceManager; localSourceManager.initialize(getSourceManager(), nullptr); - + Slang::SourceFile* srcFile = localSourceManager.createSourceFileWithString(PathInfo::makeTypeParse(), typeStr); - - // We'll use a temporary diagnostic sink + + // We'll use a temporary diagnostic sink DiagnosticSink sink(&localSourceManager, nullptr); // RAII type to make make sure current SourceManager is restored after parse. @@ -1833,7 +1833,7 @@ Type* ComponentType::getTypeFromString( // TODO(JS): For now just used the linkages ASTBuilder to keep on scope // // The parseTermString uses the linkage ASTBuilder for it's parsing. - // + // // It might be possible to just create a temporary ASTBuilder - the worry though is // that the parsing sets a member variable in AST node to one of these scopes, and then // it become a dangling pointer. So for now we go with the linkages. @@ -1867,7 +1867,7 @@ CompileRequestBase::CompileRequestBase( FrontEndCompileRequest::FrontEndCompileRequest( Linkage* linkage, - StdWriters* writers, + StdWriters* writers, DiagnosticSink* sink) : CompileRequestBase(linkage, sink) , m_writers(writers) @@ -1987,7 +1987,7 @@ protected: }; -// Holds the hierarchy of views, the children being views that were 'initiated' (have an initiating SourceLoc) in the parent. +// Holds the hierarchy of views, the children being views that were 'initiated' (have an initiating SourceLoc) in the parent. typedef Dictionary<SourceView*, List<SourceView*>> ViewInitiatingHierarchy; // Calculate the hierarchy from the sourceManager @@ -2041,7 +2041,7 @@ static SourceView* _findInitialSourceView(SourceFile* sourceFile) { return view; } - } + } } return nullptr; @@ -2154,10 +2154,10 @@ void FrontEndCompileRequest::parseTranslationUnit( // TODO(JS): NOTE! Here we are using the searchDirectories on the linkage. This is because // currently the API only allows the setting search paths on linkage. - // + // // Here we should probably be using the searchDirectories on the FrontEndCompileRequest. // If searchDirectories.parent pointed to the one in the Linkage would mean linkage paths - // would be checked too (after those on the FrontEndCompileRequest). + // would be checked too (after those on the FrontEndCompileRequest). IncludeSystem includeSystem(&linkage->searchDirectories, linkage->getFileSystemExt(), linkage->getSourceManager()); Scope* languageScope = nullptr; @@ -2185,11 +2185,11 @@ void FrontEndCompileRequest::parseTranslationUnit( combinedPreprocessorDefinitions.Add(def.Key, def.Value); // Define standard macros, if not already defined. This style assumes using `#if __SOME_VAR` style, as in - // + // // ``` // #if __SLANG_COMPILER__ // ``` - // + // // This choice is made because slang outputs a warning on using a variable in an #if if not defined // // Of course this means using #ifndef/#ifdef/defined() is probably not appropraite with thes variables. @@ -2366,7 +2366,7 @@ void FrontEndCompileRequest::generateIR() for( auto& translationUnit : translationUnits ) { // We want to only run generateIRForTranslationUnit once here. This is for two side effects: - // * it can dump ir + // * it can dump ir // * it can generate diagnostics /// Generate IR for translation unit. @@ -2397,7 +2397,7 @@ void FrontEndCompileRequest::generateIR() IRSerialWriter writer; writer.write(irModule, nullptr, SerialOptionFlag::RawSourceLocation, &serialData); - // Destroy irModule such that memory can be used for newly constructed read irReadModule + // Destroy irModule such that memory can be used for newly constructed read irReadModule irModule = nullptr; } RefPtr<IRModule> irReadModule; @@ -2500,12 +2500,12 @@ SlangResult FrontEndCompileRequest::executeActionsInner() RefPtr<ASTMarkup> markup(new ASTMarkup); ASTMarkupUtil::extract(translationUnit->getModuleDecl(), getSourceManager(), getSink(), markup); - // Convert to markdown + // Convert to markdown DocMarkdownWriter markdownWriter(markup, astBuilder); markdownWriter.writeAll(); UnownedStringSlice docText = markdownWriter.getOutput().getUnownedSlice(); - writer->write(docText.begin(), docText.getLength()); + writer->write(docText.begin(), docText.getLength()); } } } @@ -2767,7 +2767,7 @@ void FrontEndCompileRequest::addTranslationUnitSourceArtifact( IArtifact* sourceArtifact) { auto translationUnit = translationUnits[translationUnitIndex]; - + // Add the source file translationUnit->addSourceArtifact(sourceArtifact); } @@ -2811,9 +2811,9 @@ void FrontEndCompileRequest::addTranslationUnitSourceFile( SlangResult existsRes = SLANG_OK; // If we require caching, we demand it's loaded here. - // - // In practice this probably means repro capture is enabled. So we want to - // load the blob such that it's in the cache, even if it doesn't actually + // + // In practice this probably means repro capture is enabled. So we want to + // load the blob such that it's in the cache, even if it doesn't actually // have to be loaded for the compilation. if (getLinkage()->m_requireCacheFileSystem) { @@ -2967,7 +2967,7 @@ void Linkage::_diagnoseErrorInImportedModule( RefPtr<Module> Linkage::loadModule( Name* name, const PathInfo& filePathInfo, - ISlangBlob* sourceBlob, + ISlangBlob* sourceBlob, SourceLoc const& srcLoc, DiagnosticSink* sink, const LoadedModuleDictionary* additionalLoadedModules) @@ -3133,7 +3133,7 @@ RefPtr<Module> Linkage::findOrImportModule( PathInfo pathIncludedFromInfo = getSourceManager()->getPathInfo(loc, SourceLocType::Actual); PathInfo filePathInfo; - // We have to load via the found path - as that is how file was originally loaded + // We have to load via the found path - as that is how file was originally loaded if (SLANG_FAILED(includeSystem.findFile(fileName, pathIncludedFromInfo.foundPath, filePathInfo))) { sink->diagnose(loc, Diagnostics::cannotFindFile, fileName); @@ -3276,7 +3276,7 @@ void Module::updateContentsBasedHash(DigestBuilder& builder) statFailed = true; break; } - lastModifiedBuilder.addToDigest(fileStatus.st_mtime); + lastModifiedBuilder.append(fileStatus.st_mtime); } slang::Digest temp = lastModifiedBuilder.finalize(); @@ -3292,11 +3292,11 @@ void Module::updateContentsBasedHash(DigestBuilder& builder) { // Failure to read the file means this is a digest for the contents of a source // file which does not live on disk. - contentsBuilder.addToDigest(DigestUtil::fromString(file.getUnownedSlice())); + contentsBuilder.append(DigestUtil::fromString(file.getUnownedSlice())); } else { - contentsBuilder.addToDigest(fileContents); + contentsBuilder.append(fileContents); } } contentsDigest = contentsBuilder.finalize(); @@ -3308,7 +3308,7 @@ void Module::updateContentsBasedHash(DigestBuilder& builder) } } - builder.addToDigest(contentsDigest); + builder.append(contentsDigest); } void Module::addModuleDependency(Module* module) @@ -3378,7 +3378,7 @@ void Module::_processFindDeclsExportSymbolsRec(Decl* decl) { if (_canExportDeclSymbol(decl->astNodeType)) { - // It's a reference to a declaration in another module, so first get the symbol name. + // It's a reference to a declaration in another module, so first get the symbol name. String mangledName = getMangledName(getASTBuilder(), decl); Index index = Index(m_mangledExportPool.add(mangledName)); @@ -3425,7 +3425,7 @@ NodeBase* Module::findExportFromMangledName(const UnownedStringSlice& slice) if (m_mangledExportSymbols.getCount() == 0) { m_mangledExportSymbols.add(nullptr); - } + } } const Index index = m_mangledExportPool.findIndex(slice); @@ -3517,7 +3517,7 @@ SLANG_NO_THROW void SLANG_MCALL ComponentType::computeDependencyBasedHash( DigestBuilder builder; // A note on enums that may be hashed in as part of the following two function calls: - // + // // While enums are not guaranteed to be encoded the same way across all versions of // the compiler, part of hashing the linkage is hashing in the compiler version. // Consequently, any encoding differences as a result of different compiler versions @@ -3530,17 +3530,17 @@ SLANG_NO_THROW void SLANG_MCALL ComponentType::computeDependencyBasedHash( auto fileDeps = getFilePathDependencies(); for (auto& file : fileDeps) { - builder.addToDigest(file); + builder.append(file); } // Add the name and name override for the specified entry point // to the hash. auto entryPointName = getEntryPoint(entryPointIndex)->getName()->text; - builder.addToDigest(entryPointName); + builder.append(entryPointName); auto entryPointMangledName = getEntryPointMangledName(entryPointIndex); - builder.addToDigest(entryPointMangledName); + builder.append(entryPointMangledName); auto entryPointNameOverride = getEntryPointNameOverride(entryPointIndex); - builder.addToDigest(entryPointNameOverride); + builder.append(entryPointNameOverride); *outHash = builder.finalize(); } @@ -4310,7 +4310,7 @@ SpecializedComponentType::SpecializedComponentType( // An even longer-term strategy would be to allow type layout to // be performed on IR types, so taht we don't need to have front-end // code worrying about this stuff. - // + // for(auto arg : specializationArgs) { auto argType = as<Type>(arg.val); @@ -4394,7 +4394,7 @@ void SpecializedComponentType::updateDependencyBasedHash( { auto specializationArg = getSpecializationArg(i); auto argString = specializationArg.val->toString(); - builder.addToDigest(argString); + builder.append(argString); } slang::Digest baseHash; @@ -4515,8 +4515,8 @@ void Linkage::setFileSystem(ISlangFileSystem* inFileSystem) // Release what's there m_fileSystemExt.setNull(); - - // If nullptr passed in set up default + + // If nullptr passed in set up default if (inFileSystem == nullptr) { m_fileSystemExt = new Slang::CacheFileSystem(Slang::OSFileSystem::getExtSingleton()); @@ -4527,11 +4527,11 @@ void Linkage::setFileSystem(ISlangFileSystem* inFileSystem) { m_fileSystemExt = cacheFileSystem; } - else + else { if (m_requireCacheFileSystem) { - m_fileSystemExt = new Slang::CacheFileSystem(inFileSystem); + m_fileSystemExt = new Slang::CacheFileSystem(inFileSystem); } else { @@ -4542,7 +4542,7 @@ void Linkage::setFileSystem(ISlangFileSystem* inFileSystem) if (!m_fileSystemExt) { // Construct a wrapper to emulate the extended interface behavior - m_fileSystemExt = new Slang::CacheFileSystem(m_fileSystem); + m_fileSystemExt = new Slang::CacheFileSystem(m_fileSystem); } } } @@ -4588,7 +4588,7 @@ void Session::addBuiltinSource( DiagnosticSink sink(sourceManager, Lexer::sourceLocationLexer); RefPtr<FrontEndCompileRequest> compileRequest = new FrontEndCompileRequest( m_builtinLinkage, - nullptr, + nullptr, &sink); compileRequest->m_isStandardLibraryCode = true; @@ -4873,7 +4873,7 @@ char const* EndToEndCompileRequest::getDiagnosticOutput() return m_diagnosticOutput.begin(); } -SlangResult EndToEndCompileRequest::getDiagnosticOutputBlob(ISlangBlob** outBlob) +SlangResult EndToEndCompileRequest::getDiagnosticOutputBlob(ISlangBlob** outBlob) { if (!outBlob) return SLANG_E_INVALID_ARG; @@ -4927,7 +4927,7 @@ SlangResult _addLibraryReference(EndToEndCompileRequest* req, IArtifact* artifac } else { - // TODO(JS): + // TODO(JS): // Do we want to check the path exists? } @@ -5122,7 +5122,7 @@ SlangResult EndToEndCompileRequest::EndToEndCompileRequest::compile() return saveRes; } } - else if (m_dumpReproOnError && SLANG_FAILED(res)) + else if (m_dumpReproOnError && SLANG_FAILED(res)) { String reproFileName; SlangResult saveRes = SLANG_FAIL; @@ -5453,7 +5453,7 @@ SlangResult EndToEndCompileRequest::isParameterLocationUsed(Int entryPointIndex, if (!metadata) return SLANG_E_NOT_AVAILABLE; - + // TODO: optimize this with a binary search through a sorted list for (const auto& range : metadata->getUsedBindingRanges()) { diff --git a/tools/slang-unit-test/unit-test-digest-builder.cpp b/tools/slang-unit-test/unit-test-digest-builder.cpp new file mode 100644 index 000000000..b89b31a7b --- /dev/null +++ b/tools/slang-unit-test/unit-test-digest-builder.cpp @@ -0,0 +1,72 @@ +// unit-test-digest-utils.cpp + +#include "tools/unit-test/slang-unit-test.h" + +#include "../../source/core/slang-digest-builder.h" +#include "../../source/core/slang-digest-util.h" + +using namespace Slang; + +SLANG_UNIT_TEST(digestBuilder) +{ + // Raw numerical values, etc. + { + DigestBuilder builder; + + int64_t valueA = -1; + uint64_t valueB = 1; + builder.append(valueA); + builder.append(valueB); + + slang::Digest digest = builder.finalize(); + SLANG_CHECK(DigestUtil::toString(digest) == "5BA171E20898BDD205639013746F2679"); + } + + // List + { + DigestBuilder builder; + + List<int64_t> listA; + listA.add(1); + listA.add(2); + listA.add(3); + listA.add(4); + builder.append(listA); + + slang::Digest digest = builder.finalize(); + SLANG_CHECK(DigestUtil::toString(digest) == "9F66C130786A1A05E4731F71A3C5F172"); + } + + // UnownedStringSlice + { + DigestBuilder builder; + + UnownedStringSlice stringSlice = UnownedStringSlice("String Slice Test"); + builder.append(stringSlice); + + slang::Digest digest = builder.finalize(); + SLANG_CHECK(DigestUtil::toString(digest) == "5D6CC58E1824A4DFD0CF57395B603316"); + } + + // String + { + DigestBuilder builder; + + String str = String("String Test"); + builder.append(str); + + slang::Digest digest = builder.finalize(); + SLANG_CHECK(DigestUtil::toString(digest) == "DF5A79CC2170C7401CF0A506CEB0CE24"); + } + + // Digest + { + DigestBuilder builder; + + slang::Digest hash; + builder.append(hash); + + slang::Digest digest = builder.finalize(); + SLANG_CHECK(DigestUtil::toString(digest) == "4AE71336E44BF9BF79D2752E234818A5"); + } +} diff --git a/tools/slang-unit-test/unit-test-md5.cpp b/tools/slang-unit-test/unit-test-md5.cpp index 41f3d6cf6..1297d4f17 100644 --- a/tools/slang-unit-test/unit-test-md5.cpp +++ b/tools/slang-unit-test/unit-test-md5.cpp @@ -9,89 +9,39 @@ using namespace Slang; SLANG_UNIT_TEST(md5hash) { + // Empty string { - // Raw numerical values, etc. - MD5Context testCtx; - MD5HashGen testHashGen; - testHashGen.init(&testCtx); - - int64_t valueA = -1; - uint64_t valueB = 1; - testHashGen.update(&testCtx, valueA); - testHashGen.update(&testCtx, valueB); - - slang::Digest testA; - testHashGen.finalize(&testCtx, &testA); - - String testAString = DigestUtil::toString(testA); - SLANG_CHECK(testAString.equals(String("5BA171E20898BDD205639013746F2679"))); + MD5Context ctx; + MD5HashGen gen; + gen.init(&ctx); + slang::Digest digest; + gen.finalize(&ctx, &digest); + SLANG_CHECK(DigestUtil::toString(digest) == "D41D8CD98F00B204E9800998ECF8427E"); } + // One call to update() { - // List - MD5Context testCtx; - MD5HashGen testHashGen; - testHashGen.init(&testCtx); - - List<int64_t> listA; - listA.add(1); - listA.add(2); - listA.add(3); - listA.add(4); - testHashGen.update(&testCtx, listA); - - slang::Digest testB; - testHashGen.finalize(&testCtx, &testB); - - String testBString = DigestUtil::toString(testB); - SLANG_CHECK(testBString.equals(String("4352D88A78AA39750BF70CD6F27BCAA5"))); + MD5Context ctx; + MD5HashGen gen; + gen.init(&ctx); + const String str("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + gen.update(&ctx, str.getBuffer(), str.getLength()); + slang::Digest digest; + gen.finalize(&ctx, &digest); + SLANG_CHECK(DigestUtil::toString(digest) == "818C6E601A24F72750DA0F6C9B8EBE28"); } + // Two calls to update() { - // UnownedStringSlice - MD5Context testCtx; - MD5HashGen testHashGen; - testHashGen.init(&testCtx); - - UnownedStringSlice stringSlice = UnownedStringSlice("String Slice Test"); - testHashGen.update(&testCtx, stringSlice); - - slang::Digest testC; - testHashGen.finalize(&testCtx, &testC); - - String testCString = DigestUtil::toString(testC); - SLANG_CHECK(testCString.equals(String("5D6CC58E1824A4DFD0CF57395B603316"))); - } - - { - // String - MD5Context testCtx; - MD5HashGen testHashGen; - testHashGen.init(&testCtx); - - String str = String("String Test"); - testHashGen.update(&testCtx, str); - - slang::Digest testD; - testHashGen.finalize(&testCtx, &testD); - - String testDString = DigestUtil::toString(testD); - SLANG_CHECK(testDString.equals(String("DF5A79CC2170C7401CF0A506CEB0CE24"))); - } - - { - // Hash - MD5Context testCtx; - MD5HashGen testHashGen; - testHashGen.init(&testCtx); - - slang::Digest Hash; - testHashGen.update(&testCtx, Hash); - - slang::Digest testE; - testHashGen.finalize(&testCtx, &testE); - - String testEString = DigestUtil::toString(testE); - SLANG_CHECK(testEString.equals(String("4AE71336E44BF9BF79D2752E234818A5"))); + MD5Context ctx; + MD5HashGen gen; + gen.init(&ctx); + const String str1("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + const String str2("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."); + gen.update(&ctx, str1.getBuffer(), str1.getLength()); + gen.update(&ctx, str2.getBuffer(), str2.getLength()); + slang::Digest digest; + gen.finalize(&ctx, &digest); + SLANG_CHECK(DigestUtil::toString(digest) == "87D3CAECB0AB82FAAE84D60FDE994ACA"); } } |
