From ad3f10bbd018021ad66b40c48d7a7369c4821c4a Mon Sep 17 00:00:00 2001 From: Jeff Bolz Date: Tue, 10 Mar 2020 10:23:07 -0500 Subject: [PATCH] EXT_debug_printf - make escape sequences better match C/C++ --- Test/baseResults/spv.debugPrintf.frag.out | 6 +- .../spv.debugPrintf_Error.frag.out | 7 ++ Test/spv.debugPrintf.frag | 6 ++ Test/spv.debugPrintf_Error.frag | 11 +++ .../preprocessor/PpScanner.cpp | 78 ++++++++++--------- gtests/Spv.FromFile.cpp | 1 + 6 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 Test/baseResults/spv.debugPrintf_Error.frag.out create mode 100644 Test/spv.debugPrintf_Error.frag diff --git a/Test/baseResults/spv.debugPrintf.frag.out b/Test/baseResults/spv.debugPrintf.frag.out index bb6a91f6..dbd0b4ed 100644 --- a/Test/baseResults/spv.debugPrintf.frag.out +++ b/Test/baseResults/spv.debugPrintf.frag.out @@ -1,7 +1,7 @@ spv.debugPrintf.frag // Module Version 10000 // Generated by (magic number): 80008 -// Id's are bound by 13 +// Id's are bound by 17 Capability Shader Extension "SPV_KHR_non_semantic_info" @@ -11,6 +11,8 @@ spv.debugPrintf.frag EntryPoint Fragment 4 "main" ExecutionMode 4 OriginUpperLeft 6: String "ASDF \ ? \ %d %d %d" + 13: String "ABAZ" + 15: String "B#$B1Z" Source GLSL 450 SourceExtension "GL_EXT_debug_printf" Name 4 "main" @@ -23,5 +25,7 @@ spv.debugPrintf.frag 4(main): 2 Function None 3 5: Label 12: 2 ExtInst 11(NonSemantic.DebugPrintf) 1(DebugPrintf) 6 8 9 10 + 14: 2 ExtInst 11(NonSemantic.DebugPrintf) 1(DebugPrintf) 13 + 16: 2 ExtInst 11(NonSemantic.DebugPrintf) 1(DebugPrintf) 15 Return FunctionEnd diff --git a/Test/baseResults/spv.debugPrintf_Error.frag.out b/Test/baseResults/spv.debugPrintf_Error.frag.out new file mode 100644 index 00000000..31ddd9d0 --- /dev/null +++ b/Test/baseResults/spv.debugPrintf_Error.frag.out @@ -0,0 +1,7 @@ +spv.debugPrintf_Error.frag +ERROR: 0:7: 'string' : Expected hex value in escape sequence +ERROR: 0:10: 'string' : Invalid escape sequence +ERROR: 2 compilation errors. No code generated. + + +SPIR-V is not generated for failed compile or link diff --git a/Test/spv.debugPrintf.frag b/Test/spv.debugPrintf.frag index 81b64288..3dff6298 100644 --- a/Test/spv.debugPrintf.frag +++ b/Test/spv.debugPrintf.frag @@ -4,4 +4,10 @@ void main() { debugPrintfEXT("ASDF \\ \? \x5C %d %d %d", 1, 2, 3); + + // ABA{backspace}Z + debugPrintfEXT("\x41\x000042\x41\x8Z"); + + // B#${bell, aka \a}B1Z + debugPrintfEXT("\102\043\44\7\1021Z"); } diff --git a/Test/spv.debugPrintf_Error.frag b/Test/spv.debugPrintf_Error.frag new file mode 100644 index 00000000..c1980a7f --- /dev/null +++ b/Test/spv.debugPrintf_Error.frag @@ -0,0 +1,11 @@ +#version 450 +#extension GL_EXT_debug_printf : enable + +void main() +{ + // invalid hex sequence + debugPrintfEXT("\xZ"); + + // not an octal sequence + debugPrintfEXT("\8"); +} diff --git a/glslang/MachineIndependent/preprocessor/PpScanner.cpp b/glslang/MachineIndependent/preprocessor/PpScanner.cpp index 6cdadec9..e0f44f8b 100644 --- a/glslang/MachineIndependent/preprocessor/PpScanner.cpp +++ b/glslang/MachineIndependent/preprocessor/PpScanner.cpp @@ -1045,28 +1045,31 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken) case 't': ch = 0x09; break; case 'v': ch = 0x0b; break; case 'x': - // two character hex value - nextCh = getch(); - if (nextCh >= '0' && nextCh <= '9') - nextCh -= '0'; - else if (nextCh >= 'A' && nextCh <= 'F') - nextCh -= 'A' - 10; - else if (nextCh >= 'a' && nextCh <= 'f') - nextCh -= 'a' - 10; - else - pp->parseContext.ppError(ppToken->loc, "Expected hex value in escape sequence", "string", ""); - ch = nextCh * 0x10; - nextCh = getch(); - if (nextCh >= '0' && nextCh <= '9') - nextCh -= '0'; - else if (nextCh >= 'A' && nextCh <= 'F') - nextCh -= 'A' - 10; - else if (nextCh >= 'a' && nextCh <= 'f') - nextCh -= 'a' - 10; - else - pp->parseContext.ppError(ppToken->loc, "Expected hex value in escape sequence", "string", ""); - ch += nextCh; - break; + // Hex value, arbitrary number of characters. Terminated by the first + // non-hex digit + { + int numDigits = 0; + ch = 0; + while (true) { + nextCh = getch(); + if (nextCh >= '0' && nextCh <= '9') + nextCh -= '0'; + else if (nextCh >= 'A' && nextCh <= 'F') + nextCh -= 'A' - 10; + else if (nextCh >= 'a' && nextCh <= 'f') + nextCh -= 'a' - 10; + else { + ungetch(); + break; + } + numDigits++; + ch = ch * 0x10 + nextCh; + } + if (numDigits == 0) { + pp->parseContext.ppError(ppToken->loc, "Expected hex value in escape sequence", "string", ""); + } + break; + } case '0': case '1': case '2': @@ -1075,20 +1078,23 @@ int TPpContext::tStringInput::scan(TPpToken* ppToken) case '5': case '6': case '7': - // three character octal value - nextCh = getch() - '0'; - if (nextCh > 3) - pp->parseContext.ppError(ppToken->loc, "Expected octal value in escape sequence", "string", ""); - ch = nextCh * 8 * 8; - nextCh = getch() - '0'; - if (nextCh > 7) - pp->parseContext.ppError(ppToken->loc, "Expected octal value in escape sequence", "string", ""); - ch += nextCh * 8; - nextCh = getch() - '0'; - if (nextCh > 7) - pp->parseContext.ppError(ppToken->loc, "Expected octal value in escape sequence", "string", ""); - ch += nextCh; - break; + // Octal value, up to three octal digits + { + int numDigits = 1; + ch = nextCh - '0'; + while (numDigits < 3) { + nextCh = getch(); + if (nextCh >= '0' && nextCh <= '7') + nextCh -= '0'; + else { + ungetch(); + break; + } + numDigits++; + ch = ch * 8 + nextCh; + } + break; + } default: pp->parseContext.ppError(ppToken->loc, "Invalid escape sequence", "string", ""); break; diff --git a/gtests/Spv.FromFile.cpp b/gtests/Spv.FromFile.cpp index 110313e9..244fa1ec 100644 --- a/gtests/Spv.FromFile.cpp +++ b/gtests/Spv.FromFile.cpp @@ -310,6 +310,7 @@ INSTANTIATE_TEST_CASE_P( "spv.dataOutIndirect.frag", "spv.dataOutIndirect.vert", "spv.debugPrintf.frag", + "spv.debugPrintf_Error.frag", "spv.demoteDisabled.frag", "spv.deepRvalue.frag", "spv.depthOut.frag",