Are there any lint tools for C and C++ that check formatting? [closed]
Asked Answered
T

4

14

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.

Thirtytwo answered 14/3, 2010 at 11:5 Comment(1)
Not sure why this was closed, after it's been open for 13 years, received over 10k views and 14 upvotes. The statistics alone would indicate that it's very much on topicThirtytwo
M
11

Google uses cpplint. This is their style guide.

Monoclinous answered 14/3, 2010 at 11:40 Comment(1)
Too bad that Google's cpplint isn't easily configured.Cusk
T
5

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++).

Tatiana answered 14/3, 2010 at 11:22 Comment(2)
Is this part of the standard linux distributions?Thirtytwo
No, but it's just a perl script (available at the link in my answer: kernel.org/pub/linux/kernel/people/apw/checkpatch )Tatiana
S
2

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.

Sharronsharyl answered 14/3, 2010 at 15:8 Comment(0)
S
0

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.

Sihunn answered 14/3, 2010 at 11:27 Comment(5)
Note that for SVN, changing files in a pre-commit hook is not recommended: svnbook.red-bean.com/en/1.5/…Orsa
You are right, better is to deny the commit when the formatting is wrong (but that only works if all formatting is automated)Sihunn
It is recommended to not use SVN at all (Linus Torvalds recommends git :)Compliment
@Compliment - its a bad idea to follow what Linus recs; git doesn't scale to large repos and has other known problems.Cepeda
@GoodPerson - it doesn't? o.0 I have a git repo that is several gigabytes large consisting mostly of binary files.Compliment

© 2022 - 2024 — McMap. All rights reserved.