How to update all C/C++ identifier names in a project

c, c++, refactoring, regex

Solution

Although I am one of those that frequently points out that names with leading underscores may be reserved, I strongly recommend you don't do this unless you are experiencing problems caused by the names. Making this global change will make your version control system less useful than it might otherwise be by causing all sorts of spurious diffs. Also, there is a chance of creating duplicate names.

Also, there are many underscore prefixed names that re perfectly valid. One thinks immediately of `__LINE__` and `__FILE__` etc, and of all the names of non-standard functions that may be provided by your specific implementation. Filtering out those names will be far from trivial (I would say next to impossible), certainly a simple Perl or sed script will not be enough.

So instead, I would encourage you to change the names on a file by file basis as you make other changes to the code. Change your programming standards to avoid the leading underscore names and write new code in accordance with the standards.

Problem

After frequently coming across recommendation not to use leading and double underscores in C/C++ identifiers I decided to fix all our sources once and for all. What I need to do now is convert `_Identifier` to `Identifier_`. Should I use a specialized tool for this task of regular expressions will do for this job? In latter case what is the pattern to match C/C++ identifier?

Original source