Skip to content

Commit 2520867

Browse files
File2String: preserve shader format comments when stripping C-style comments
1 parent 60849b7 commit 2520867

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

BuildTools/File2Include/script.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,37 @@ def strip_c_comments(text):
4141
line_has_comment = False
4242
line_has_noncomment_code = False
4343

44+
block_comment_buf = []
45+
line_comment_buf = []
46+
4447
def append_char(c):
4548
nonlocal line_has_noncomment_code
4649
current_line.append(c)
4750
if not c.isspace():
4851
line_has_noncomment_code = True
4952

53+
def append_text(s):
54+
for ch in s:
55+
append_char(ch)
56+
57+
def should_preserve_block_comment(comment_text):
58+
inner = comment_text[2:-2].strip()
59+
return inner.startswith("format")
60+
61+
def should_preserve_line_comment(comment_text):
62+
inner = comment_text[2:].strip()
63+
return inner.startswith("format")
64+
5065
def flush_line(add_newline):
5166
nonlocal current_line, line_has_comment, line_has_noncomment_code
5267

53-
# Remove lines that contain only a comment (possibly with whitespace around it)
5468
if not (line_has_comment and not line_has_noncomment_code and "".join(current_line).strip() == ""):
5569
result.extend(current_line)
5670
if add_newline:
5771
result.append("\n")
5872

5973
current_line = []
6074
line_has_noncomment_code = False
61-
62-
# If we are still inside a block comment, the next physical line is also comment-only so far
6375
line_has_comment = in_block_comment
6476

6577
i = 0
@@ -71,14 +83,33 @@ def flush_line(add_newline):
7183

7284
if in_line_comment:
7385
if c == "\n":
86+
comment_text = "".join(line_comment_buf)
87+
if should_preserve_line_comment(comment_text):
88+
append_text(comment_text)
89+
90+
line_comment_buf = []
7491
in_line_comment = False
7592
flush_line(True)
93+
i += 1
94+
continue
95+
96+
line_comment_buf.append(c)
7697
i += 1
7798
continue
7899

79100
if in_block_comment:
101+
block_comment_buf.append(c)
102+
80103
if c == "*" and nxt == "/":
104+
block_comment_buf.append("/")
105+
comment_text = "".join(block_comment_buf)
106+
81107
in_block_comment = False
108+
block_comment_buf = []
109+
110+
if should_preserve_block_comment(comment_text):
111+
append_text(comment_text)
112+
82113
i += 2
83114
continue
84115

@@ -112,12 +143,14 @@ def flush_line(add_newline):
112143
if c == "/" and nxt == "/":
113144
line_has_comment = True
114145
in_line_comment = True
146+
line_comment_buf = ["//"]
115147
i += 2
116148
continue
117149

118150
if c == "/" and nxt == "*":
119151
line_has_comment = True
120152
in_block_comment = True
153+
block_comment_buf = ["/*"]
121154
i += 2
122155
continue
123156

@@ -141,6 +174,11 @@ def flush_line(add_newline):
141174
append_char(c)
142175
i += 1
143176

177+
if in_line_comment:
178+
comment_text = "".join(line_comment_buf)
179+
if should_preserve_line_comment(comment_text):
180+
append_text(comment_text)
181+
144182
if current_line:
145183
if not (line_has_comment and not line_has_noncomment_code and "".join(current_line).strip() == ""):
146184
result.extend(current_line)

0 commit comments

Comments
 (0)