Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128742: Remove comments from find_assignment_target result #128743

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,30 @@ def test_error_if_plain_with_comment(self):
"""
self.run_cases_test(input, output)

def test_PyStackRef_FromPyObjectNew_with_comment(self):
input = """
inst(OP, (-- value)) {
// Comment is ok
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have more /* ... */ comments as well. And more than one comment as well? something like:

inst(OP, (-- value)) {
    // Comment is ok
	value = ...
}

inst(OP, (-- value)) {
    /* comment is ok */
	value = ...
}

inst(OP, (-- value)) {
    /* 
     comment is ok 
	*/
	value = ...
}

inst(OP, (-- value)) {
	// comment
	// comment
	value = ...
}

inst(OP, (-- value)) {
	// comment
	value = ...
	// comment
}

(I don't know which comments are actually accepted). The output being always the same, you can parametrize the test as follows:

output = ... 
line = "value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));"
for src in [
	("//", line),
	("// comment", line),
	("/* comment */", line),
    ...
]:
	lines = textwrap.indent('\n'.join(src), " " * 4)
	input = f"inst(OP, (-- value)) {{\n{lines}\n}}"
	with self.subTest(src=src):
		self.run_cases_test(input, output)

value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));
}
"""

output = """
TARGET(OP) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP);
_PyStackRef value;
// Comment is ok
value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));
stack_pointer[0] = value;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
self.run_cases_test(input, output)

def test_error_if_pop(self):
input = """
inst(OP, (left, right -- res)) {
Expand Down
7 changes: 4 additions & 3 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ def find_assignment_target(node: parser.InstDef, idx: int) -> list[lexer.Token]:
offset = 0
for tkn in reversed(node.block.tokens[: idx]):
if tkn.kind in {"SEMI", "LBRACE", "RBRACE"}:
return node.block.tokens[idx - offset : idx]
tokens = node.block.tokens[idx - offset : idx]
while tokens and tokens[0].kind == "COMMENT":
tokens = tokens[1:]
return tokens
offset += 1
return []

Expand All @@ -406,8 +409,6 @@ def find_stores_outputs(node: parser.InstDef) -> list[lexer.Token]:
continue
lhs = find_assignment_target(node, idx)
assert lhs
while lhs and lhs[0].kind == "COMMENT":
lhs = lhs[1:]
if len(lhs) != 1 or lhs[0].kind != "IDENTIFIER":
continue
name = lhs[0]
Expand Down
Loading