maint: tighten curly brace syntax checking

Now that hanging brace offenders have been fixed, we can automate
the check, and document our style.  Done as a separate commit from
code changes, to make it easier to just backport code changes, if
that is ever needed.

* cfg.mk (sc_curly_braces_style): Catch hanging braces.
* docs/hacking.html.in: Document it.
* HACKING: Regenerate.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2014-09-03 16:18:19 -06:00
parent 625e04a86e
commit bc7e63d39c
3 changed files with 70 additions and 6 deletions

27
HACKING
View File

@ -461,6 +461,33 @@ But if negating a complex condition is too ugly, then at least add braces:
x = y;
}
Use hanging braces for compound statements: the opening brace of a compound
statement should be on the same line as the condition being tested. Only
top-level function bodies, nested scopes, and compound structure declarations
should ever have { on a line by itself.
void
foo(int a, int b)
{ // correct - function body
int 2d[][] = {
{ // correct - complex initialization
1, 2,
},
};
if (a)
{ // BAD: compound brace on its own line
do_stuff();
}
{ // correct - nested scope
int tmp;
if (a < b) { // correct - hanging brace
tmp = b;
b = a;
a = tmp;
}
}
}
Preprocessor
============

18
cfg.mk
View File

@ -918,12 +918,18 @@ sc_require_if_else_matching_braces:
$(_sc_search_regexp)
sc_curly_braces_style:
@files=$$($(VC_LIST_EXCEPT) | grep '\.[ch]$$'); \
$(GREP) -nHP \
'^\s*(?!([a-zA-Z_]*for_?each[a-zA-Z_]*) ?\()([_a-zA-Z0-9]+( [_a-zA-Z0-9]+)* ?\()?(\*?[_a-zA-Z0-9]+(,? \*?[_a-zA-Z0-9\[\]]+)+|void)\) ?\{' \
$$files && { echo '$(ME): Non-K&R style used for curly' \
'braces around function body, see' \
'HACKING' 1>&2; exit 1; } || :
@files=$$($(VC_LIST_EXCEPT) | grep '\.[ch]$$'); \
if $(GREP) -nHP \
'^\s*(?!([a-zA-Z_]*for_?each[a-zA-Z_]*) ?\()([_a-zA-Z0-9]+( [_a-zA-Z0-9]+)* ?\()?(\*?[_a-zA-Z0-9]+(,? \*?[_a-zA-Z0-9\[\]]+)+|void)\) ?\{' \
$$files; then \
echo '$(ME): Non-K&R style used for curly braces around' \
'function body, see HACKING' 1>&2; exit 1; \
fi; \
if $(GREP) -A1 -En ' ((if|for|while|switch) \(|(else|do)\b)[^{]*$$'\
$$files | $(GREP) '^[^ ]*- *{'; then \
echo '$(ME): Use hanging braces for compound statements,' \
'see HACKING' 1>&2; exit 1; \
fi
sc_prohibit_windows_special_chars_in_filename:
@files=$$($(VC_LIST_EXCEPT) | grep '[:*?"<>|]'); \

View File

@ -593,6 +593,37 @@
}
</pre>
<p>Use hanging braces for compound statements: the opening brace
of a compound statement should be on the same line as the
condition being tested. Only top-level function bodies, nested
scopes, and compound structure declarations should ever have {
on a line by itself.
</p>
<pre>
void
foo(int a, int b)
{ // correct - function body
int 2d[][] = {
{ // correct - complex initialization
1, 2,
},
};
if (a)
{ // BAD: compound brace on its own line
do_stuff();
}
{ // correct - nested scope
int tmp;
if (a &lt; b) { // correct - hanging brace
tmp = b;
b = a;
a = tmp;
}
}
}
</pre>
<h2><a name="preprocessor">Preprocessor</a></h2>
<p>Macros defined with an ALL_CAPS name should generally be