[cfe-dev] Returning original parsing state after parsing arbitrary tokens

Eli Friedman eli.friedman at gmail.com
Tue Oct 23 03:11:05 PDT 2012


On Tue, Oct 23, 2012 at 3:02 AM, Eli Friedman <eli.friedman at gmail.com> wrote:
> On Tue, Oct 23, 2012 at 2:44 AM, ja_comp <ja_comp at yahoo.com> wrote:
>> Assume that a program did the following:
>>
>> 1) Called EnterTokenStream with arbitrary tokens (followed by a Semicolon
>> and Eof)
>> 2) Called setSuppressAllDiagnostics(true) to keep from emitting errors (in
>> case the next step fails)
>> 3) Called ParseStatement to produce a StmtResult from the additional tokens
>> (which may or may not produce a valid result)
>> 4) Lexed tokens (if necessary) to remove any unlexed added tokens
>> 5) Called setSuppressAllDiagnostics(false) to restore error handling
>>
>> What variables and/or other states should be saved to return the
>> Preprocessor/Parser/Lexer to the state they were in before step 1?
>
> It's impossible to completely save/restore the state of semantic
> analysis short of using fork() or equivalent.

This is a slight exaggeration; that said, parsing a statement in the
general case can change essentially arbitrary pieces of the AST and
the lookup data-structures in Sema, particularly in C++ when template
instantiation is involved.  Even in C there are bits stored in the AST
like whether a declaration is used which can't be easily
saved/restored.

-Eli



More information about the cfe-dev mailing list