2012-07-31 06:00:48 -07:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
#########################################################################
|
|
|
|
# #
|
|
|
|
# OCaml #
|
|
|
|
# #
|
|
|
|
# Damien Doligez, projet Gallium, INRIA Rocquencourt #
|
|
|
|
# #
|
|
|
|
# Copyright 2012 Institut National de Recherche en Informatique et #
|
|
|
|
# en Automatique. All rights reserved. This file is distributed #
|
|
|
|
# under the terms of the Q Public License version 1.0. #
|
|
|
|
# #
|
|
|
|
#########################################################################
|
|
|
|
|
|
|
|
# check-typo - Check typographic conventions on OCaml sources.
|
|
|
|
|
|
|
|
# This program will check files for the following rules:
|
|
|
|
|
|
|
|
# - absence of TAB characters (tab)
|
|
|
|
# - absence of non-ASCII characters (non-ascii)
|
|
|
|
# - absence of non-printing ASCII characters (non-printing)
|
|
|
|
# - absence of white space at end of line (white-at-eol)
|
2012-10-15 06:49:55 -07:00
|
|
|
# - absence of empty lines at end of file (white-at-eof)
|
2012-07-31 06:00:48 -07:00
|
|
|
# - presence of a LF character at the end of the file (missing-lf)
|
|
|
|
# - maximum line length of 80 characters (long-line)
|
2014-07-21 13:41:52 -07:00
|
|
|
# - maximum line length of 132 characters (very-long-line)
|
2012-08-01 05:39:56 -07:00
|
|
|
# - presence of a copyright header (missing-header)
|
2012-10-15 10:50:56 -07:00
|
|
|
# - absence of a leftover "$Id" string (svn-keyword)
|
2012-07-31 06:00:48 -07:00
|
|
|
|
|
|
|
# Exceptions are handled with a SVN property: "ocaml:typo".
|
|
|
|
# Its value for a given file is a comma-separated list of rule names,
|
|
|
|
# which lists the rules that should be disabled for this file.
|
|
|
|
# The rule names are the ones shown above in parentheses.
|
|
|
|
|
|
|
|
# Built-in exceptions:
|
|
|
|
# - Any binary file (i.e. with svn:mime-type = application/octet-stream)
|
|
|
|
# is automatically exempt from all the rules.
|
2014-04-12 00:22:18 -07:00
|
|
|
# - Any file whose name matches one of the following patterns is
|
|
|
|
# automatically exempt from all rules
|
|
|
|
# *.reference
|
|
|
|
# */reference
|
2014-08-22 06:45:02 -07:00
|
|
|
# */.depend*
|
2012-07-31 06:00:48 -07:00
|
|
|
# - Any file whose name begins with "Makefile" is automatically exempt
|
|
|
|
# from the "tabs" rule.
|
2012-08-01 05:39:56 -07:00
|
|
|
# - Any file whose name matches one of the following patterns is
|
|
|
|
# automatically exempt from the "missing-header" rule.
|
|
|
|
# */.ignore
|
|
|
|
# *.mlpack
|
|
|
|
# *.mllib
|
2012-08-02 00:39:50 -07:00
|
|
|
# *.mltop
|
2012-08-01 05:39:56 -07:00
|
|
|
# *.odocl
|
|
|
|
# *.clib
|
2014-07-21 13:41:52 -07:00
|
|
|
# - Any file whose name matches the following pattern is automatically
|
|
|
|
# exempt from the "long-line" rule (but not from "very-long-line").
|
|
|
|
# */ocamldoc/*
|
2012-07-31 06:00:48 -07:00
|
|
|
|
|
|
|
# ASCII characters are bytes from 0 to 127. Any other byte is
|
|
|
|
# flagged as a non-ASCII character.
|
|
|
|
|
|
|
|
# For the purpose of this tool, printing ASCII characters are:
|
|
|
|
# - the non-white printable ASCII characters (33 to 126)
|
|
|
|
# - TAB (09)
|
|
|
|
# - LF (10)
|
|
|
|
# - SPC (32)
|
|
|
|
# Anything else is flagged as a non-printing ASCII character.
|
|
|
|
|
|
|
|
# This program will recursively explore the files and directories given
|
|
|
|
# on the command line (or by default the current directory), and check
|
|
|
|
# every file therein for compliance to the rules.
|
|
|
|
|
|
|
|
# Directories named .svn and _build (and their contents) are always ignored.
|
|
|
|
# This program ignores any file that is not under svn control, unless
|
|
|
|
# explicitly given on the command line.
|
|
|
|
|
2012-10-15 10:17:44 -07:00
|
|
|
# If a directory has the SVN property "ocaml:typo" set to "prune",
|
|
|
|
# then it and its contents are ignored.
|
|
|
|
|
2012-07-31 06:00:48 -07:00
|
|
|
# You can ignore a rule by giving the option -<rule> on the command
|
|
|
|
# line (before any file names).
|
|
|
|
|
2012-10-15 10:17:44 -07:00
|
|
|
# Special case for recursive call from the find command (see IGNORE_DIRS).
|
|
|
|
case "$1" in
|
|
|
|
--check-prune)
|
|
|
|
case `svn propget ocaml:typo "$2" 2>/dev/null` in
|
|
|
|
prune) echo "INFO: pruned directory $2 (ocaml:typo=prune)" >&2; exit 0;;
|
|
|
|
*) exit 3;;
|
|
|
|
esac;;
|
|
|
|
esac
|
|
|
|
|
2012-07-31 06:00:48 -07:00
|
|
|
usage () {
|
2013-03-09 14:38:52 -08:00
|
|
|
echo "usage: check-typo {-<rule>} [--] {<file-or-dir>}" >&2
|
2012-07-31 06:00:48 -07:00
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
userrules=''
|
|
|
|
|
|
|
|
while : ; do
|
|
|
|
case "$1" in
|
2012-10-15 06:49:55 -07:00
|
|
|
-help|--help) usage;;
|
2012-07-31 06:00:48 -07:00
|
|
|
-*) userrules="${1#-},$userrules"; shift;;
|
|
|
|
--) shift; break;;
|
|
|
|
*) break;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2012-10-15 10:17:44 -07:00
|
|
|
IGNORE_DIRS="
|
2012-07-31 06:00:48 -07:00
|
|
|
-name .svn -prune -o
|
|
|
|
-name _build -prune -o
|
2012-10-15 10:17:44 -07:00
|
|
|
-type d -exec $0 --check-prune {} ; -prune -o
|
|
|
|
"
|
2012-07-31 06:00:48 -07:00
|
|
|
|
|
|
|
( case $# in
|
|
|
|
0) find . $IGNORE_DIRS -type f -print;;
|
|
|
|
*) for i in "$@"; do find "$i" $IGNORE_DIRS -type f -print; done;;
|
|
|
|
esac
|
|
|
|
) | (
|
|
|
|
while read f; do
|
|
|
|
case `svn status "$f" 2>&1` in
|
|
|
|
'?'*) is_svn=false;;
|
|
|
|
I*) is_svn=false;;
|
|
|
|
svn:*"is not a working copy") is_svn=false;;
|
|
|
|
*) is_svn=true;;
|
|
|
|
esac
|
|
|
|
case "$*" in
|
|
|
|
*$f*) is_cmd_line=true;;
|
|
|
|
*) is_cmd_line=false;;
|
|
|
|
esac
|
|
|
|
if $is_svn || $is_cmd_line; then :; else continue; fi
|
|
|
|
svnrules=''
|
|
|
|
if $is_svn; then
|
|
|
|
case `svn propget svn:mime-type "$f"` in
|
|
|
|
application/octet-stream) continue;;
|
|
|
|
esac
|
|
|
|
svnrules=`svn propget ocaml:typo "$f"`
|
|
|
|
fi
|
|
|
|
rules="$userrules"
|
2014-07-21 13:41:52 -07:00
|
|
|
add_hd(){ rules="missing-header,$rules"; }
|
2012-07-31 06:00:48 -07:00
|
|
|
case "$f" in
|
2012-08-01 05:39:56 -07:00
|
|
|
Makefile*|*/Makefile*) rules="tab,$rules";;
|
2014-08-22 06:45:02 -07:00
|
|
|
*/.ignore) add_hd;;
|
2014-07-21 13:41:52 -07:00
|
|
|
*.mlpack|*.mllib|*.mltop|*.odocl|*.itarget|*.clib) add_hd;;
|
2014-08-22 06:45:02 -07:00
|
|
|
*.reference|*/reference|*/.depend*) continue;;
|
2012-08-01 05:39:56 -07:00
|
|
|
esac
|
|
|
|
case "$f" in
|
2014-07-21 13:41:52 -07:00
|
|
|
ocamldoc/*|*/ocamldoc/*) rules="long-line,$rules";;
|
2013-09-04 08:12:37 -07:00
|
|
|
esac
|
2012-07-31 06:00:48 -07:00
|
|
|
|
|
|
|
(cat "$f"; echo) \
|
|
|
|
| awk -v rules="$rules" -v svnrules="$svnrules" -v file="$f" \
|
|
|
|
'
|
|
|
|
function err(name, msg) {
|
|
|
|
++ counts[name];
|
2012-08-02 00:39:50 -07:00
|
|
|
if (("," rules svnrules ",") !~ ("[, ]" name "[, ]") \
|
2012-07-31 06:00:48 -07:00
|
|
|
&& counts[name] <= 10){
|
|
|
|
printf ("%s:%d.%d:", file, NR, RSTART + RLENGTH);
|
|
|
|
printf (" [%s] %s\n", name, msg);
|
|
|
|
if (counts[name] == 10){
|
|
|
|
printf ("WARNING: too many [%s] in this file.", name);
|
|
|
|
printf (" Others will not be reported.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match($0, /\t/) {
|
|
|
|
err("tab", "TAB character(s)");
|
|
|
|
}
|
|
|
|
|
|
|
|
match($0, /[\200-\377]/) {
|
|
|
|
err("non-ascii", "non-ASCII character(s)");
|
|
|
|
}
|
|
|
|
|
|
|
|
match($0, /[^\t\200-\377 -~]/) {
|
|
|
|
err("non-printing", "non-printing ASCII character(s)");
|
|
|
|
}
|
|
|
|
|
|
|
|
match($0, /[ \t]+$/) {
|
|
|
|
err("white-at-eol", "whitespace at end of line");
|
|
|
|
}
|
|
|
|
|
2012-10-15 10:50:56 -07:00
|
|
|
match($0, /\$Id(: .*)?\$/) {
|
2012-08-02 00:39:50 -07:00
|
|
|
err("svn-keyword", "SVN keyword marker");
|
|
|
|
}
|
|
|
|
|
2012-07-31 06:00:48 -07:00
|
|
|
length($0) > 80 {
|
|
|
|
RSTART = 81;
|
2012-08-01 05:39:56 -07:00
|
|
|
RLENGTH = 0;
|
2012-07-31 06:00:48 -07:00
|
|
|
err("long-line", "line is over 80 characters");
|
|
|
|
}
|
|
|
|
|
2014-07-21 13:41:52 -07:00
|
|
|
length($0) > 132 {
|
|
|
|
RSTART = 133;
|
|
|
|
RLENGTH = 0;
|
|
|
|
err("very-long-line", "line is over 132 characters");
|
|
|
|
}
|
|
|
|
|
2012-08-02 00:39:50 -07:00
|
|
|
3 <= NR && NR <= 5 \
|
2012-08-01 05:39:56 -07:00
|
|
|
&& (/ OCaml / || / ocamlbuild / || / OCamldoc /) {
|
2012-08-02 00:39:50 -07:00
|
|
|
header_ocaml = NR;
|
2012-08-01 05:39:56 -07:00
|
|
|
}
|
|
|
|
|
2012-08-02 00:39:50 -07:00
|
|
|
header_ocaml && header_ocaml + 4 <= NR && NR <= header_ocaml + 6 \
|
|
|
|
&& / Copyright / {
|
2012-08-01 05:39:56 -07:00
|
|
|
header_copyright = 1;
|
|
|
|
}
|
|
|
|
|
2012-07-31 06:00:48 -07:00
|
|
|
{
|
|
|
|
prev_line = last_line;
|
|
|
|
last_line = $0;
|
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
|
|
|
if (match(last_line, /.+/)){
|
|
|
|
err("missing-lf", "missing linefeed at EOF");
|
|
|
|
prev_line = last_line;
|
|
|
|
++ NR;
|
|
|
|
empty_file = 0;
|
|
|
|
}else{
|
|
|
|
empty_file = NR == 1;
|
|
|
|
}
|
2012-10-15 06:49:55 -07:00
|
|
|
if (!empty_file && match(prev_line, /^$/)){
|
|
|
|
err("white-at-eof", "empty line(s) at EOF");
|
2012-07-31 06:00:48 -07:00
|
|
|
}
|
2012-08-01 05:39:56 -07:00
|
|
|
NR = 1;
|
|
|
|
RSTART = 1;
|
|
|
|
RLENGTH = 0;
|
|
|
|
if (!(header_ocaml && header_copyright)){
|
|
|
|
err("missing-header", "missing copyright header");
|
|
|
|
}
|
2012-08-02 00:39:50 -07:00
|
|
|
split(svnrules, r, "[, ]");
|
2012-07-31 06:00:48 -07:00
|
|
|
for (i in r){
|
|
|
|
name = r[i];
|
|
|
|
if (name != "" && !counts[name]){
|
2012-08-01 05:39:56 -07:00
|
|
|
err("unused-prop", sprintf("unused [%s] in ocaml:typo", name));
|
2012-07-31 06:00:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
done
|
|
|
|
)
|