5

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?

Evan Teran
  • 80,654
  • 26
  • 169
  • 231

4 Answers4

2

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.

pnezis
  • 11,459
  • 1
  • 35
  • 37
  • I am accepting this answer. Even though I have settled on a slow but steady code review. This answer was most directly applicable to my question. – Evan Teran Nov 10 '11 at 16:37
2

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.

bilash.saha
  • 6,918
  • 2
  • 32
  • 40
1

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.

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479
1

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

Community
  • 1
  • 1
moooeeeep
  • 28,315
  • 14
  • 88
  • 166
  • cppcheck is proving to be very good at finding basic common issues with the code, but definitely has less of a focus on style. – Evan Teran Nov 10 '11 at 16:38