Skip to content

Official Codebase of 3DCodeVerse

Python

glsl_shader: strip comments in one left-to-right scan — the two-pass stripper blanked real code after '// ... /*', so lint, defined_functions and compose's feedback check never saw it

gaoypenge12e183203a8
diff --git a/harness/codeverse3d/languages/glsl_shader/__init__.py b/harness/codeverse3d/languages/glsl_shader/__init__.py
index 842c2f6e..c8bf8889 100644
--- a/harness/codeverse3d/languages/glsl_shader/__init__.py
+++ b/harness/codeverse3d/languages/glsl_shader/__init__.py
@@ -54,8 +54,9 @@ MAIN_IMAGE_TRAILER = """
void main() { mainImage(fragColor, gl_FragCoord.xy); }
"""
-_BLOCK_COMMENT = re.compile(r"/\*.*?\*/", re.S)
-_LINE_COMMENT = re.compile(r"//[^\n]*")
+#: ONE left-to-right scan: a `//` inside a block comment and a `/*` inside a line comment
+#: are comment text. Two passes (blocks, then lines) blanked real code after `// … /*`.
+_COMMENT = re.compile(r"//[^\n]*|/\*.*?\*/", re.S)
_MAIN_IMAGE = re.compile(r"\bvoid\s+mainImage\s*\(")
_PLAIN_MAIN = re.compile(r"\bvoid\s+main\s*\(\s*(void)?\s*\)")
@@ -73,7 +74,7 @@ def strip_comments(text: str) -> str:
def _blank(m: re.Match[str]) -> str:
return re.sub(r"[^\n]", " ", m.group(0))
- return _LINE_COMMENT.sub(_blank, _BLOCK_COMMENT.sub(_blank, text))
+ return _COMMENT.sub(_blank, text)
def detect_convention(shader_src: str) -> str:
diff --git a/harness/codeverse3d/tracks/graphics.py b/harness/codeverse3d/tracks/graphics.py
index f80f36f0..dfc7bcdc 100644
--- a/harness/codeverse3d/tracks/graphics.py
+++ b/harness/codeverse3d/tracks/graphics.py
@@ -32,7 +32,12 @@ from codeverse3d.contracts.common import HARNESS_OWNED_SRC, TRACK_INFO, Language
from codeverse3d.contracts.plan import GraphicsPlan, Plan
from codeverse3d.contracts.run import RoundRecord
from codeverse3d.languages._gl_common import METRICS_NAME, SHEET_NAME, read_metrics
-from codeverse3d.languages.glsl_shader import COMMON_GLSL, FUNC_DEF, strip_comments
+from codeverse3d.languages.glsl_shader import (
+ COMMON_GLSL,
+ FUNC_DEF,
+ defined_functions,
+ strip_comments,
+)
from codeverse3d.orchestrator import TaskGroup
from codeverse3d.prompts import render
from codeverse3d.prompts.sections import Section, split_sections
@@ -169,7 +174,7 @@ def cookbook_functions(md: str) -> dict[str, Recipe]:
def defined_names(glsl: str) -> set[str]:
"""Function and ``#define`` names a GLSL file already defines (what a seed must not repeat)."""
names = set(_DEFINE.findall(glsl))
- names.update(m.group(1) for line in strip_comments(glsl).splitlines() if (m := FUNC_DEF.match(line)))
+ names.update(defined_functions(glsl))
return names
diff --git a/harness/tests/graphics/test_wrap_lint.py b/harness/tests/graphics/test_wrap_lint.py
index cd4c426b..a33b90d9 100644
--- a/harness/tests/graphics/test_wrap_lint.py
+++ b/harness/tests/graphics/test_wrap_lint.py
@@ -7,10 +7,12 @@ from codeverse3d.languages.glsl_shader import (
HEADER,
_check_file,
compose,
+ defined_functions,
detect_convention,
first_error,
lint_workspace,
parse_glsl_log,
+ strip_comments,
)
from codeverse3d.languages.opengl_python import lint_source
from codeverse3d.languages.opengl_python import lint_workspace as lint_program_ws
@@ -75,6 +77,19 @@ def test_glsl_lint_rules():
assert any(f.data["kind"] == "buffer_self_ref" for f in _check_file("src/buffer_a.frag", SHADER.replace("u_resolution", "u_buffer_a") + "\n", role="buffer_a"))
+def test_mixed_comments_blank_only_the_comments():
+ """A `/*` inside a `//` comment opens nothing: the two-pass stripper (blocks, then lines)
+ blanked everything up to a later `*/`, so the lint never saw the code in between."""
+ src = ("// tweak /* the fog here\nfloat fogAmt(float d) { return d; }\nuniform float u_extra;\n// end */\n"
+ "/* a // inside a block */ float x() { return 1.0; }\n")
+ stripped = strip_comments(src)
+ assert stripped.count("\n") == src.count("\n") and "uniform float u_extra;" in stripped
+ assert "tweak" not in stripped and "inside" not in stripped
+ assert defined_functions(src) == {"fogAmt": 2, "x": 5}
+ shader = src + SHADER
+ assert any(f.data["kind"] == "custom_uniform" for f in _check_file("src/shader.frag", shader, role="shader"))
+
+
def test_glsl_lint_workspace_missing_and_stray(tmp_ws):
rep = lint_workspace(tmp_ws)
assert not rep.passed and rep.findings[0].data["kind"] == "missing_entry"