xref: /dpdk/devtools/check-forbidden-tokens.awk (revision 129f38c5b7ab4428ed94d088e3d3ef6a81807b55)
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright 2018 Arnon Warshavsky <arnon@qwilt.com>
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	split(SKIP_FILES,skip_files," ");
14	in_file=0;
15	in_comment=0;
16	count=0;
17	warned=0;
18	comment_start="/*"
19	comment_end="*/"
20}
21# search for add/remove instances in current file
22# state machine assumes the comments structure is enforced by
23# checkpatches.pl
24(in_file) {
25	if ($0 ~ "^@@") {
26		in_comment = 0
27	}
28	# comment start
29	if (index($0,comment_start) > 0) {
30		in_comment = 1
31	}
32	# non comment code
33	if (in_comment == 0) {
34		for (i in deny_expr) {
35			forbidden_added = "^\\+.*" deny_expr[i];
36			forbidden_removed="^-.*" deny_expr[i];
37			if ($0 ~ forbidden_added) {
38				count = count + 1
39			}
40			if ($0 ~ forbidden_removed) {
41				count = count - 1
42			}
43		}
44	}
45	# comment end
46	if (index($0,comment_end) > 0) {
47		in_comment = 0
48	}
49}
50# switch to next file , check if the balance of add/remove
51# of previous file had new additions
52($0 ~ "^\\+\\+\\+ b/") {
53	in_file = 0;
54	if (count > 0) {
55		warned = warned + 1
56		print "Warning in " substr(last_file,7) ":"
57	}
58	count = 0
59	for (i in deny_folders) {
60		if (!($0 ~ "^\\+\\+\\+ b/" deny_folders[i])) {
61			continue
62		}
63		skip = 0
64		for (j in skip_files) {
65			if (!($0 ~ "^\\+\\+\\+ b/" skip_files[j])) {
66				continue
67			}
68			skip = 1
69			break
70		}
71		if (skip == 0) {
72			in_file = 1
73			last_file = $0
74		}
75		break
76	}
77}
78END {
79	if (count > 0) {
80		warned = warned + 1
81		print "Warning in " substr(last_file,7) ":"
82	}
83	if (warned > 0) {
84		print MESSAGE
85		exit RET_ON_FAIL
86	}
87}
88