Closed bcfriesen closed 7 years ago
I need to re-read the indent
docs, but I think this will be fine. I am not a huge fan of automating source reformatting on check-in, but we should require ourselves to run indent on code before check-in. Is there a way to have indent
run in a check mode, where if it doesn't meet the coding style it causes an error? Passing the format check could be required before a merge, say.
Agreed, nothing should be automated. As far as I can tell, indent
has no "check" mode. So the decision to make is how much we care about this, viz. policing ourselves vs the RESIST arbiter checking each request. One could emulate a "check mode" with a script like the following:
bash-4.4$ cat checkpatch.sh
#!/bin/bash
INDENT="gindent"
INDENT_OPTS="-linux"
INPUT="$1"
FORMATTED_INPUT="${INPUT}.fmt"
${INDENT} ${INDENT_OPTS} ${INPUT} -o ${FORMATTED_INPUT}
if [ ! $(cmp -s ${INPUT} ${FORMATTED_INPUT}) ]; then
printf "%s\n" "ERROR: file ${INPUT} violates code style!"
fi
rm -f ${FORMATTED_INPUT}
That sounds viable, let us have that.
OK. I shall make a pull request momentarily with this script. Do you prefer a subdir called utils
or scripts
or misc
or some such?
Also which style do you prefer? I don't have a preference, only that we enforce the one we pick. Note also that, in addition to the myriad formatting flags to choose from, indent
also supports 3 "shortcut" styles: -gnu
(the GNU style), -linux
(the Linux kernel style), and -kr
(Kernighan & Ritchie).
The downside to doing it this way is that we don't actually get output like pep8
gives you: a list of what's wrong. What about pep7
Huh, I did not know pep7
was a thing. Yeah we could definitely do that. What style does it enforce?
Why it enforces PEP 7 style of course, which seems okay to me...
Oh. Ok then let's do that!
To bring our offline discussion back online, PEP 7 seems to be woefully out of sync with PEP 8 and depends on it unnecessarily. So we should use GNU indent
instead.
Further research turned up uncrustify
which is quite configurable. In commit 510ab18e4b9fc642cbe01c4d31032416046afece I've added a make target make format
for checking all the source, along with a configuration file that we can use to enforce consistent style. One simply needs to install uncrustify
for it to work.
In future, make sure to do make format
before committing to identify any noncompliant files. If you just want to run the formatter, you can do uncrustify -c uncrustify.cfg --replace --no-backup file.c
and it will format the source.
Since this will be pure C, we have the great luxury of being able to use GNU
indent
to do this for us.