| 117 | * would trip -Werror=unused-function. */ |
| 118 | static char *build_perl_nested_calls(int depth) __attribute__((unused)); |
| 119 | static char *build_perl_nested_calls(int depth) { |
| 120 | /* |
| 121 | * Header: "sub f { return $_[0]; }\nmy $x = " (~32 bytes) |
| 122 | * Per open: "f(" (2 bytes each) |
| 123 | * Inner literal: "1" (1 byte) |
| 124 | * Per close: ")" (1 byte each) |
| 125 | * Trailer: ";\n" (2 bytes) |
| 126 | * Null: 1 byte |
| 127 | * |
| 128 | * Total upper bound: 40 + depth*2 + 1 + depth + 3 = depth*3 + 44 |
| 129 | */ |
| 130 | size_t sz = (size_t)depth * 3 + 64; |
| 131 | char *buf = (char *)malloc(sz); |
| 132 | if (!buf) return NULL; |
| 133 | |
| 134 | char *p = buf; |
| 135 | p += snprintf(p, sz, "sub f { return $_[0]; }\nmy $x = "); |
| 136 | |
| 137 | /* NESTING_DEPTH levels of `f(` */ |
| 138 | for (int i = 0; i < depth; i++) { |
| 139 | *p++ = 'f'; |
| 140 | *p++ = '('; |
| 141 | } |
| 142 | |
| 143 | /* innermost literal */ |
| 144 | *p++ = '1'; |
| 145 | |
| 146 | /* matching closing parens */ |
| 147 | for (int i = 0; i < depth; i++) { |
| 148 | *p++ = ')'; |
| 149 | } |
| 150 | |
| 151 | /* statement terminator */ |
| 152 | p += snprintf(p, (size_t)(buf + sz - p), ";\n"); |
| 153 | |
| 154 | return buf; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * repro_issue471_glr_nested_ambiguity_terminates |