check-expect.lua revision 1.5 1 #! /usr/bin/lua
2 -- $NetBSD: check-expect.lua,v 1.5 2023/06/01 07:27:30 rillig Exp $
3
4 --[[
5
6 usage: lua ./check-expect.lua *.mk
7
8 Check that the various 'expect' comments in the .mk files produce the
9 expected text in the corresponding .exp file.
10
11 # expect: <line>
12 All of these lines must occur in the .exp file, in the same order as
13 in the .mk file.
14
15 # expect-reset
16 Search the following 'expect:' comments from the top of the .exp
17 file again.
18
19 # expect[+-]offset: <message>
20 Each message must occur in the .exp file and refer back to the
21 source line in the .mk file.
22
23 # expect-all
24 Each message from the .exp file that can be matched by an
25 'expect[+-]offset' comment must actually be matched.
26 ]]
27
28
29 local had_errors = false
30 ---@param fmt string
31 function print_error(fmt, ...)
32 print(fmt:format(...))
33 had_errors = true
34 end
35
36
37 ---@return nil | string[]
38 local function load_lines(fname)
39 local lines = {}
40
41 local f = io.open(fname, "r")
42 if f == nil then return nil end
43
44 for line in f:lines() do
45 table.insert(lines, line)
46 end
47 f:close()
48
49 return lines
50 end
51
52
53 ---@param exp_lines string[]
54 local function collect_lineno_diagnostics(exp_lines)
55 ---@type table<string, string[]>
56 local by_location = {}
57
58 for _, line in ipairs(exp_lines) do
59 ---@type string | nil, string, string
60 local l_fname, l_lineno, l_msg =
61 line:match('^make: "([^"]+)" line (%d+): (.*)')
62 if l_fname ~= nil then
63 local location = ("%s:%d"):format(l_fname, l_lineno)
64 if by_location[location] == nil then
65 by_location[location] = {}
66 end
67 table.insert(by_location[location], l_msg)
68 end
69 end
70
71 return by_location
72 end
73
74
75 local function check_mk(mk_fname)
76 local exp_fname = mk_fname:gsub("%.mk$", ".exp")
77 local mk_lines = load_lines(mk_fname)
78 local exp_lines = load_lines(exp_fname)
79 if exp_lines == nil then return end
80 local by_location = collect_lineno_diagnostics(exp_lines)
81 local prev_expect_line = 0
82 local match_all = false
83
84 for mk_lineno, mk_line in ipairs(mk_lines) do
85 for text in mk_line:gmatch("#%s*expect:%s*(.*)") do
86 local i = prev_expect_line
87 -- As of 2022-04-15, some lines in the .exp files contain trailing
88 -- whitespace. If possible, this should be avoided by rewriting the
89 -- debug logging. When done, the gsub can be removed.
90 -- See deptgt-phony.exp lines 14 and 15.
91 while i < #exp_lines and text ~= exp_lines[i + 1]:gsub("%s*$", "") do
92 i = i + 1
93 end
94 if i < #exp_lines then
95 prev_expect_line = i + 1
96 else
97 print_error("error: %s:%d: '%s:%d+' must contain '%s'",
98 mk_fname, mk_lineno, exp_fname, prev_expect_line + 1, text)
99 end
100 end
101 if mk_line:match("^#%s*expect%-reset$") then
102 prev_expect_line = 0
103 end
104
105 ---@param text string
106 for offset, text in mk_line:gmatch("#%s*expect([+%-]%d+):%s*(.*)") do
107 local location = ("%s:%d"):format(mk_fname, mk_lineno + tonumber(offset))
108
109 local found = false
110 if by_location[location] ~= nil then
111 for i, message in ipairs(by_location[location]) do
112 if message ~= "" and message:find(text, 1, true) then
113 by_location[location][i] = ""
114 found = true
115 break
116 end
117 end
118 end
119
120 if not found then
121 print_error("error: %s:%d: %s must contain '%s'",
122 mk_fname, mk_lineno, exp_fname, text)
123 end
124 end
125
126 if mk_line:match("^#%s*expect%-all$") then
127 match_all = true
128 end
129 end
130
131 if match_all then
132 -- XXX: The messages are not sorted in any meaningful way.
133 for location, messages in pairs(by_location) do
134 for _, message in ipairs(messages) do
135 if message ~= "" then
136 print_error("error: %s: missing 'expect' comment for '%s'",
137 location, message)
138 end
139 end
140 end
141 end
142 end
143
144 for _, fname in ipairs(arg) do
145 check_mk(fname)
146 end
147 os.exit(not had_errors)
148