From 4808a13a3d1e0c219e1adc15fe19be271ef79ce8 Mon Sep 17 00:00:00 2001 From: Kevin Traini Date: Fri, 9 May 2025 18:54:49 +0200 Subject: [PATCH] fix: Cpp $rule.text fail if $rule is nullptr With a grammar like this: ```antlr rule: (VAL | VAR { is_constant = false; }) name=IDENTIFIER (COLON type { type_name = $type.text; <-- we look this line })? (EQ value=expression { value = $value.tree; })? ``` When generating for Cpp target we get this specific result: ```cpp type_name = (antlrcpp::downCast(_localctx)->typeContext != nullptr ? _input->getText(antlrcpp::downCast(_localctx)->typeContext->start, antlrcpp::downCast(_localctx)->typeContext->stop) : nullptr); ``` If input doesn't match the `type` branch then we fallback on nullptr. As of cpp23 standard this line doesn't compile as it waits for `std::string`. AS `Token::getText` returns `std::string`, then fallback value should be empty string Signed-off-by: Kevin Traini --- tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg index e557fc811d..bdd3b6f6f3 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg @@ -979,7 +979,7 @@ RulePropertyRef_stopHeader(r) ::= "" RulePropertyRef_stop(r) ::= "(-> != nullptr ? (->->stop) : nullptr)" RulePropertyRef_textHeader(r) ::= "" -RulePropertyRef_text(r) ::= "(-> != nullptr ? _input->getText(->->start, ->->stop) : nullptr)" +RulePropertyRef_text(r) ::= "(-> != nullptr ? _input->getText(->->start, ->->stop) : \"\")" RulePropertyRef_ctxHeader(r) ::= "" RulePropertyRef_ctx(r) ::= "->"