]> git.puffer.fish Git - mirror/frr.git/commitdiff
tools/gcc-plugins: add small test for frr-format
authorDavid Lamparter <equinox@diac24.net>
Sun, 29 Mar 2020 08:22:15 +0000 (10:22 +0200)
committerDavid Lamparter <equinox@diac24.net>
Sun, 29 Mar 2020 08:45:46 +0000 (10:45 +0200)
Just enough to check that it works.

Signed-off-by: David Lamparter <equinox@diac24.net>
tools/gcc-plugins/format-test.c [new file with mode: 0644]
tools/gcc-plugins/format-test.py [new file with mode: 0644]

diff --git a/tools/gcc-plugins/format-test.c b/tools/gcc-plugins/format-test.c
new file mode 100644 (file)
index 0000000..b031ca5
--- /dev/null
@@ -0,0 +1,107 @@
+#include <stddef.h>
+#include <stdlib.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+typedef unsigned long mytype;
+typedef size_t mysize;
+
+typedef unsigned int not_in_addr_t;
+typedef in_addr_t yes_in_addr_t;
+typedef struct in_addr in_addr_s;
+
+struct other {
+       int x;
+};
+
+int testfn(const char *fmt, ...) __attribute__((frr_format("frr_printf", 1, 2)));
+
+#ifndef _FRR_ATTRIBUTE_PRINTFRR
+#error please load the frr-format plugin
+#endif
+
+#pragma FRR printfrr_ext "%pI4" (struct in_addr *)
+#pragma FRR printfrr_ext "%pI4" (in_addr_t *)
+
+int test(unsigned long long ay)
+{
+       size_t v_size_t = 0;
+       long v_long = 0;
+       int v_int = 0;
+       uint64_t v_uint64_t = 0;
+       mytype v_mytype = 0;
+       mysize v_mysize = 0;
+       pid_t v_pid_t = 0;
+
+       testfn("%zu", v_size_t);                // NOWARN
+       testfn("%zu", v_long);                  // WARN
+       testfn("%zu", v_int);                   // WARN
+       testfn("%zu", sizeof(v_int));           // NOWARN
+       testfn("%zu", v_mytype);                // WARN
+       testfn("%zu", v_mysize);                // NOWARN
+       testfn("%zu", v_uint64_t);              // WARN
+       testfn("%zu", v_pid_t);                 // WARN
+
+       testfn("%lu", v_long);                  // NOWARN PEDANTIC
+       testfn("%lu", v_int);                   // WARN
+       testfn("%lu", v_size_t);                // WARN
+       testfn("%lu", sizeof(v_int));           // NOWARN (integer constant)
+       testfn("%lu", v_uint64_t);              // WARN
+       testfn("%lu", v_pid_t);                 // WARN
+
+       testfn("%ld", v_long);                  // NOWARN
+       testfn("%ld", v_int);                   // WARN
+       testfn("%ld", v_size_t);                // WARN
+       testfn("%ld", sizeof(v_int));           // NOWARN (integer constant)
+       testfn("%ld", v_uint64_t);              // WARN
+       testfn("%ld", v_pid_t);                 // WARN
+
+       testfn("%d",  v_int);                   // NOWARN
+       testfn("%d",  v_long);                  // WARN
+       testfn("%d",  v_size_t);                // WARN
+       testfn("%d",  sizeof(v_int));           // WARN
+       testfn("%d",  v_uint64_t);              // WARN
+       testfn("%d",  v_pid_t);                 // WARN
+
+       testfn("%Lu", v_size_t);                // WARN
+       testfn("%Lu", v_long);                  // WARN
+       testfn("%Lu", v_int);                   // WARN
+       testfn("%Lu", sizeof(v_int));           // NOWARN (integer constant)
+       testfn("%Lu", v_mytype);                // WARN
+       testfn("%Lu", v_mysize);                // WARN
+       testfn("%Lu", v_pid_t);                 // WARN
+       testfn("%Lu", v_uint64_t);              // NOWARN
+
+       testfn("%Ld", v_size_t);                // WARN
+       testfn("%Ld", v_long);                  // WARN
+       testfn("%Ld", v_int);                   // WARN
+       testfn("%Ld", sizeof(v_int));           // NOWARN (integer constant)
+       testfn("%Ld", v_mytype);                // WARN
+       testfn("%Ld", v_mysize);                // WARN
+       testfn("%Ld", v_pid_t);                 // WARN
+       testfn("%Ld", v_uint64_t);              // NOWARN
+
+       testfn("%pI4", &v_long);                // WARN
+
+       in_addr_t v_in_addr_t;
+       yes_in_addr_t v_yes_in_addr_t;
+       not_in_addr_t v_not_in_addr_t;
+       void *v_voidp = &v_in_addr_t;
+
+       testfn("%pI4", &v_in_addr_t);           // NOWARN
+       testfn("%pI4", &v_yes_in_addr_t);       // NOWARN
+       testfn("%pI4", &v_not_in_addr_t);       // WARN
+       testfn("%pI4", v_voidp);                // WARN
+
+       struct in_addr v_in_addr;
+       in_addr_s v_in_addr_s;
+       struct other v_other;
+       const struct in_addr *v_in_addr_const = &v_in_addr;
+
+       testfn("%pI4", &v_in_addr);             // NOWARN
+       testfn("%pI4", &v_in_addr_s);           // NOWARN
+       testfn("%pI4", &v_other);               // WARN
+       testfn("%pI4", v_in_addr_const);        // NOWARN
+       return 0;
+}
diff --git a/tools/gcc-plugins/format-test.py b/tools/gcc-plugins/format-test.py
new file mode 100644 (file)
index 0000000..cc6ca61
--- /dev/null
@@ -0,0 +1,57 @@
+import subprocess
+import sys
+import shlex
+import os
+import re
+
+os.environ['LC_ALL'] = 'C'
+os.environ['LANG'] = 'C'
+for k in list(os.environ.keys()):
+    if k.startswith('LC_'):
+        os.environ.pop(k)
+
+c_re = re.compile(r'//\s+(NO)?WARN')
+expect = {}
+lines = {}
+
+with open('format-test.c', 'r') as fd:
+    for lno, line in enumerate(fd.readlines(), 1):
+        lines[lno] = line.strip()
+        m = c_re.search(line)
+        if m is None:
+            continue
+        if m.group(1) is None:
+            expect[lno] = 'warn'
+        else:
+            expect[lno] = 'nowarn'
+
+cmd = shlex.split('gcc -Wall -Wextra -Wno-unused -fplugin=./frr-format.so -fno-diagnostics-show-caret -c -o format-test.o format-test.c')
+
+gcc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+sout, serr = gcc.communicate()
+gcc.wait()
+
+gcclines = serr.decode('UTF-8').splitlines()
+line_re = re.compile(r'^format-test\.c:(\d+):(.*)$')
+gcc_warns = {}
+
+for line in gcclines:
+    if line.find('In function') >= 0:
+        continue
+    m = line_re.match(line)
+    if m is None:
+        sys.stderr.write('cannot process GCC output: %s\n' % line)
+        continue
+
+    lno = int(m.group(1))
+    gcc_warns.setdefault(lno, []).append(line)
+
+for lno, val in expect.items():
+    if val == 'nowarn' and lno in gcc_warns:
+        sys.stderr.write('unexpected gcc warning on line %d:\n\t%s\n\t%s\n' % (lno, lines[lno], '\n\t'.join(gcc_warns[lno])))
+    if val == 'warn' and lno not in gcc_warns:
+        sys.stderr.write('expected warning on line %d but did not get one\n\t%s\n' % (lno, lines[lno]))
+
+leftover = set(gcc_warns.keys()) - set(expect.keys())
+for lno in sorted(leftover):
+    sys.stderr.write('unmarked gcc warning on line %d:\n\t%s\n\t%s\n' % (lno, lines[lno], '\n\t'.join(gcc_warns[lno])))