check-headers: add header usage checks for .c files
PATCH 1/2 check-headers: add header usage checks for .c files
Teach check-header.sh to ensure that the first included header in .c files is either git-compat-util.h, builtin.h, or cache.h.
Ensure that common-cmds.h is only included by help.c.
Move the logic into functions so that we can skip parts of the check.
---
This depends on my previous patch that adds check-header.sh.
check-headers.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 5 deletions(-)
check-headers.sh +61 -5
@@ -1,5 +1,9 @@ #!/bin/sh +# This script is run via make. +# "make check-headers SKIP_HEADER_CHECK=1" skips the header dependency check. +# "make check-headers SKIP_USAGE_CHECK=1" skips the header usage check. + exit_code=0 maybe_exit () {
@@ -14,11 +18,11 @@ maybe_exit () { fi } -for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h -do +check_header () { + header="$1" case "$header" in common-cmds.h) - # should only be included by help.c + # should only be included by help.c, not checked ;; *) subdir=$(dirname "$header") &&
@@ -29,6 +33,58 @@ do maybe_exit $? ;; esac -done +} + +check_headers () { + for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h + do + check_header "$header" + done +} + +check_header_usage () { + first=$(grep '^#include' "$1" | + head -n1 | + sed -e 's,#include ",,' -e 's,"$,,') + + case "$first" in + cache.h|builtin.h|git-compat-util.h) + # happy + ;; + *) + echo "error: $1 must #include \"git-compat-util.h\" before $first" + maybe_exit 1 + ;; + esac + + if grep common-cmds.h "$1" >/dev/null && test "$1" != help.c + then + echo "error: $1 must not include common-cmds.h" + maybe_exit 1 + fi +} + +check_usage () { + # Implementation files should #include git-compat-util.h, cache.h, + # or builtin.h before any others. + for impl in *.c builtin/*.c + do + check_header_usage "$impl" + done +} + +main () { + if test -z "$SKIP_HEADER_CHECK" + then + check_headers "$@" + fi + + if test -z "$SKIP_USAGE_CHECK" + then + check_usage + fi + + exit $exit_code +} -exit $exit_code +main "$@"
Signature · 1 line
2.1.0.241.ga16d620
PATCH 2/2 cleanups: ensure that git-compat-util.h is included first
CodingGuidelines states that the first #include in C files should be git-compat-util.h or another header file that includes it, such as cache.h or builtin.h.
Tweak the tiny minority of files that do not follow this advice. This makes "make check-headers SKIP_HEADER_CHECK=1" happy.
--- bulk-checkin.c | 1 + bulk-checkin.h | 2 -- http.c | 1 + merge-recursive.c | 2 +- sigchain.c | 2 +- test-regex.c | 2 +- test-sigchain.c | 2 +- varint.c | 1 + varint.h | 2 -- 9 files changed, 7 insertions(+), 8 deletions(-)
bulk-checkin.c +1 -0
@@ -1,6 +1,7 @@/* * Copyright (c) 2011, Google Inc. */ +#include "cache.h" #include "bulk-checkin.h" #include "csum-file.h" #include "pack.h"
bulk-checkin.h +0 -2
@@ -4,8 +4,6 @@#ifndef BULK_CHECKIN_H #define BULK_CHECKIN_H -#include "cache.h" -extern int index_bulk_checkin (unsigned char sha1[],int fd,size_t size,enum object_type type,const char *path,unsigned flags);
http.c +1 -0
@@ -1,3 +1,4 @@ +#include "git-compat-util.h" #include "http.h" #include "pack.h" #include "sideband.h"
merge-recursive.c +1 -1
@@ -3,8 +3,8 @@* Fredrik Kuivinen. * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 */ -#include "advice.h" #include "cache.h" +#include "advice.h" #include "cache-tree.h" #include "commit.h" #include "blob.h"
sigchain.c +1 -1
@@ -1,5 +1,5 @@ -#include "sigchain.h" #include "cache.h" +#include "sigchain.h" #define SIGCHAIN_MAX_SIGNALS 32
test-regex.c +1 -1
@@ -1,4 +1,4 @@ -#include <git-compat-util.h> +#include "git-compat-util.h" int main (int argc,char **argv) {
test-sigchain.c +1 -1
@@ -1,5 +1,5 @@ -#include "sigchain.h" #include "cache.h" +#include "sigchain.h" #define X (f) \ static void f(int sig) { \
varint.c +1 -0
@@ -1,3 +1,4 @@ +#include "git-compat-util.h" #include "varint.h" uintmax_t decode_varint (const unsigned char **bufp)
varint.h +0 -2
@@ -1,8 +1,6 @@#ifndef VARINT_H #define VARINT_H -#include "git-compat-util.h" -extern int encode_varint (uintmax_t ,unsigned char *);extern uintmax_t decode_varint (const unsigned char **);
Signature · 1 line
2.1.0.241.ga16d620
Quoted 11 lines · David Aguilar
Teach
check-header.shto ensure that the first included header in .c files is eithergit-compat-util.h,builtin.h, orcache.h.Ensure that
common-cmds.his only included byhelp.c.Move the logic into functions so that we can skip parts of the check.
Signed-off-by David Aguilar <[email protected]>---
This depends on my previous patch that addscheck-header.sh.
...
check-headers.sh do · line 41
Quoted 4 lines
check-headers.sh +4
-0+check_headers () { + for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h + do + check_header "$header"
Hmmmm, doesn't check_header run "$@" as a command?
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · David Aguilar
Teach
check-header.shto ensure that the first included header in .c files is eithergit-compat-util.h,builtin.h, orcache.h.Ensure that
common-cmds.his only included byhelp.c.Move the logic into functions so that we can skip parts of the check.
Quoted 10 lines
Review
Quoted 8 lines
Signed-off-by David Aguilar <[email protected]>---
This depends on my previous patch that addscheck-header.sh.
...(quoted diff) +4
-0+check_headers () { + for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h + do + check_header "$header"Hmmmm, doesn't
check_headerrun "$@" as a command?
Taking the previous two together, perhaps
check-headers.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
check-headers.sh +8 -5
@@ -20,6 +20,7 @@ maybe_exit () { check_header () { header="$1" + shift case "$header" in common-cmds.h) # should only be included by help.c, not checked
@@ -38,15 +39,17 @@ check_header () { check_headers () { for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h do - check_header "$header" + check_header "$header" "$@" done } check_header_usage () { - first=$(grep '^#include' "$1" | - head -n1 | - sed -e 's,#include ",,' -e 's,"$,,') - + first=$( + sed -n -e '/^#include/{ + s/#include ["<]\(.*\)".*/\1/p + q + }' "$1" + ) case "$first" in cache.h|builtin.h|git-compat-util.h) # happy
Quoted 22 lines · Junio C Hamano
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · David Aguilar
Teach
check-header.shto ensure that the first included header in .c files is eithergit-compat-util.h,builtin.h, orcache.h.Ensure that
common-cmds.his only included byhelp.c.Move the logic into functions so that we can skip parts of the check.
Quoted 10 lines
Review
Quoted 8 lines
Signed-off-by David Aguilar <[email protected]>---
This depends on my previous patch that addscheck-header.sh.
...(quoted diff) +4
-0+check_headers () { + for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h + do + check_header "$header"Hmmmm, doesn't
check_headerrun "$@" as a command?
Quoted 1 line
Taking the previous two together, perhaps
Thanks, yes, that's better.
Quoted 38 lines
check-headers.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)check-headers.sh +8
-5@@ -20,6 +20,7 @@ maybe_exit () { check_header () { header="$1" + shift case "$header" in common-cmds.h) # should only be included by help.c, not checked@@ -38,15 +39,17 @@ check_header () { check_headers () { for header in *.h ewah/*.h vcs-svn/*.h xdiff/*.h do - check_header "$header" + check_header "$header" "$@" done } check_header_usage () { - first=$(grep '^#include' "$1" | - head -n1 | - sed -e 's,#include ",,' -e 's,"$,,') - + first=$( + sed -n -e '/^#include/{ + s/#include ["<]\(.*\)".*/\1/p + q + }' "$1" + ) case "$first" in cache.h|builtin.h|git-compat-util.h) # happy
Signature · 1 line
David
check-headers.sh do · line 48
Quoted 4 lines · David Aguilar
check-headers.sh +4
-0+check_header_usage () { + first=$(grep '^#include' "$1" | + head -n1 | + sed -e 's,#include ",,' -e 's,"$,,')
perhaps a single "sed" invocation suffices?
sed -n -e '/^#include/{
s/#include ["<]\(.*\)".*/\1/p
q
}' "$1"