views:

65

answers:

4

I'm looking for a very flexible C code formatting solution.

Here's the context: I need to convert some external C code to my organization's "coding rules". For example, these rules demand that I put the type of each variable in its name (int_foo, pfloat_bar, etc.). Most of these rules are completely absurd but I don't have a choice.

I would prefer not to do this by hand, so I'm looking for some piece of software that would allow me to automatically format C code using powerful directives (for example to prefix all variable names with their type).

Does such a software exist?

+1  A: 

I don't think anything is going to automatically change names for you, particularly to that rather unique 'verbose Hungarian'.

If I had to do this, I'd grab the source of the Eclipse CDT, and I'd add a new plugin for the purpose that walked the AST and did the job.

It would probably take about 4 working days, so that gives you a tradeoff to chew upon.

bmargulies
A: 

Hi,

I am sorry I do not have any software in mind that might help, I cant think of any that is that "flexible"

However I do have a recommendation, in my past I have had to do large amounts of manipulation on text and csv files including adding and reformatting sections in a programmatic fashion. To do this I used mostly scripts.

So if you can't find anything at all I would recommend you try to write some scripts for yourself instead. For example, it would not be hard at all to write a script to perform the rule you explain above. Parse every file looking for a declaration(using regular expressions most likely) and then when you find one make sure the variable name has the type in it, and if it does not, add it and then make a note to change that original word everywhere else you see it. For scripting I use perl, it is an amazingly simple syntax and it has powerful built in regular expressions.

It gets harder the larger the program and more files there are, but if you just start slow with one file at a time, and one small change at a time, and compile every time to see that it still works, you can probably figure out a decent set of rules to do most of the work. Ultimately you will probably have to go through all the files still, but this should eliminate a huge chunk of the work - that was my strategy at least.

gnomed
A: 

Use uncrustify to fix the mechanical formatting (braces, indenting, etc.). It takes a little while to set it up just right but well worth the effort.

For the variable renaming, you might want to consider using something like exuberant ctags to identify all the variables and functions. Then parse the output to process each variable. Utilities like sed or interpreters like Perl can do in-place search and replace on whole directories of files.

Incidentally, if you don't find an easy tool to do this, it would be a great excuse to keep the variables named in their original form. ;-)

Amardeep
A: 

Most of the code formatters are non-intrusive, i.e. they modify indentation and bracing style, but they do not modify the variable names (or method names for that matter).

My approach would be to use sed.

First, manually, find the replacements that need to be done and collect them in a file, e.g. subst.sed, as

s/foo/int_foo/g

Then, write a small shell script like following:

for file in `find . -name \*`
    cp file file.bak    
    sed -f subst.sed file.bak > file

If a variable is int in one file but something else, e.g. float, in a different file, then the substituition pattern has to be constructed on per-file basis, e.g. <file>.subst.sed.

However, in one file, if the a variable is of different type in different function

// file1.cpp
void f() {
    int foo;
}
void g() {
    float foo;
}

then it is very difficult to automate :-(.

ArunSaha