diff --git a/Test/baseResults/hlsl.pp.expand.frag.out b/Test/baseResults/hlsl.pp.expand.frag.out index adfe02c12..97df910ad 100644 --- a/Test/baseResults/hlsl.pp.expand.frag.out +++ b/Test/baseResults/hlsl.pp.expand.frag.out @@ -9,7 +9,7 @@ struct A float4 a; float4 b; float4 c = { 1, 2, 3, 4 }; - float4 d = {({ {(({ 1, 2, 3, 4 }))} })}, { { 1, 2, 3, 4 } }; + float4 d = { ({ { ( ({ 1, 2, 3, 4 })) } }) }, { { 1, 2, 3, 4 } }; }; void main() diff --git a/Test/baseResults/preprocessor.edge_cases.vert.out b/Test/baseResults/preprocessor.edge_cases.vert.out index 17d9049bf..6a4ad4a7e 100644 --- a/Test/baseResults/preprocessor.edge_cases.vert.out +++ b/Test/baseResults/preprocessor.edge_cases.vert.out @@ -10,7 +10,7 @@ -void main(){ +void main() { gl_Position = vec4(3 + 2 + 2 * 4 + 2 + 3 * 2); } diff --git a/Test/baseResults/preprocessor.extensions.vert.out b/Test/baseResults/preprocessor.extensions.vert.out index 57d2e199f..59c6e8567 100644 --- a/Test/baseResults/preprocessor.extensions.vert.out +++ b/Test/baseResults/preprocessor.extensions.vert.out @@ -7,6 +7,6 @@ #extension unknown_extension : require -int main(){ +int main() { } diff --git a/Test/baseResults/preprocessor.function_macro.vert.out b/Test/baseResults/preprocessor.function_macro.vert.out index 010251e0c..84b0a8727 100644 --- a/Test/baseResults/preprocessor.function_macro.vert.out +++ b/Test/baseResults/preprocessor.function_macro.vert.out @@ -15,10 +15,10 @@ -int main(){ +int main() { gl_Position = vec4(3 + 1, 3 + 4, 3 + 1); gl_Position = vec4(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12); gl_Position = vec4(4 + 3 + 3); - gl_Position = 4 + 3 + F . a; + gl_Position = 4 + 3 + F.a; } diff --git a/Test/baseResults/preprocessor.line.frag.out b/Test/baseResults/preprocessor.line.frag.out index 3e2206f88..76877943c 100644 --- a/Test/baseResults/preprocessor.line.frag.out +++ b/Test/baseResults/preprocessor.line.frag.out @@ -1,5 +1,5 @@ #version 310 es #line 1 2 #pragma something -void main(){ } +void main() { } diff --git a/Test/baseResults/preprocessor.line.vert.out b/Test/baseResults/preprocessor.line.vert.out index 2bf090327..fbd851cb2 100644 --- a/Test/baseResults/preprocessor.line.vert.out +++ b/Test/baseResults/preprocessor.line.vert.out @@ -23,7 +23,7 @@ #line 8 -void main(){ +void main() { gl_Position = vec4(10); } diff --git a/Test/baseResults/preprocessor.pragma.vert.out b/Test/baseResults/preprocessor.pragma.vert.out index ebe1e4a74..8fa3d338d 100644 --- a/Test/baseResults/preprocessor.pragma.vert.out +++ b/Test/baseResults/preprocessor.pragma.vert.out @@ -9,6 +9,6 @@ #pragma once -int main(){ +int main() { } diff --git a/Test/baseResults/preprocessor.simple.vert.out b/Test/baseResults/preprocessor.simple.vert.out index 57b020c67..b54e0a004 100644 --- a/Test/baseResults/preprocessor.simple.vert.out +++ b/Test/baseResults/preprocessor.simple.vert.out @@ -11,15 +11,15 @@ - float fn(float x){ return x + 4.0;} + float fn(float x) { return x + 4.0; } -int main(){ +int main() { gl_Position = vec4(1); gl_Position = clamp(1, 2, 3); gl_Position = vec4(1); gl_Position = vec4(1, 2); gl_Position = vec4(fn(3)); - []. ++ -- + [].++ -- + - * % / - ! ~ << >> < > <= >= == != @@ -46,16 +46,21 @@ struct S { +void bar(int x) { } + void foo() { S s; - s . member2 + s . member1; - s . member3 . zyx; - s . member2 . xxyz; - s . member2 . yyz; - s . member2 . xxyz(); - s . member2 . yzy; - vec3 a = vec3(0);vec3 b = a . zxyz;vec3 b = a . xxyz;vec3 b = a . yyz;vec3 b = a . xxyz();vec3 b = a . yzy;vec3 b = a . z; + s.member2 + s.member1; + s.member3.zyx; + s.member2.xxyz; + s.member2.yyz; + s.member2.xxyz(); + s.member2.yzy; + for (int i = 0; i < 100; i = i + 1) { + bar(i) + } + vec3 a = vec3(0); vec3 b = a.zxyz; vec3 b = a.xxyz; vec3 b = a.yyz; vec3 b = a.xxyz(); vec3 b = a.yzy; vec3 b = a.z; yyz; diff --git a/Test/baseResults/preprocessor.success_if_parse_would_fail.vert.out b/Test/baseResults/preprocessor.success_if_parse_would_fail.vert.out index 624813a01..ed1ad0c0e 100644 --- a/Test/baseResults/preprocessor.success_if_parse_would_fail.vert.out +++ b/Test/baseResults/preprocessor.success_if_parse_would_fail.vert.out @@ -1,4 +1,4 @@ -int x(){ +int x() { something that shouldnt compile; } diff --git a/Test/preprocessor.simple.vert b/Test/preprocessor.simple.vert index 788df76ce..6c4485008 100644 --- a/Test/preprocessor.simple.vert +++ b/Test/preprocessor.simple.vert @@ -46,6 +46,8 @@ struct S { vec3 b = a.yzy; \ vec3 b = a.z; +void bar(int x) {} + void foo() { S s; @@ -55,6 +57,9 @@ void foo() s.member2.yzy(); s.member2.xyz(); s.member2.yzy; + for(int i = 0;i < 100; i = i + 1) { + bar (i) + } FUN_MAC() yzy diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp index 2c77e2fd2..9a42acae9 100644 --- a/glslang/MachineIndependent/ShaderLang.cpp +++ b/glslang/MachineIndependent/ShaderLang.cpp @@ -1068,8 +1068,8 @@ struct DoPreprocessing { EShOptimizationLevel, EShMessages) { // This is a list of tokens that do not require a space before or after. - static const std::string unNeededSpaceTokens = ";()[]"; - static const std::string noSpaceBeforeTokens = ","; + static const std::string noNeededSpaceBeforeTokens = ";)[].,"; + static const std::string noNeededSpaceAfterTokens = ".(["; glslang::TPpToken ppToken; parseContext.setScanner(&input); @@ -1142,6 +1142,7 @@ struct DoPreprocessing { }); int lastToken = EndOfInput; // lastToken records the last token processed. + std::string lastTokenName; do { int token = ppContext.tokenize(ppToken); if (token == EndOfInput) @@ -1160,12 +1161,23 @@ struct DoPreprocessing { // Output a space in between tokens, but not at the start of a line, // and also not around special tokens. This helps with readability // and consistency. - if (!isNewString && !isNewLine && lastToken != EndOfInput && - (unNeededSpaceTokens.find((char)token) == std::string::npos) && - (unNeededSpaceTokens.find((char)lastToken) == std::string::npos) && - (noSpaceBeforeTokens.find((char)token) == std::string::npos)) { - outputBuffer += ' '; + if (!isNewString && !isNewLine && lastToken != EndOfInput) { + // left parenthesis need a leading space, except it is in a function-call-like context. + // examples: `for (xxx)`, `a * (b + c)`, `vec(2.0)`, `foo(x, y, z)` + if (token == '(') { + if (lastToken != PpAtomIdentifier || + lastTokenName == "if" || + lastTokenName == "for" || + lastTokenName == "while" || + lastTokenName == "switch") + outputBuffer += ' '; + } else if ((noNeededSpaceBeforeTokens.find((char)token) == std::string::npos) && + (noNeededSpaceAfterTokens.find((char)lastToken) == std::string::npos)) { + outputBuffer += ' '; + } } + if (token == PpAtomIdentifier) + lastTokenName = ppToken.name; lastToken = token; if (token == PpAtomConstString) outputBuffer += "\"";