aboutsummaryrefslogtreecommitdiff
path: root/tests/c-lint.sh
blob: 0fa0b01f9c2f2f28339e312eb5d2815b055e66e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
set -eu

awk '
BEGIN {
	ret = 0
	msg = "function not on the start of the line:"
}

/^[a-zA-Z0-9_]+ .+\(/ {
	if (ret == 0) {
		print msg
	}
	printf "%s:%s:%s\n", FILENAME, FNR, $0
	ret = 1
}

END {
	exit ret
}
' "$@"


awk '
BEGIN {
	ret = 0
	static = 1
	msg = "non-static function is not declared in a header:"
}

/^[a-zA-Z0-9_]+\(.*$/ && static == 0 {
	split($0, line, /\(/)
	fn_name = line[1]
	if (fn_name != "main") {
		header = substr(FILENAME, 0, length(FILENAME) - 2)  ".h"
		if (system("grep -q ^\"" fn_name "\" \"" header "\"")) {
			if (ret == 0) {
				print msg
			}
			printf "%s:%s:%s\n", FILENAME, FNR, $0
			ret = 1
		}
	}
}

/^static / {
	static = 1
}

!/^static / {
	static = 0
}

END {
	exit ret
}
' "$@"


RE='[a-z]+\(\) {'
if grep -Eq "$RE" "$@"; then
	echo 'Functions with no argument without explicit "void" parameter:' >&2
	grep -En "$RE" "$@"
	exit 1
fi