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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#! /usr/bin/lua
-- $NetBSD: check-expect.lua,v 1.6 2023/07/08 10:01:17 rillig Exp $
--[[
usage: lua ./check-expect.lua *.c
Check that the /* expect+-n: ... */ comments in the .c source files match the
actual messages found in the corresponding .exp files. The .exp files are
expected in the current working directory.
The .exp files are generated on the fly during the ATF tests, see
t_integration.sh. During development, they can be generated using
lint1/accept.sh.
]]
local function test(func)
func()
end
local function assert_equals(got, expected)
if got ~= expected then
assert(false, string.format("got %q, expected %q", got, expected))
end
end
local had_errors = false
---@param fmt string
function print_error(fmt, ...)
print(fmt:format(...))
had_errors = true
end
local function load_lines(fname)
local lines = {}
local f = io.open(fname, "r")
if f == nil then return nil end
for line in f:lines() do
table.insert(lines, line)
end
f:close()
return lines
end
local function save_lines(fname, lines)
local f = io.open(fname, "w")
for _, line in ipairs(lines) do
f:write(line .. "\n")
end
f:close()
end
-- Load the 'expect:' comments from a C source file.
--
-- example return values:
-- {
-- ["file.c(18)"] = {"invalid argument 'a'", "invalid argument 'b'"},
-- ["file.c(23)"] = {"not a constant expression [123]"},
-- },
-- { "file.c(18)", "file.c(23)" }
local function load_c(fname)
local basename = fname:match("([^/]+)$")
local lines = load_lines(fname)
if lines == nil then return nil, nil end
local pp_fname = fname
local pp_lineno = 0
local comment_locations = {}
local comments_by_location = {}
local function add_expectation(offset, message)
local location = ("%s(%d)"):format(pp_fname, pp_lineno + offset)
if comments_by_location[location] == nil then
table.insert(comment_locations, location)
comments_by_location[location] = {}
end
local trimmed_msg = message:match("^%s*(.-)%s*$")
table.insert(comments_by_location[location], trimmed_msg)
end
for phys_lineno, line in ipairs(lines) do
for offset, comment in line:gmatch("/%* expect([+%-]%d+): (.-) %*/") do
add_expectation(tonumber(offset), comment)
end
pp_lineno = pp_lineno + 1
local ppl_lineno, ppl_fname = line:match("^#%s*(%d+)%s+\"([^\"]+)\"")
if ppl_lineno ~= nil then
if ppl_fname == basename and tonumber(ppl_lineno) ~= phys_lineno + 1 then
print_error("error: %s:%d: preprocessor line number must be %d",
fname, phys_lineno, phys_lineno + 1)
end
if ppl_fname:match("%.c$") and ppl_fname ~= basename then
print_error("error: %s:%d: preprocessor filename must be '%s'",
fname, phys_lineno, basename)
end
pp_fname = ppl_fname
pp_lineno = ppl_lineno
end
end
return comment_locations, comments_by_location
end
-- Load the expected raw lint output from a .exp file.
--
-- example return value: {
-- {
-- exp_lineno = 18,
-- location = "file.c(18)",
-- message = "not a constant expression [123]",
-- }
-- }
local function load_exp(exp_fname)
local lines = load_lines(exp_fname)
if lines == nil then
print_error("check-expect.lua: error: file " .. exp_fname .. " not found")
return
end
local messages = {}
for exp_lineno, line in ipairs(lines) do
for location, message in line:gmatch("(%S+%(%d+%)): (.+)$") do
table.insert(messages, {
exp_lineno = exp_lineno,
location = location,
message = message
})
end
end
return messages
end
local function matches(comment, pattern)
if comment == "" then return false end
local any_prefix = pattern:sub(1, 3) == "..."
if any_prefix then pattern = pattern:sub(4) end
local any_suffix = pattern:sub(-3) == "..."
if any_suffix then pattern = pattern:sub(1, -4) end
if any_prefix and any_suffix then
return comment:find(pattern, 1, true) ~= nil
elseif any_prefix then
return pattern ~= "" and comment:sub(-#pattern) == pattern
elseif any_suffix then
return comment:sub(1, #pattern) == pattern
else
return comment == pattern
end
end
test(function()
assert_equals(matches("a", "a"), true)
assert_equals(matches("a", "b"), false)
assert_equals(matches("a", "aaa"), false)
assert_equals(matches("abc", "a..."), true)
assert_equals(matches("abc", "c..."), false)
assert_equals(matches("abc", "...c"), true)
assert_equals(matches("abc", "...a"), false)
assert_equals(matches("abc123xyz", "...a..."), true)
assert_equals(matches("abc123xyz", "...b..."), true)
assert_equals(matches("abc123xyz", "...c..."), true)
assert_equals(matches("abc123xyz", "...1..."), true)
assert_equals(matches("abc123xyz", "...2..."), true)
assert_equals(matches("abc123xyz", "...3..."), true)
assert_equals(matches("abc123xyz", "...x..."), true)
assert_equals(matches("abc123xyz", "...y..."), true)
assert_equals(matches("abc123xyz", "...z..."), true)
assert_equals(matches("pattern", "...pattern..."), true)
end)
-- Inserts the '/* expect */' lines to the .c file, so that the .c file matches
-- the .exp file. Multiple 'expect' comments for a single line of code are not
-- handled correctly, but it's still better than doing the same work manually.
local function insert_missing(missing)
for fname, items in pairs(missing) do
table.sort(items, function(a, b) return a.lineno > b.lineno end)
local lines = load_lines(fname)
for _, item in ipairs(items) do
local lineno, message = item.lineno, item.message
local indent = (lines[lineno] or ""):match("^([ \t]*)")
local line = ("%s/* expect+1: %s */"):format(indent, message)
table.insert(lines, lineno, line)
end
save_lines(fname, lines)
end
end
local function check_test(c_fname, update)
local exp_fname = c_fname:gsub("%.c$", ".exp"):gsub(".+/", "")
local c_comment_locations, c_comments_by_location = load_c(c_fname)
if c_comment_locations == nil then return end
local exp_messages = load_exp(exp_fname) or {}
local missing = {}
for _, exp_message in ipairs(exp_messages) do
local c_comments = c_comments_by_location[exp_message.location] or {}
local expected_message =
exp_message.message:gsub("/%*", "**"):gsub("%*/", "**")
local found = false
for i, c_comment in ipairs(c_comments) do
if c_comment ~= "" and matches(expected_message, c_comment) then
c_comments[i] = ""
found = true
break
end
end
if not found then
print_error("error: %s: missing /* expect+1: %s */",
exp_message.location, expected_message)
if update then
local fname = exp_message.location:match("^([^(]+)")
local lineno = tonumber(exp_message.location:match("%((%d+)%)$"))
if not missing[fname] then missing[fname] = {} end
table.insert(missing[fname], {
lineno = lineno,
message = expected_message,
})
end
end
end
for _, c_comment_location in ipairs(c_comment_locations) do
for _, c_comment in ipairs(c_comments_by_location[c_comment_location]) do
if c_comment ~= "" then
print_error(
"error: %s: declared message \"%s\" is not in the actual output",
c_comment_location, c_comment)
end
end
end
if missing then
insert_missing(missing)
end
end
local function main(args)
local update = args[1] == "-u"
if update then
table.remove(args, 1)
end
for _, name in ipairs(args) do
check_test(name, update)
end
end
main(arg)
os.exit(not had_errors)
|