For a while I've been maintaining an ad-hoc script to check code style against our project's guidelines.
I'm aware of AStyle/Uncrustify and have used them, but they are best for once-off code-style cleanups, but impractical to just run all code through some automatic process after every commit.
To explain the issue a little more, yes - we have code-style documentation, but devs don't always follow exactly, and it's not really great to give them personal mail every time they make some tiny change that goes against some convention.
So I took the approach to deal with this like compiler warnings, report inconsistencies but leave it up to the developer to resolve (some of you may be familiar with Python's pep8 style checker). Once every so often we do style cleanups.
In general I'm very happy with the outcome (unlike most style checking tools), you get to get a lot of flexibility - so you can choose where to be strict and where to be relaxed about conventions (avoid too many false positives is important too).
Currently I'm using Python and Pygments to parse C source code (some C++ also but mainly C) and then my own script to check the tokenized results meet our conventions. This works fairly well, I don't need the full AST you can get with GCC or Clang for example, simply knowing if text is a comment, string, punctuation, preprocessor... etc. is enough (what you would get from syntax highlighting, or a source tokenizer).
But I did end up having to write quite a few utility functions for the source:
- extract the body of a block between brackets.
- find matching brackets.
- identify casts.
- inspecting whitespace around punctuation.
... nothing really advanced.
The exact conventions are probably not so important, but for an example here are some checks:
- Placement of braces for 'if' statement checks broken into multiple lines.
- Placement of
- Spaces around operators, braces, function calls, after commas.
- Indentation for multi-line if statements.
- Indentation consistency.
At this point I would be interested to move away from my own utility functions but still have a script which checks code-style, if there is something else well maintained solving similar problems.
I'd be surprised if there is not some other people doing similar projects to maintain consistent source style in this way, so I'm interested to know of any alternatives available.
Considering the workflow I have described, can anyone cite their experiences using a similar workflow, are there projects that already do this, I'm interested to see a precedent.