Fix stream parsing
authorAndres Nötzli <andres.noetzli@gmail.com>
Sat, 17 Jun 2017 00:42:22 +0000 (17:42 -0700)
committerGitHub <noreply@github.com>
Sat, 17 Jun 2017 00:42:22 +0000 (17:42 -0700)
commit0da095c3f5be79a85c085b4b46d7a0a0513ecdd6
treeb651858c19ac8afc6f8cebf69f958dfe97781b86
parentb3e1f7ade822d061f276a9477f1ea67fcb1f3a50
Fix stream parsing

This commit fixes bug 811. Bug 811 was caused because tokens were referring to
a buffer that was reallocated and thus the pointers were not valid anymore.

Background:
The buffered input stream avoids copying the whole input stream before handing
it to ANTLR (in contrast to the non-buffered input stream that first copies
everything into a buffer). This enables interactivity (e.g. with kind2) and may
save memory.
CVC4 uses it when reading from stdin in competition mode for the application
track (the incremental benchmarks) and in non-competition mode. To set the
CVC4_SMTCOMP_APPLICATION_TRACK flag, the {C,CXX}FLAGS have to be modified at
configure time.

Solution:
This commit fixes the issue by changing how a stream gets buffered. Instead of
storing the stream into a single buffer, CVC4 now stores each line in a
separate buffer, making sure that they do not have to move, keeping tokens
valid. The commit adds the LineBuffer class for managing those buffers. It
further modifies CVC4's LA and consume functions to use line number and
position within a line to index into the line buffer. This allows us to use the
standard mark()/etc. functions because they automatically store and
restore that state. The solution also (arguably) simplifies the code.

Disadvantages:
Tokens split across lines would cause problems (seems reasonable to me). One
allocation per line.

Alternatives considered:
Pull request 162 by Tim was a first attempt to solve the problem. The issues
with this solution are: memory usage (old versions of the buffer do not get
deleted), tokens split across buffers would be problematic, and
mark()/rewind()/etc. would have to be overwritten for the approach to work.
I had a partially working fix that used indexes into the stream instead of
pointers to memory. The solution stored the content of the stream into a
segmented buffer (lines were not guaranteed to be consecutive in memory. This
approach was working for basic use cases but had the following issues: ugly
casting (the solution requires casting the index to a pointer and storing it in
the input stream's nextChar because that's where ANTLR is taking the location
information from when creating a token), more modifications (not only would
this solution require overwriting more functions of the input stream such as
substr, it also requires changes to the use of GETCHARINDEX() in the Smt2
parser and AntlrInput::tokenText() for example), more complex code.
src/parser/Makefile.am
src/parser/antlr_input.cpp
src/parser/antlr_input.h
src/parser/antlr_line_buffered_input.cpp
src/parser/antlr_line_buffered_input.h
src/parser/input.h
src/parser/line_buffer.cpp [new file with mode: 0644]
src/parser/line_buffer.h [new file with mode: 0644]