Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/xlint lint: add additional queries that are no...



details:   https://anonhg.NetBSD.org/src/rev/9377af232ae5
branches:  trunk
changeset: 368335:9377af232ae5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Jul 05 22:50:41 2022 +0000

description:
lint: add additional queries that are not enabled by default

In the last 18 months, several lint warnings have been made adjusted to
allow common usage patterns.  For example, lint no longer warns about a
constant condition in the statement 'do { ... } while (false)' (message
161), as this pattern is well-known in statement-like macros, making it
unlikely that the 'false' is a mistake.  Another example is casts
between unequal pointer types (message 247) for a few well-known
patterns that are unlikely to be bugs.

Occasionally, it is useful to query the code for patterns or events that
would not justify a warning.  These patterns are modeled as predefined
queries that can be selected individually, in addition to and
independently of the existing warnings and errors.

New queries can be added as needed, in the same way as new warnings.
Queries that are deemed no longer used can be deactivated in the same
way as warnings that are no longer used.

As long as none of the queries is enabled, they produce a minimal
overhead of querying a single global variable.  Computations that are
more expensive than a few machine instructions should be guarded by
any_query_enabled.

https://mail-index.netbsd.org/source-changes-d/2022/06/28/msg013716.html

ok christos@

diffstat:

 distrib/sets/lists/tests/mi             |    3 +-
 tests/usr.bin/xlint/lint1/Makefile      |    3 +-
 tests/usr.bin/xlint/lint1/queries.c     |  108 ++++++++++++++++++++++++++++++++
 usr.bin/xlint/lint1/Makefile            |    8 +-
 usr.bin/xlint/lint1/Makefile.err-msgs-h |    4 +-
 usr.bin/xlint/lint1/README.md           |    8 +-
 usr.bin/xlint/lint1/check-msgs.lua      |   25 ++++---
 usr.bin/xlint/lint1/err.c               |   58 ++++++++++++++++-
 usr.bin/xlint/lint1/externs1.h          |    6 +-
 usr.bin/xlint/lint1/lint1.h             |   17 ++++-
 usr.bin/xlint/lint1/main1.c             |    9 +-
 usr.bin/xlint/lint1/makeman             |   58 +++++++++++++---
 usr.bin/xlint/lint1/tree.c              |   41 +++++++++++-
 usr.bin/xlint/xlint/lint.1              |   20 +++++-
 usr.bin/xlint/xlint/xlint.c             |    7 +-
 15 files changed, 325 insertions(+), 50 deletions(-)

diffs (truncated from 733 to 300 lines):

diff -r 7d8252825929 -r 9377af232ae5 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Tue Jul 05 22:20:55 2022 +0000
+++ b/distrib/sets/lists/tests/mi       Tue Jul 05 22:50:41 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1215 2022/06/17 20:23:58 rillig Exp $
+# $NetBSD: mi,v 1.1216 2022/07/05 22:50:41 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -7316,6 +7316,7 @@
 ./usr/tests/usr.bin/xlint/lint1/platform_schar.exp             tests-obsolete          obsolete,atf
 ./usr/tests/usr.bin/xlint/lint1/platform_uchar.c               tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/platform_uchar.exp             tests-obsolete          obsolete,atf
+./usr/tests/usr.bin/xlint/lint1/queries.c                      tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/stmt_for.c                     tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/stmt_for.exp                   tests-obsolete          obsolete,atf
 ./usr/tests/usr.bin/xlint/lint1/stmt_goto.c                    tests-usr.bin-tests     compattestfile,atf
diff -r 7d8252825929 -r 9377af232ae5 tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Tue Jul 05 22:20:55 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Tue Jul 05 22:50:41 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.128 2022/06/17 20:31:56 rillig Exp $
+# $NetBSD: Makefile,v 1.129 2022/07/05 22:50:41 rillig Exp $
 
 NOMAN=         # defined
 MAX_MESSAGE=   349             # see lint1/err.c
@@ -167,6 +167,7 @@
 FILES+=                platform_lp64.c
 FILES+=                platform_schar.c
 FILES+=                platform_uchar.c
+FILES+=                queries.c
 FILES+=                stmt_for.c
 FILES+=                stmt_goto.c
 FILES+=                stmt_if.c
diff -r 7d8252825929 -r 9377af232ae5 tests/usr.bin/xlint/lint1/queries.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/queries.c       Tue Jul 05 22:50:41 2022 +0000
@@ -0,0 +1,108 @@
+/*     $NetBSD: queries.c,v 1.1 2022/07/05 22:50:41 rillig Exp $       */
+# 3 "queries.c"
+
+/*
+ * Demonstrate the case-by-case queries.  Unlike warnings, queries do not
+ * point to questionable code but rather to code that may be interesting to
+ * inspect manually on a case-by-case basis.
+ *
+ * Possible use cases are:
+ *
+ *     Understanding how C works internally, by making the usual arithmetic
+ *     conversions visible.
+ *
+ *     Finding code that intentionally suppresses a regular lint warning,
+ *     such as casts between arithmetic types.
+ */
+
+/* lint1-extra-flags: -q 1,2,3,4,5,6,7 */
+
+int
+Q1(double dbl)
+{
+       /* expect+1: implicit conversion from floating point 'double' to integer 'int' [Q1] */
+       return dbl;
+}
+
+int
+Q2(double dbl)
+{
+       /* expect+2: cast from floating point 'double' to integer 'int' [Q2] */
+       /* expect+1: redundant cast from 'double' to 'int' before assignment [Q7] */
+       return (int)dbl;
+}
+
+void
+Q3(int i, unsigned u)
+{
+       /* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
+       u = i;
+
+       /* expect+1: implicit conversion changes sign from 'unsigned int' to 'int' [Q3] */
+       i = u;
+}
+
+unsigned long long
+Q4(char *ptr, int i, unsigned long long ull)
+{
+       /*
+        * The conversion from 'char' to 'int' is done by the integer
+        * promotions (C11 6.3.1.1p2), not by the usual arithmetic
+        * conversions (C11 6.3.1.8p1).
+        */
+       /* expect+2: usual arithmetic conversion for '+' from 'int' to 'unsigned long long' [Q4] */
+       /* expect+1: implicit conversion changes sign from 'int' to 'unsigned long long' [Q3] */
+       return ptr[0] + ptr[1] + i + ull;
+}
+
+void
+Q5(char *ptr, int i)
+{
+       if (ptr + i > ptr)
+               return;
+
+       /* expect+1: pointer addition has integer on the left-hand side [Q5] */
+       if (i + ptr > ptr)
+               return;
+
+       if (ptr[i] != '\0')
+               return;
+
+       /* expect+1: pointer addition has integer on the left-hand side [Q5] */
+       if (i[ptr] != '\0')
+               return;
+}
+
+void
+Q6(int i)
+{
+       /* expect+1: no-op cast from 'int' to 'int' [Q6] */
+       i = (int)4;
+
+       /* expect+1: no-op cast from 'int' to 'int' [Q6] */
+       i = (int)i + 1;
+}
+
+extern void *allocate(unsigned long);
+
+char *
+Q7(void)
+{
+       /* expect+1: redundant cast from 'pointer to void' to 'pointer to char' before assignment [Q7] */
+       char *str = (char *)allocate(64);
+
+       if (str == (void *)0)
+               /* expect+1: redundant cast from 'pointer to void' to 'pointer to char' before assignment [Q7] */
+               str = (char *)allocate(64);
+
+       return str;
+}
+
+
+/*
+ * Since queries do not affect the exit status, force a warning to make this
+ * test conform to the general expectation that a test that produces output
+ * exits non-successfully.
+ */
+/* expect+1: warning: static variable 'unused' unused [226] */
+static int unused;
diff -r 7d8252825929 -r 9377af232ae5 usr.bin/xlint/lint1/Makefile
--- a/usr.bin/xlint/lint1/Makefile      Tue Jul 05 22:20:55 2022 +0000
+++ b/usr.bin/xlint/lint1/Makefile      Tue Jul 05 22:50:41 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.91 2022/06/17 20:23:58 rillig Exp $
+#      $NetBSD: Makefile,v 1.92 2022/07/05 22:50:41 rillig Exp $
 
 .include <bsd.own.mk>
 
@@ -62,9 +62,11 @@
            ${CPPFLAGS:C/-([IDUW]) */-\1/Wg:M-[IDUW]*} \
            -i -UYYDEBUG ${.IMPSRC}
 
-${MAN}:                makeman ${LINT1:./%=%} Makefile ${MAN}.date
+${MAN}:                makeman err.c Makefile ${MAN}.date
        ${_MKTARGET_CREATE}
-       ${HOST_SH} ${.ALLSRC:M*makeman} "$$(cat ${.ALLSRC:M*.date})" ${LINT1} -m >${.TARGET}
+       ${HOST_SH} ${.ALLSRC:M*makeman} \
+           "$$(cat ${.ALLSRC:M*.date})" ${.ALLSRC:M*err.c} \
+           >${.TARGET}
 
 LDADD+=                -lm
 .ifndef HOSTPROG
diff -r 7d8252825929 -r 9377af232ae5 usr.bin/xlint/lint1/Makefile.err-msgs-h
--- a/usr.bin/xlint/lint1/Makefile.err-msgs-h   Tue Jul 05 22:20:55 2022 +0000
+++ b/usr.bin/xlint/lint1/Makefile.err-msgs-h   Tue Jul 05 22:50:41 2022 +0000
@@ -1,9 +1,9 @@
-#      $NetBSD: Makefile.err-msgs-h,v 1.3 2021/04/10 23:51:37 rillig Exp $
+#      $NetBSD: Makefile.err-msgs-h,v 1.4 2022/07/05 22:50:41 rillig Exp $
 
 err-msgs.h: err.c Makefile.err-msgs-h
        ${_MKTARGET_CREATE}
        sp='[[:space:]]*'; \
-       from="^$$sp\(\".*\"\)\,$$sp/\*$$sp\([0-9][0-9]*\)$$sp\*/\$$"; \
+       from="^$$sp\(\".*\"\)\,$$sp/\*$$sp\(Q*[0-9][0-9]*\)$$sp\*/\$$"; \
        ${TOOL_SED} -n -e "s,$$from,#define MSG_\2 \1,p" < ${.ALLSRC:M*err.c} > ${.TARGET}.tmp
        mv -f ${.TARGET}.tmp ${.TARGET}
 
diff -r 7d8252825929 -r 9377af232ae5 usr.bin/xlint/lint1/README.md
--- a/usr.bin/xlint/lint1/README.md     Tue Jul 05 22:20:55 2022 +0000
+++ b/usr.bin/xlint/lint1/README.md     Tue Jul 05 22:50:41 2022 +0000
@@ -1,4 +1,4 @@
-[//]: # ($NetBSD: README.md,v 1.7 2022/07/03 19:47:34 rillig Exp $)
+[//]: # ($NetBSD: README.md,v 1.8 2022/07/05 22:50:41 rillig Exp $)
 
 # Introduction
 
@@ -43,8 +43,8 @@
 
 ## Configurable diagnostic messages
 
-Whether lint prints a message and whether each message is an error or a 
-warning depends on several things:
+Whether lint prints a message and whether each message is an error, a warning
+or just informational depends on several things:
 
 * The language level, with its possible values:
     * traditional C (`-t`)
@@ -61,6 +61,8 @@
   types, the option `-aa` extends this check to small integer types as well,
   reusing the same message ID.
 * The option `-X` suppresses arbitrary messages by their message ID.
+* The option `-q` enables additional queries that are not suitable as regular
+  warnings but may be interesting to look at on a case-by-case basis.
 
 # Fundamental types
 
diff -r 7d8252825929 -r 9377af232ae5 usr.bin/xlint/lint1/check-msgs.lua
--- a/usr.bin/xlint/lint1/check-msgs.lua        Tue Jul 05 22:20:55 2022 +0000
+++ b/usr.bin/xlint/lint1/check-msgs.lua        Tue Jul 05 22:50:41 2022 +0000
@@ -1,5 +1,5 @@
 #! /usr/bin/lua
--- $NetBSD: check-msgs.lua,v 1.16 2022/07/03 21:17:24 rillig Exp $
+-- $NetBSD: check-msgs.lua,v 1.17 2022/07/05 22:50:41 rillig Exp $
 
 --[[
 
@@ -16,7 +16,7 @@
 
   local f = assert(io.open("err.c"))
   for line in f:lines() do
-    local msg, id = line:match("%s*\"(.+)\",%s*/%*%s*(%d+)%s*%*/$")
+    local msg, id = line:match("%s*\"(.+)\",%s*/%*%s*(Q?%d+)%s*%*/$")
     if msg ~= nil then
       msgs[id] = msg
     end
@@ -61,14 +61,15 @@
     fname, lineno, id, msg, comment)
 end
 
-local is_message_function = {
-  error = true,
-  error_at = true,
-  warning = true,
-  warning_at = true,
-  c99ism = true,
-  c11ism = true,
-  gnuism = true,
+local message_prefix = {
+  error = "",
+  error_at = "",
+  warning = "",
+  warning_at = "",
+  query_message = "Q",
+  c99ism = "",
+  c11ism = "",
+  gnuism = "",
 }
 
 local function check_file(fname, msgs)
@@ -79,7 +80,9 @@
     lineno = lineno + 1
 
     local func, id = line:match("^%s+([%w_]+)%((%d+)[),]")
-    if is_message_function[func] then
+    local prefix = message_prefix[func]
+    if prefix then
+      id = prefix .. id
       local comment = prev:match("^%s+/%* (.+) %*/$")
       if comment ~= nil then
         check_message(fname, lineno, id, comment, msgs)
diff -r 7d8252825929 -r 9377af232ae5 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Tue Jul 05 22:20:55 2022 +0000
+++ b/usr.bin/xlint/lint1/err.c Tue Jul 05 22:50:41 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: err.c,v 1.180 2022/07/02 11:17:54 rillig Exp $ */
+/*     $NetBSD: err.c,v 1.181 2022/07/05 22:50:41 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.180 2022/07/02 11:17:54 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.181 2022/07/05 22:50:41 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -706,3 +706,57 @@
        va_end(ap);
        return severity > 0;
 }
+
+
+static const char *queries[] = {
+       "",                     /* unused, to make queries 1-based */
+       "implicit conversion from floating point '%s' to integer '%s'", /* Q1 */
+       "cast from floating point '%s' to integer '%s'",              /* Q2 */
+       "implicit conversion changes sign from '%s' to '%s'",         /* Q3 */
+       "usual arithmetic conversion for '%s' from '%s' to '%s'",     /* Q4 */
+       "pointer addition has integer on the left-hand side",         /* Q5 */
+       "no-op cast from '%s' to '%s'",                               /* Q6 */
+       "redundant cast from '%s' to '%s' before assignment",         /* Q7 */



Home | Main Index | Thread Index | Old Index