Skip to content

Commit 92e9f27

Browse files
author
Daniel Kroening
authored
Merge pull request #294 from danpoe/remove-returns
Restore returns fix
2 parents afdcc7d + 0c6ad51 commit 92e9f27

File tree

8 files changed

+110
-16
lines changed

8 files changed

+110
-16
lines changed

regression/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DIRS = ansi-c cbmc cpp
1+
DIRS = ansi-c cbmc cpp goto-instrument
22

33
test:
44
$(foreach var,$(DIRS), $(MAKE) -C $(var) test || exit 1;)

regression/goto-instrument/Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
default: tests.log
3+
4+
test:
5+
@if ! ../test.pl -c ../chain.sh ; then \
6+
../failed-tests-printer.pl ; \
7+
exit 1; \
8+
fi
9+
10+
tests.log:
11+
@if ! ../test.pl -c ../chain.sh ; then \
12+
../failed-tests-printer.pl ; \
13+
exit 1; \
14+
fi
15+
16+
show:
17+
@for dir in *; do \
18+
if [ -d "$$dir" ]; then \
19+
vim -o "$$dir/*.c" "$$dir/*.out"; \
20+
fi; \
21+
done;
22+
23+
clean:
24+
@for dir in *; do \
25+
rm -f tests.log; \
26+
if [ -d "$$dir" ]; then \
27+
cd "$$dir"; \
28+
rm -f *.out *.gb; \
29+
cd ..; \
30+
fi \
31+
done
32+

regression/goto-instrument/chain.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
SRC=../../../src
4+
5+
GC=$SRC/goto-cc/goto-cc
6+
GI=$SRC/goto-instrument/goto-instrument
7+
8+
OPTS=$1
9+
NAME=${2%.c}
10+
11+
$GC $NAME.c -o $NAME.gb
12+
$GI $OPTS $NAME.gb
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
int foo()
3+
{
4+
int x;
5+
return x;
6+
}
7+
8+
int main()
9+
{
10+
foo();
11+
return 0;
12+
}
13+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
ret.c
3+
"--escape-analysis --dump-c"
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
int foo()
3+
{
4+
int x;
5+
int y;
6+
if (x)
7+
{
8+
return 0;
9+
}
10+
int z;
11+
return 1;
12+
}
13+
14+
int main()
15+
{
16+
foo();
17+
return 0;
18+
}
19+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
ret.c
3+
"--escape-analysis --dump-c"
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring

src/goto-programs/remove_returns.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,26 +316,29 @@ bool remove_returnst::restore_returns(
316316
// replace "fkt#return_value=x;" by "return x;"
317317
code_returnt return_code(assign.rhs());
318318

319-
// now turn the `return' into `assignment'
320-
i_it->type=RETURN;
321-
i_it->code=return_code;
319+
// the assignment might be a goto target
320+
i_it->make_skip();
321+
i_it++;
322322

323-
// remove the subsequent goto (and possibly dead)
324-
goto_programt::instructionst::iterator next=i_it;
325-
++next;
326-
assert(next!=goto_program.instructions.end());
323+
while(!i_it->is_goto() && !i_it->is_end_function())
324+
{
325+
assert(i_it->is_dead());
326+
i_it++;
327+
}
327328

328-
if(next->is_dead())
329+
if(i_it->is_goto())
329330
{
330-
assert(to_code_dead(next->code).symbol()==
331-
return_code.return_value());
332-
next=goto_program.instructions.erase(next);
333-
assert(next!=goto_program.instructions.end());
331+
goto_programt::const_targett target=i_it->get_target();
332+
assert(target->is_end_function());
333+
}
334+
else
335+
{
336+
assert(i_it->is_end_function());
337+
i_it=goto_program.instructions.insert(i_it, *i_it);
334338
}
335339

336-
assert(next->is_goto());
337-
// i_it remains valid
338-
goto_program.instructions.erase(next);
340+
i_it->make_return();
341+
i_it->code=return_code;
339342
}
340343
}
341344

0 commit comments

Comments
 (0)