The Clang frontend sometimes fails on the following assertion when launched with -serialize-diagnostic-file <x>:
Assertion failed: (BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance"), function ~BitstreamWriter, file BitstreamWriter.h, line 125.
This was first noticed when passing an unknown command-line argument to -cc1.
It turns out the DiagnosticConsumer::finish() function should be called as soon as processing of all source files ends, but there are some code paths where that doesn't happen:
- when command line parsing fails in cc1_main(),
- when !Act.PrepareToExecute(*this) or !createTarget() evaluate to true in CompilerInstance::ExecuteAction and the function returns early.
This patch ensures finish() is called in all those code paths.
This combination of command-line arguments causes GenerateHeaderModuleAction::PrepareToExecuteAction to return false and !Act.PrepareToExecute(*this) == true.