comparison lint/check.sh @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 #!/bin/sh
2 # check.sh - do checks on a source file
3 # usage: check.sh [--set] topdir sourcefile
4
5 usage() {
6 echo "$0: Usage: $0 [--tool|main|example] topdir srcfile" 1>&2
7 }
8
9 SET=unknown
10 case "$1" in
11 --tool) SET=tool; shift;;
12 --main) SET=main; shift;;
13 --example) SET=example; shift;;
14 -*) usage; exit 1;;
15 esac
16 if [ $# != 2 ]; then
17 usage; exit 1
18 fi
19
20 cd "$1" || exit 1
21 FILE="$2"
22
23 ############################################################
24 #
25 # 1. Find unapproved preprocessor symbols used in conditionals.
26 #
27
28 egrep -n '^(#if|#elif)' "$FILE" | tr -d '\r' | expand |\
29 awk '{
30 lineno=$1;
31 sub(":.*", "", lineno);
32 sub("^[0-9]*:", "", $1);
33
34 directive=$1;
35
36 if (file ~ "\\.h$" || file ~ "\\.hpp$") {
37 sym=file;
38 sub("^.*/", "", sym);
39 sym=toupper(sym);
40 gsub("\\.", "_", sym);
41 gsub("-", "_", sym);
42 if (sym == $2 && directive == "#ifndef") {
43 next;
44 }
45 }
46
47 sub("//.*", "", $0);
48 sub("/\\*.*\\*/", "", $0);
49 sub(" *$", "", $0);
50
51 $1 = file ":" lineno ": @";
52
53 print;
54 }' "file=$FILE" | sed '
55 /@ 0$/d
56
57 /@ __BCPLUSPLUS__$/d
58 /@ __GNUC__$/d
59 /@ __IBMCPP__$/d
60 /@ _MSC_VER$/d
61 /@ __WATCOM_CPLUSPLUS__$/d
62
63 /@ O_BINARY$/d
64 ' | (
65 if [ $SET = main ]; then
66 sed '
67 /@ AG_ON_AMIGA$/d
68 /@ AG_ON_UNIX$/d
69 /@ AG_ON_WINDOWS$/d
70
71 /@ DUMMYGUI$/d
72 /@ VACLGUI$/d
73
74 /@ INCLUDE_LOGGING$/d
75 /@ AG_EXE$/d
76 '
77 elif [ $SET = example ]; then
78 sed '
79 /@ __MSDOS__$/d
80 /@ defined(__MSDOS__) || defined(__WINDOWS__)$/d
81 '
82 else
83 cat
84 fi
85 ) | sed '
86 s/@ /Symbol `/
87 s/$/'"'"' in conditional/
88 '
89
90
91 ############################################################
92 #
93 # 2. Check for certain issues with header files.
94 #
95
96 awk < "$FILE" '
97 {
98 ++lineno;
99 sub("\r$", "", $0);
100 gsub("\t", " ", $0);
101 gsub("/\\*.*\\*/", " ", $0);
102 sub(" *$", "", $0);
103 sub("^ *# *", "#", $0);
104 }
105
106 function complain(msg) {
107 printf "%s:%d: %s\n", file, lineno, msg;
108 bad = 1;
109 }
110
111 /^#include / && $2 == "<sys/types.h>" { systypes=1; }
112 /^#include / && $2 ~ "<sys/.*\\.h>" && !systypes {
113 complain(sprintf("Used %s without <sys/types.h>", $2));
114 }
115
116 set=="main" && /^#include / && $2 == "<assert.h>" {
117 complain("Used system <assert.h>");
118 }
119
120 # The tree is not ready for these measures
121
122 #length($0) > 79 {
123 # complain("Line too long");
124 #}
125
126 #(/break;/ || /continue;/ || /return[^;]*;/) &&
127 #!(/^ *break; *$/ || /^ *continue; *$/ || /^ *return[^;]*; *$/) {
128 # complain("break/continue/return not on own line");
129 #}
130
131 #{
132 # t = $0;
133 # gsub(", ", " ", t);
134 # sub(",$", "", t);
135 # if (t ~ ",") {
136 # complain("comma without following space");
137 # }
138 #}
139
140 END {
141 exit(bad);
142 }
143 ' "file=$FILE" "set=$SET"
144
145 #if [ "$?" != 0 ]; then
146 # BAD=1
147 #fi
148
149 ############################################################
150
151 # done
152 exit 0