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

Trim leading/trailing spaces after topformflat #165

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
35 changes: 22 additions & 13 deletions cvise/passes/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@ def __format(self, test_case, check_sanity):
with (
CloseableTemporaryFile(mode='w+', dir=tmp) as backup,
CloseableTemporaryFile(mode='w+', dir=tmp) as tmp_file,
CloseableTemporaryFile(mode='w+', dir=tmp) as stripped_tmp_file,
):
backup.close()
with open(test_case) as in_file:
try:
cmd = [self.external_programs['topformflat'], self.arg]
proc = subprocess.run(cmd, stdin=in_file, capture_output=True, text=True)
with subprocess.Popen(cmd, stdin=in_file, stdout=subprocess.PIPE, text=True) as proc:
Copy link
Contributor Author

@emaxx-google emaxx-google Jan 22, 2025

Choose a reason for hiding this comment

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

I took the liberty to change this to read stdout per line, instead of accumulating everything in the memory. It's not a big speedup (transform and __count_instances load the whole file anyway), but the optimization felt easily doable...

for line in proc.stdout:
if not line.isspace():
tmp_file.write(line)
linebreak = '\n' if line.endswith('\n') else ''
stripped_tmp_file.write(line.strip() + linebreak)
except subprocess.SubprocessError:
return

for line in proc.stdout.splitlines(keepends=True):
if not line.isspace():
tmp_file.write(line)
tmp_file.close()
stripped_tmp_file.close()

# we need to check that sanity check is still fine
if check_sanity:
shutil.copy(test_case, backup.name)
shutil.copy(tmp_file.name, test_case)
try:
check_sanity()
except InsaneTestCaseError:
shutil.copy(backup.name, test_case)
# if we are not the first lines pass, we should bail out
if self.arg != '0':
self.bailout = True
# try the stripped file first, fall back to the original stdout if needed
candidates = [stripped_tmp_file.name, tmp_file.name]
for candidate in candidates:
shutil.copy(candidate, test_case)
try:
check_sanity()
except InsaneTestCaseError:
pass
else:
return
shutil.copy(backup.name, test_case)
# if we are not the first lines pass, we should bail out
if self.arg != '0':
self.bailout = True
else:
shutil.copy(tmp_file.name, test_case)

Expand Down
30 changes: 21 additions & 9 deletions cvise/tests/test_lines.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from cvise.passes.lines import LinesPass
from cvise.utils.error import InsaneTestCaseError
from cvise.utils.externalprograms import find_external_programs


Expand Down Expand Up @@ -50,14 +51,14 @@ def test_new_reformatting_arg0(input_path, external_programs):
write_file(input_path, 'int f() {\nchar x;\n}\nnamespace foo\n{\n}\n')
p = LinesPass('0', external_programs)
p.new(input_path, check_sanity=lambda: True)
assert read_file(input_path) == 'int f() { char x; }\n namespace foo { }\n'
assert read_file(input_path) == 'int f() { char x; }\nnamespace foo { }\n'


def test_new_reformatting_arg1(input_path, external_programs):
write_file(input_path, 'int f() {\nchar x;\n}\nnamespace foo\n{\n}\n')
p = LinesPass('1', external_programs)
p.new(input_path, check_sanity=lambda: True)
assert read_file(input_path) == 'int f() {\n char x;\n }\n namespace foo {\n }\n'
assert read_file(input_path) == 'int f() {\nchar x;\n}\nnamespace foo {\n}\n'


def test_transform_deletes_individual_line(input_path, external_programs):
Expand All @@ -72,16 +73,16 @@ def test_transform_deletes_individual_line(input_path, external_programs):
def test_transform_deletes_lines_range(input_path, external_programs):
write_file(input_path, 'A;\nB;\nC;\nD;\nE;\nF;\nG;\nH;\n')
p = LinesPass('0', external_programs)
state = p.new(input_path)
state = p.new(input_path, check_sanity=lambda: True)
all_transforms = collect_all_transforms(p, state, input_path)
# deletion of a half:
assert 'A;\n B;\n C;\n D;\n' in all_transforms
assert ' E;\n F;\n G;\n H;\n' in all_transforms
assert 'A;\nB;\nC;\nD;\n' in all_transforms
assert 'E;\nF;\nG;\nH;\n' in all_transforms
# deletion of a quarter:
assert ' C;\n D;\n E;\n F;\n G;\n H;\n' in all_transforms
assert 'A;\n B;\n E;\n F;\n G;\n H;\n' in all_transforms
assert 'A;\n B;\n C;\n D;\n G;\n H;\n' in all_transforms
assert 'A;\n B;\n C;\n D;\n E;\n F;\n' in all_transforms
assert 'C;\nD;\nE;\nF;\nG;\nH;\n' in all_transforms
assert 'A;\nB;\nE;\nF;\nG;\nH;\n' in all_transforms
assert 'A;\nB;\nC;\nD;\nG;\nH;\n' in all_transforms
assert 'A;\nB;\nC;\nD;\nE;\nF;\n' in all_transforms


def test_advance_on_success(input_path, external_programs):
Expand All @@ -95,3 +96,14 @@ def test_advance_on_success(input_path, external_programs):
state = advance_until(p, state, input_path, lambda s: 'bar' in s)
p.advance_on_success(input_path, state)
assert read_file(input_path) == ' bar;\n'


def test_new_reformatting_keeps_spaces_if_needed(input_path, external_programs):
def check_sanity():
if ' char' not in read_file(input_path):
raise InsaneTestCaseError([], '')

write_file(input_path, 'int f() {\n char x;}\n')
p = LinesPass('1', external_programs)
p.new(input_path, check_sanity)
assert read_file(input_path) == 'int f() {\n char x;\n}\n'
2 changes: 1 addition & 1 deletion tests/test_cvise.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ def test_simple_reduction(self):
self.check_cvise(
'blocksort-part.c',
'-c "gcc -c blocksort-part.c && grep nextHi blocksort-part.c"',
['#define nextHi', '#define nextHi '],
['#define nextHi', '#define nextHi'],
)
Loading