views:

377

answers:

4

I have a codebase that is touched by many people. While most people make an effort to keep the code nicely formatted (e.g. consistent indentation and use of braces), some don't, and even those that do can't always do it because we all use different editors, so settings like spaces vs. tabs are different.

Is there any standard lint tool that checks that code is properly formatted, but doesn't actually change it (like indent but that returns only errors and warnings)?

While this question could be answered generally, my focus is on C and C++, because that's what this project is written in.

+3  A: 

The Linux kernel uses a tool that does exactly this - it's called checkpatch. You'd have to modify it to check your coding standards rather than theirs, but it could be a good basis to work from. (It is also designed for C code rather than C++).

caf
Is this part of the standard linux distributions?
Nathan Fellman
No, but it's just a perl script (available at the link in my answer: http://www.kernel.org/pub/linux/kernel/people/apw/checkpatch/ )
caf
+1  A: 

There are several programs that can do formatting for you automatically on save (such as Eclipse). You can have format settings that everyone can use ensuring the same formatting.

It is also possible to automatically apply such formatting when code is committed. When you use SVN, the system to do this is called svn hooks. This basically starts a program to process (or check and deny) the formatting when a commit happens.

This site explains how you can make your own. But also ones already exist to do this.

Thirler
Note that for SVN, changing files in a pre-commit hook is not recommended: http://svnbook.red-bean.com/en/1.5/svn.reposadmin.create.html#svn.reposadmin.create.hooks
sbi
You are right, better is to deny the commit when the formatting is wrong (but that only works if all formatting is automated)
Thirler
+2  A: 

Google uses cpplint. This is their style guide.

Nikola Smiljanić
Too bad that Google's cpplint isn't easily configured.
dalle
+1  A: 

Take a look at Vera++, it has a number of rules already available but the nice part is that you can modify them or write your own.

Eugen Constantin Dinca