diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-12-03 09:58:59 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-03 09:58:59 -0500 |
| commit | 9653dcc2c9d5d20d3d0e8918aaf1d5b09e963060 (patch) | |
| tree | aa1b42132361b2920d9f9301f0b0e44ea82d6b8c /tools | |
| parent | a3651d99fb8f3a046365d60751d1f3f806e48f7a (diff) | |
getStringHash on string literals (#1140)
* WIP getStringHash
* Have a use.
* Add slang-string-hash.h/.cpp
* Use StringSlicePool for holding strings for StringHash.
Add outputBuffer to string-literal-hash.slang so value can be tested.
Ignore the GlobalHashedStringLiterals instruction on emit.
* Add all the hashed string literals to ProgramLayout.
* Add reflection support for hashed string literals to reflection test.
* Fix string literal hash test.
* Small fixes to pass test suite.
* Fix issue in serialization where IRUse is not correctly initialized.
* Fix problem initializing IRUse for string hash pass.
Remove hack from slang-ir-specialize - specially handling if user is not null.
* * Use shared builder when replacing getStringHash
* Comments for functions in slang-ir-string-hash
* Do not allow zero length string literals. Could be allowed, but doing so would require StringSlicePool to have a special case (or some other mechanism)
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/slang-reflection-test/slang-reflection-test-main.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tools/slang-reflection-test/slang-reflection-test-main.cpp b/tools/slang-reflection-test/slang-reflection-test-main.cpp index a327f1960..9dc507250 100644 --- a/tools/slang-reflection-test/slang-reflection-test-main.cpp +++ b/tools/slang-reflection-test/slang-reflection-test-main.cpp @@ -33,6 +33,15 @@ static void writeRawChar(PrettyWriter& writer, int c) writeRaw(writer, buffer, buffer + 1); } + +static void writeHexChar(PrettyWriter& writer, int c) +{ + char v = char(c) + (c < 10 ? '0' : ('a' - 10)); + + char buffer[] = { v, 0 }; + writeRaw(writer, buffer, buffer + 1); +} + static void adjust(PrettyWriter& writer) { if (!writer.startOfLine) @@ -77,6 +86,43 @@ static void write(PrettyWriter& writer, char const* text, size_t length = 0) } } +static void writeEscapedString(PrettyWriter& writer, char const* text, size_t length) +{ + adjust(writer); + + writeRawChar(writer, '"'); + + for (size_t i = 0; i < length; ++i) + { + const char c = text[i]; + switch (c) + { + case '\n': write(writer, "\\n"); break; + case '\t': write(writer, "\\t"); break; + case '\b': write(writer, "\\b"); break; + case '\f': write(writer, "\\f"); break; + case '\0': write(writer, "\\0"); break; + case '"': write(writer, "\\\""); break; + default: + { + if (c < ' ' || int(c) >= 128) + { + // Not strictly right - as we should decode as a unicode code point and write that. + write(writer, "\\u00"); + writeHexChar(writer, (c >> 4) & 0xf); + writeHexChar(writer, c & 0xf); + } + else + { + writeRawChar(writer, c); + } + } + } + } + + writeRawChar(writer, '"'); +} + static void write(PrettyWriter& writer, uint64_t val) { adjust(writer); @@ -1011,6 +1057,36 @@ static void emitReflectionJSON( dedent(writer); write(writer, "\n]"); } + + { + SlangUInt count = programReflection->getHashedStringCount(); + if (count) + { + write(writer, ",\n\"hashedStrings\": {\n"); + indent(writer); + + for (SlangUInt i = 0; i < count; ++i) + { + if (i) + { + write(writer, ",\n"); + } + + size_t charsCount; + const char* chars = programReflection->getHashedString(i, &charsCount); + const int hash = spCalcStringHash(chars, charsCount); + + writeEscapedString(writer, chars, charsCount); + write(writer, ": "); + + write(writer, hash); + } + + dedent(writer); + write(writer, "\n}\n"); + } + } + dedent(writer); write(writer, "\n}\n"); } |
