pavelkolodin
pavelkolodin

Reputation: 2967

Automatic refactoring

Please suggest a tool that could automate replacing like:

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

to

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?

Upvotes: 2

Views: 231

Answers (2)

Ira Baxter
Ira Baxter

Reputation: 95324

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.

Upvotes: 3

Doc Brown
Doc Brown

Reputation: 20044

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.

Upvotes: 0

Related Questions