RFR: 8361494: [IR Framework] Escape too much in replacement of placeholder

Manuel Hässig mhaessig at openjdk.org
Thu Jul 10 14:54:39 UTC 2025


On Thu, 10 Jul 2025 12:54:55 GMT, Marc Chevalier <mchevalier at openjdk.org> wrote:

> In `RawIRNode::regex`, the call to `String::replaceAll` doesn't quote the replace string.
> 
> Meaning that in the IR rule
> 
> @IR(failOn = {IRNode.ALLOC_OF, "\\w+"})
> 
> The interpreted `\w` is interpreted as a group reference, and we get
> 
> java.lang.IllegalArgumentException: Illegal group reference
> 
> so we should write instead
> 
> @IR(failOn = {IRNode.ALLOC_OF, "\\\\w+"})
> 
> To mean the interpreted string `\\w`, to mean an escaped single backslash. Same goes with `$` (used for nested classes).
> 
> Since we don't want to refer to groups (and anyway, there are not in `IRNode.IS_REPLACED`), we just quote the replacement string with `java.util.regex.Matcher.quoteReplacement` to make it more usable.
> 
> Note that you would still need to write `\$` since the `$` is the end of string regex, and needs to be escaped at the regex level (and not at the string, so it's not `$`, since `$` is not a special character). Before the fix, it should be `\\\$`. Phew! Regexes are bad enough, let's not escape them manually twice!
> 
> In `test/hotspot/jtreg/compiler/c2/TestMergeStores.java`, that makes us save 1344 backslashes.
> 
> Thanks,
> Marc

Thank you for implementing this improvement @marc-chevalier! Nice to see this paper cut getting eliminated.

I have a small suggestion, but this looks good to me regardless.

test/hotspot/jtreg/compiler/lib/ir_framework/driver/irmatching/irrule/checkattribute/parsing/RawIRNode.java line 65:

> 63:                 nodeRegex = regexForVectorIRNode(nodeRegex, vmInfo, bound);
> 64:             } else if (userPostfix.isValid()) {
> 65:                 nodeRegex = nodeRegex.replaceAll(IRNode.IS_REPLACED, java.util.regex.Matcher.quoteReplacement(userPostfix.value()));

Perhaps you might want to `import java.util.regex.Matcher` to make it a bit more concise?

-------------

Marked as reviewed by mhaessig (Committer).

PR Review: https://git.openjdk.org/jdk/pull/26243#pullrequestreview-3006077669
PR Review Comment: https://git.openjdk.org/jdk/pull/26243#discussion_r2197957431


More information about the hotspot-compiler-dev mailing list