1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright 2018 Arnon Warshavsky <[email protected]>
3
4# This awk script receives a list of expressions to monitor
5# and a list of folders to search these expressions in
6# - No search is done inside comments
7# - Both additions and removals of the expressions are checked
8#   A positive balance of additions fails the check
9
10BEGIN {
11	split(FOLDERS,deny_folders," ");
12	split(EXPRESSIONS,deny_expr," ");
13	in_file=0;
14	in_comment=0;
15	count=0;
16	comment_start="/*"
17	comment_end="*/"
18}
19# search for add/remove instances in current file
20# state machine assumes the comments structure is enforced by
21# checkpatches.pl
22(in_file) {
23	if ($0 ~ "^@@") {
24		in_comment = 0
25	}
26	# comment start
27	if (index($0,comment_start) > 0) {
28		in_comment = 1
29	}
30	# non comment code
31	if (in_comment == 0) {
32		for (i in deny_expr) {
33			forbidden_added = "^\\+.*" deny_expr[i];
34			forbidden_removed="^-.*" deny_expr[i];
35			current = expressions[deny_expr[i]]
36			if ($0 ~ forbidden_added) {
37				count = count + 1;
38				expressions[deny_expr[i]] = current + 1
39			}
40			if ($0 ~ forbidden_removed) {
41				count = count - 1;
42				expressions[deny_expr[i]] = current - 1
43			}
44		}
45	}
46	# comment end
47	if (index($0,comment_end) > 0) {
48		in_comment = 0
49	}
50}
51# switch to next file , check if the balance of add/remove
52# of previous filehad new additions
53($0 ~ "^\\+\\+\\+ b/") {
54	in_file = 0;
55	if (count > 0) {
56		exit;
57	}
58	for (i in deny_folders) {
59		re = "^\\+\\+\\+ b/" deny_folders[i];
60		if ($0 ~ re) {
61			in_file = 1
62			last_file = $0
63		}
64	}
65}
66END {
67	if (count > 0) {
68		print "Warning in " substr(last_file,7) ":"
69		print MESSAGE
70		exit RET_ON_FAIL
71	}
72}
73