Evan Teran
Evan Teran

Reputation: 90422

Tool to detect style issues for c++ code?

I am inheriting a fairly large code base which unfortunately exhibits a lot of "bad habits". One of my largest personal pet peeves is declaring several variables in one expression like this:

int x, y, z;

personally, I prefer:

int x;
int y;
int z;

this allows me to easily adjust the types individually, and avoids issues with pointer types like this:

int *x, y, z; // whoops I meant to make y and z pointers too!

Also, I'd love to detect when a type whose size is bigger than sizeof(void*) is being passed by value.

There are other "style" issues I'd like to detect and correct as well, but these are the most annoying IMO. What are the best tools for this?

Upvotes: 5

Views: 317

Answers (4)

moooeeeep
moooeeeep

Reputation: 32502

You may try cppcheck for static code analysis. This question and its answers provide more hints on tools for static code analysis.

Upvotes: 1

bilash.saha
bilash.saha

Reputation: 7296

I use Artistic Style or astyle for formatting my codes of C++ and JAVA since about 2 Years.It can be customized in great detail.Probably there are many better ones now...

But Its Very useful to me.

Upvotes: 2

pnezis
pnezis

Reputation: 12321

One of the best tools I have used, for checking the style of C++ files is the KWStyle. However I am not quite sure if it supports all your requirements.

Upvotes: 2

My feeling is that the style you want to use is specific to your needs. So you need to customize a tool to do the checks for you.

I believe that your example (assuming that your code base is large enough to make the effort worthwhile) is a very good case for compiler customization.

Recent (i.e. 4.6) versions of GCC can be extended thru plugins, and you can also extend GCC by customizing it using GCC MELT, which is a high-level domain specific language to ease the development of GCC extensions.

Of course, to extend GCC (either by coding plugins in C, or extensions in MELT) you do have to understand its internal representations (notably Gimple & Tree), which does take some work.

P.S. I am the main developer of GCC MELT.

Upvotes: 1

Related Questions