Module Name:    src
Committed By:   rillig
Date:           Sat Oct  9 13:57:55 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_338.c msg_338.exp

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


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_338.c
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/msg_338.exp

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/lint1/msg_338.c
diff -u src/tests/usr.bin/xlint/lint1/msg_338.c:1.5 src/tests/usr.bin/xlint/lint1/msg_338.c:1.6
--- src/tests/usr.bin/xlint/lint1/msg_338.c:1.5	Sun Aug 22 22:15:07 2021
+++ src/tests/usr.bin/xlint/lint1/msg_338.c	Sat Oct  9 13:57:55 2021
@@ -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 @@ question_option(int argc, char **argv)
 	}
 	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;
+}

Index: src/tests/usr.bin/xlint/lint1/msg_338.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_338.exp:1.3 src/tests/usr.bin/xlint/lint1/msg_338.exp:1.4
--- src/tests/usr.bin/xlint/lint1/msg_338.exp:1.3	Sun Aug 22 22:15:07 2021
+++ src/tests/usr.bin/xlint/lint1/msg_338.exp	Sat Oct  9 13:57:55 2021
@@ -2,3 +2,6 @@ msg_338.c(26): warning: option 'e' shoul
 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]

Reply via email to