Take the 2-minute tour ×

Please suggest a tool that could automate replacing like:

Mutex staticMutex = Mutex(m_StaticMutex.Handle());


boost::unique_lock<boost::mutex> lock(m_StaticMutex);

As you see, the arguments must be taken into account. Is there a way simpler than regular expressions?

share|improve this question
The editors search-replace should work well in this case I think. –  Joachim Pileborg Nov 23 '11 at 7:25
You could Extract Method, passing in m_StaticMutex as a parameter, and make sure you're calling that method everywhere, then replace the body with the boost code, then Inline Method. –  Carl Manaster Nov 23 '11 at 15:23
Most "somewhat better" editors have regex search-replace that can span several lines, and some even allow captures. This will work if the snippets are reasonably similar, so only maybe the variable name or whitespace differs. Other than that, you'll hardly find a tool for that kind of job, because such a task would require considerably more intelligence than what so-called "artificial intelligence" is typically able to provide. Basically, you'd be trying to tell the machine "write new code for me like this and that", which maybe works in Star Trek, but not here and now. –  Damon Nov 23 '11 at 15:48

2 Answers 2

up vote 3 down vote accepted

If you can do this with a modest amount of manual work (even including "search and replace") then this answer isn't relevant.

If the code varies too much (indentation, comments, different variable names) and there's a lot of these, you might need a Program Transformation tool. Such tools tend to operate on program representations such as abstract syntax trees, and consequently are not bother by layout or whitespace or even numbers that are spelled differently because of radix, but actually have the same value.

Our DMS Software Reengineering Toolkit is one of these, and has a C++ Front End.

You'd need to give it a rewrite rule something like the following:

 domain Cpp; -- tell DMS to use the C++ front end for parsing and prettyprinting

 rule replace_mutex(i:IDENTIFIER):statements -> statements
      "Mutex \i = Mutex(m_StaticMutex.Handle());  
       \i.Wait();" =>
      "boost::unique_lock<boost::mutex> lock(m_StaticMutex);";

The use of the metavariable \i in both places will ensure that the rule only fires if the name is exactly the same in both places.

It isn't clear to me precisely what you are trying to accomplish; it sort of looks like you want to replace each private mutex with one global one, but I'm not a boost expert. If you tried to do that, I'd expect your program to behave differently.

share|improve this answer

If those lines appear frequently in your code, similar formatted, just with different variable names, but not "too" frequently (<200~300 times), I would suggest you use an editor with record-replay capabilities (for example Visual Studio under Windows). Record the steps to replace the 2 lines by the new one (but keep the variable name). Then repeat "search for Mutex" - "replay macro" as often as you need it.

Of course, this specific case should be also solvable for all occurences at once by any text editor with good "Find-and-Replace in Files" capabilities.

share|improve this answer

Your Answer


