-2

I'm working on making a change to a large C++ codebase where I need to change a method call to an equivalent function call, e.g. someExpression.foo() becomes bar(someExpression).

A few more examples of the sort of transformation I'm looking for:

  • (itemA + itemB).foo() becomes bar(itemA + itemB)
  • outerFunction(object->method(), arg2 + val, innerFunction(arg3)).foo() becomes bar(outerFunction(object->method(), arg2 + val, innerFunction(arg3)))

Basically, the expression proceeding the method call to foo() could be fairly complex. But foo() comes up a lot of times in my codebase, so I'm really hoping there's some way I can automate this change rather than having to laboriously edit everything manually.

Can this be done with a regex? Or is there another tool I can use to make this change?

3
  • en.wikipedia.org/wiki/VIATRA might provide it for you, but I'm not an expert on this. Commented Dec 1, 2022 at 17:51
  • it probably can be done with a regex, finding all your edge cases will be fun though. Commented Dec 1, 2022 at 18:03
  • It doesn't need to be perfect. Even if it only catches 80% of all of the instances of foo() it'd still be a big help. Commented Dec 1, 2022 at 18:13

1 Answer 1

0

I've slapped together a regex that seems to work so far, based on the non-recursive parenthesis-balancing regex from this answer.

([^\s\(]*(?:\((?:[^)(]|\((?:[^)(]|\((?:[^)(]|\([^)(]*\))*\))*\))*\))?)\.foo\(\)

That won't match any valid expression proceeding .foo() but will match most of them. There may be bugs with this regex, but I haven't found any or hit any mismatches so far. And it contains a capture group that can be used for replacement, i.e.: bar(\1)

I'm going to leave this question open though because there are almost certainly better methods than this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.