Module Name:    src
Committed By:   rillig
Date:           Sat Feb 27 17:16:48 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint: check-expect.lua
        src/tests/usr.bin/xlint/lint1: d_c99_bool_strict.c

Log Message:
tests/lint: allow 'expect' comments to refer to other lines

This allows /* expect+1: ... */ to refer to the following line, as well
as /* expect-1: ... */ to refer to the previous line.  This avoids
horizontal scrolling to see the expectations, it also allows these
expectations comments to be more verbose, mentioning the whole
diagnostic in many cases.

The 'expect' comments don't need to be at the end of a line anymore
since that was rather surprising.  The one 'expect' comment that had not
been at the end of the line was in d_c99_bool_strict.c and was not
intended to be ignored.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/check-expect.lua
cvs rdiff -u -r1.21 -r1.22 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/check-expect.lua
diff -u src/tests/usr.bin/xlint/check-expect.lua:1.4 src/tests/usr.bin/xlint/check-expect.lua:1.5
--- src/tests/usr.bin/xlint/check-expect.lua:1.4	Sun Feb 21 09:07:58 2021
+++ src/tests/usr.bin/xlint/check-expect.lua	Sat Feb 27 17:16:48 2021
@@ -1,5 +1,5 @@
 #!  /usr/bin/lua
--- $NetBSD: check-expect.lua,v 1.4 2021/02/21 09:07:58 rillig Exp $
+-- $NetBSD: check-expect.lua,v 1.5 2021/02/27 17:16:48 rillig Exp $
 
 --[[
 
@@ -28,19 +28,32 @@ end
 local function load_expect_comments_from_c(fname, errors)
 
   local lines = load_lines(fname)
-  if lines == nil then return nil end
+  if lines == nil then return nil, nil end
+
+  local comment_linenos = {}
+  local comments_by_lineno = {}
+  local function add_expectation(lineno, msg)
+    if comments_by_lineno[lineno] == nil then
+      table.insert(comment_linenos, lineno)
+      comments_by_lineno[lineno] = {}
+    end
+    local trimmed_msg = msg:match("^%s*(.-)%s*$")
+    table.insert(comments_by_lineno[lineno], trimmed_msg)
+  end
 
-  local comments_by_line = {}
-  local seen_comment = false
   for lineno, line in ipairs(lines) do
-    local comments_in_line = {}
-    for comments in line:gmatch("/%* expect: (.-) %*/$") do
+
+    for offset, comments in line:gmatch("/%* expect([+%-]%d+): (.-) %*/") do
       for comment in comments:gmatch("[^,]+") do
-	table.insert(comments_in_line, comment:match("^%s*(.-)%s*$"))
-        seen_comment = true
+        add_expectation(lineno + tonumber(offset), comment)
+      end
+    end
+
+    for comments in line:gmatch("/%* expect: (.-) %*/") do
+      for comment in comments:gmatch("[^,]+") do
+	add_expectation(lineno, comment)
       end
     end
-    comments_by_line[lineno] = comments_in_line
 
     local pp_lineno, pp_fname = line:match("^#%s*(%d+)%s+\"([^\"]+)\"")
     if pp_lineno ~= nil then
@@ -51,7 +64,7 @@ local function load_expect_comments_from
     end
   end
 
-  return comments_by_line
+  return comment_linenos, comments_by_lineno
 end
 
 
@@ -77,31 +90,20 @@ end
 
 local function check_test(c_fname, errors)
   local exp_fname = c_fname:gsub("%.c$", ".exp")
-  local comments = load_expect_comments_from_c(c_fname, errors)
-  if comments == nil or #comments == 0 then return end
+  local comment_linenos, comments_by_lineno =
+    load_expect_comments_from_c(c_fname, errors)
+  if comment_linenos == nil or #comment_linenos == 0 then return end
   local messages = load_actual_messages_from_exp(exp_fname)
   if messages == nil then return end
 
-  local remaining = 0
-  for lineno, exps in ipairs(comments) do
-    for _, msg in ipairs(exps) do
-      -- print("comment", lineno, msg)
-      remaining = remaining + 1
-    end
-  end
-
   for _, act in ipairs(messages) do
-    -- print("messages", act.exp_lineno, act.c_lineno, act.msg)
-
-    local exp = comments[act.c_lineno] or {}
+    local exp = comments_by_lineno[act.c_lineno] or {}
 
     local found = false
     for i, msg in ipairs(exp) do
       if msg ~= "" and act.msg:find(msg, 1, true) then
         exp[i] = ""
         found = true
-        remaining = remaining - 1
-        -- print("found", act.c_lineno, act.msg, msg, remaining)
         break
       end
     end
@@ -112,10 +114,11 @@ local function check_test(c_fname, error
     end
   end
 
-  for lineno, exps in ipairs(comments) do
-    for _, msg in ipairs(exps) do
+  for _, lineno in ipairs(comment_linenos) do
+    for _, msg in ipairs(comments_by_lineno[lineno]) do
       if msg ~= "" then
-        errors:add("error: %s:%d: declared message \"%s\" is not in the actual output",
+        errors:add(
+          "error: %s:%d: declared message \"%s\" is not in the actual output",
           c_fname, lineno, msg)
       end
     end

Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c
diff -u src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c:1.21 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c:1.22
--- src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c:1.21	Sat Feb 20 18:55:10 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c	Sat Feb 27 17:16:48 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: d_c99_bool_strict.c,v 1.21 2021/02/20 18:55:10 rillig Exp $	*/
+/*	$NetBSD: d_c99_bool_strict.c,v 1.22 2021/02/27 17:16:48 rillig Exp $	*/
 # 3 "d_c99_bool_strict.c"
 
 /*
@@ -306,7 +306,7 @@ strict_bool_conversion_function_argument
 	take_arguments(p, p, p);	/* expect: 334, 154 */
 
 	/* Passing bool as vararg. */
-	take_arguments(b, i, p, b, i, p); /* expect: arg#4 */ // TODO
+	take_arguments(b, i, p, b, i, p); /* TODO: expect: arg#4 */
 
 	/* Passing a bool constant. */
 	take_arguments(__lint_false, i, p);

Reply via email to