Skip to content

Commit

Permalink
Improve preprocessor ouput format
Browse files Browse the repository at this point in the history
Modify preprocessor.simple.vert to test spaces before parenthesis.
  • Loading branch information
chirsz-ever committed Sep 22, 2023
1 parent 4c57db1 commit 28c8b81
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Test/baseResults/preprocessor.edge_cases.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@



void main(){
void main() {
gl_Position = vec4(3 + 2 + 2 * 4 + 2 + 3 * 2);
}

2 changes: 1 addition & 1 deletion Test/baseResults/preprocessor.extensions.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

#extension unknown_extension : require

int main(){
int main() {
}

4 changes: 2 additions & 2 deletions Test/baseResults/preprocessor.function_macro.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

2 changes: 1 addition & 1 deletion Test/baseResults/preprocessor.line.frag.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version 310 es
#line 1 2
#pragma something
void main(){ }
void main() { }

2 changes: 1 addition & 1 deletion Test/baseResults/preprocessor.line.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#line 8

void main(){
void main() {
gl_Position = vec4(10);
}

Expand Down
2 changes: 1 addition & 1 deletion Test/baseResults/preprocessor.pragma.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#pragma once

int main(){
int main() {
}

25 changes: 15 additions & 10 deletions Test/baseResults/preprocessor.simple.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -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));
[]. ++ --
[].++ --
+ - * % / - ! ~
<< >> < > <= >=
== !=
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int x(){
int x() {
something that shouldnt compile;
}

5 changes: 5 additions & 0 deletions Test/preprocessor.simple.vert
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ struct S {
vec3 b = a.yzy; \
vec3 b = a.z;

void bar(int x) {}

void foo()
{
S s;
Expand All @@ -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

Expand Down
26 changes: 19 additions & 7 deletions glslang/MachineIndependent/ShaderLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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 += "\"";
Expand Down

0 comments on commit 28c8b81

Please sign in to comment.