Source-Changes-HG archive

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

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: test getopt with an op...



details:   https://anonhg.NetBSD.org/src/rev/ce853bc1474a
branches:  trunk
changeset: 1024084:ce853bc1474a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Oct 09 13:57:55 2021 +0000

description:
tests/lint: test getopt with an options string starting with ':'

diffstat:

 tests/usr.bin/xlint/lint1/msg_338.c   |  73 ++++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_338.exp |   3 +
 2 files changed, 75 insertions(+), 1 deletions(-)

diffs (94 lines):

diff -r cafd1927130f -r ce853bc1474a tests/usr.bin/xlint/lint1/msg_338.c
--- a/tests/usr.bin/xlint/lint1/msg_338.c       Sat Oct 09 13:09:17 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_338.c       Sat Oct 09 13:57:55 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_338.c,v 1.5 2021/08/22 22:15:07 rillig Exp $       */
+/*     $NetBSD: msg_338.c,v 1.6 2021/10/09 13:57:55 rillig Exp $       */
 # 3 "msg_338.c"
 
 // Test for message: option '%c' should be handled in the switch [338]
@@ -79,3 +79,74 @@
        }
        return 0;
 }
+
+/*
+ * If the first character of the options string is ':', getopt does not print
+ * its own error messages. Getopt returns ':' if an option is missing its
+ * argument; that is handled by the 'default:' already.
+ */
+int
+suppress_errors(int argc, char **argv)
+{
+       int c;
+
+       /* expect+1: warning: option 'o' should be handled in the switch [338] */
+       while ((c = getopt(argc, argv, ":b:o")) != -1) {
+               switch (c) {
+               case 'b':
+                       return 'b';
+               default:
+                       usage();
+               }
+       }
+       return 0;
+}
+
+/*
+ * If the first character of the options string is ':', getopt returns ':'
+ * if an option is missing its argument. This condition can be handled
+ * separately from '?', which getopt returns for unknown options.
+ */
+int
+missing_argument(int argc, char **argv)
+{
+       int c;
+
+       /* expect+1: warning: option 'o' should be handled in the switch [338] */
+       while ((c = getopt(argc, argv, ":b:o")) != -1) {
+               switch (c) {
+               case 'b':
+                       return 'b';
+               case ':':
+                       return 'm';
+               default:
+                       usage();
+               }
+       }
+       return 0;
+}
+
+/*
+ * Getopt only returns ':' if ':' is the first character in the options
+ * string. Everywhere else, a ':' marks the preceding option as having a
+ * required argument. In theory, if the options string contained "a::x",
+ * that could be interpreted as '-a argument', followed by '-:' and '-x',
+ * but nobody does that.
+ */
+int
+unreachable_colon(int argc, char **argv)
+{
+       int c;
+
+       /* expect+1: warning: option 'b' should be handled in the switch [338] */
+       while ((c = getopt(argc, argv, "b:")) != -1) {
+               switch (c) {
+               /* TODO: expect+1: warning: option ':' should be listed in the options string [339] */
+               case ':':
+                       return 'm';
+               default:
+                       usage();
+               }
+       }
+       return 0;
+}
diff -r cafd1927130f -r ce853bc1474a tests/usr.bin/xlint/lint1/msg_338.exp
--- a/tests/usr.bin/xlint/lint1/msg_338.exp     Sat Oct 09 13:09:17 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_338.exp     Sat Oct 09 13:57:55 2021 +0000
@@ -2,3 +2,6 @@
 msg_338.c(28): warning: option 'f' should be listed in the options string [339]
 msg_338.c(14): warning: option 'c' should be handled in the switch [338]
 msg_338.c(14): warning: option 'd' should be handled in the switch [338]
+msg_338.c(94): warning: option 'o' should be handled in the switch [338]
+msg_338.c(116): warning: option 'o' should be handled in the switch [338]
+msg_338.c(142): warning: option 'b' should be handled in the switch [338]



Home | Main Index | Thread Index | Old Index