diff --git a/contributors.txt b/contributors.txt index b9a7b6a2e..2c82eaffb 100644 --- a/contributors.txt +++ b/contributors.txt @@ -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 diff --git a/runtime/Cpp/runtime/src/NoViableAltException.cpp b/runtime/Cpp/runtime/src/NoViableAltException.cpp index b00bc69e8..273c208c7 100755 --- a/runtime/Cpp/runtime/src/NoViableAltException.cpp +++ b/runtime/Cpp/runtime/src/NoViableAltException.cpp @@ -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 buildConfigsRef(atn::ATNConfigSet *configs, bool deleteConfigs) { + if (deleteConfigs) { + return Ref(configs); + } else { + return Ref(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(); } diff --git a/runtime/Cpp/runtime/src/NoViableAltException.h b/runtime/Cpp/runtime/src/NoViableAltException.h index 94d43c54c..b15039d0c 100755 --- a/runtime/Cpp/runtime/src/NoViableAltException.h +++ b/runtime/Cpp/runtime/src/NoViableAltException.h @@ -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 _deadEndConfigs; /// The token object at the start index; the input stream might /// not be buffering tokens so get a reference to it. (At the