Commit f0ed454
committed
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<Variable_declarationContext *>(_localctx)->typeContext != nullptr ? _input->getText(antlrcpp::downCast<Variable_declarationContext *>(_localctx)->typeContext->start, antlrcpp::downCast<Variable_declarationContext *>(_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 <[email protected]>1 parent cb85012 commit f0ed454
File tree
1 file changed
+1
-1
lines changed- tool/resources/org/antlr/v4/tool/templates/codegen/Cpp
1 file changed
+1
-1
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
979 | 979 | | |
980 | 980 | | |
981 | 981 | | |
982 | | - | |
| 982 | + | |
983 | 983 | | |
984 | 984 | | |
985 | 985 | | |
| |||
0 commit comments