Improve preprocessor ouput format

Modify preprocessor.simple.vert to test spaces before parenthesis.
This commit is contained in:
chirsz-ever 2023-09-22 16:49:37 +08:00 committed by arcady-lunarg
parent 4c57db1595
commit 2bfacdac91
11 changed files with 48 additions and 26 deletions

View File

@ -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()

View File

@ -10,7 +10,7 @@
void main(){
void main() {
gl_Position = vec4(3 + 2 + 2 * 4 + 2 + 3 * 2);
}

View File

@ -7,6 +7,6 @@
#extension unknown_extension : require
int main(){
int main() {
}

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
#version 310 es
#line 1 2
#pragma something
void main(){ }
void main() { }

View File

@ -23,7 +23,7 @@
#line 8
void main(){
void main() {
gl_Position = vec4(10);
}

View File

@ -9,6 +9,6 @@
#pragma once
int main(){
int main() {
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
int x(){
int x() {
something that shouldnt compile;
}

View File

@ -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

View File

@ -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 += "\"";