Merge pull request #2501 from gedimitr/master

Avoid double deletion when NoViableAltException is thrown
This commit is contained in:
Terence Parr 2019-11-15 09:08:59 -08:00 committed by GitHub
commit 54daca92f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View File

@ -215,6 +215,7 @@ YYYY/MM/DD, github id, Full name, email
2018/12/20, WalterCouto, Walter Couto, WalterCouto@users.noreply.github.com
2018/12/23, youkaichao, Kaichao You, youkaichao@gmail.com
2019/02/06, ralucado, Cristina Raluca Vijulie, ralucris.v[at]gmail[dot]com
2019/02/23, gedimitr, Gerasimos Dimitriadis, gedimitr@gmail.com
2019/03/13, base698, Justin Thomas, justin.thomas1@gmail.com
2019/03/18, carlodri, Carlo Dri, carlo.dri@gmail.com
2019/05/02, askingalot, Andy Collins, askingalot@gmail.com

View File

@ -9,6 +9,20 @@
using namespace antlr4;
namespace {
// Create a normal shared pointer if the configurations are to be deleted. If not, then
// the shared pointer is created with a deleter that does nothing.
Ref<atn::ATNConfigSet> buildConfigsRef(atn::ATNConfigSet *configs, bool deleteConfigs) {
if (deleteConfigs) {
return Ref<atn::ATNConfigSet>(configs);
} else {
return Ref<atn::ATNConfigSet>(configs, [](atn::ATNConfigSet *){});
}
}
}
NoViableAltException::NoViableAltException(Parser *recognizer)
: NoViableAltException(recognizer, recognizer->getTokenStream(), recognizer->getCurrentToken(),
recognizer->getCurrentToken(), nullptr, recognizer->getContext(), false) {
@ -17,12 +31,10 @@ NoViableAltException::NoViableAltException(Parser *recognizer)
NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input,Token *startToken,
Token *offendingToken, atn::ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx, bool deleteConfigs)
: RecognitionException("No viable alternative", recognizer, input, ctx, offendingToken),
_deadEndConfigs(deadEndConfigs), _deleteConfigs(deleteConfigs), _startToken(startToken) {
_deadEndConfigs(buildConfigsRef(deadEndConfigs, deleteConfigs)), _startToken(startToken) {
}
NoViableAltException::~NoViableAltException() {
if (_deleteConfigs)
delete _deadEndConfigs;
}
Token* NoViableAltException::getStartToken() const {
@ -30,5 +42,5 @@ Token* NoViableAltException::getStartToken() const {
}
atn::ATNConfigSet* NoViableAltException::getDeadEndConfigs() const {
return _deadEndConfigs;
return _deadEndConfigs.get();
}

View File

@ -27,10 +27,9 @@ namespace antlr4 {
private:
/// Which configurations did we try at input.index() that couldn't match input.LT(1)?
atn::ATNConfigSet* _deadEndConfigs;
// Flag that indicates if we own the dead end config set and have to delete it on destruction.
bool _deleteConfigs;
/// Shared pointer that conditionally deletes the configurations (based on flag
/// passed during construction)
Ref<atn::ATNConfigSet> _deadEndConfigs;
/// The token object at the start index; the input stream might
/// not be buffering tokens so get a reference to it. (At the