From a82d6070640d5f024063b7a163e450a7d61a2160 Mon Sep 17 00:00:00 2001
From: Robin Getz <robin.getz@analog.com>
Date: Sun, 29 Mar 2020 19:52:47 -0400
Subject: [PATCH] examples: Add -Wextra & -pedantic-errors to the example
 Makefile

can clean up the resulting errors. This resolves a few codacy issues
that have been lingering for awhile. There shouldn't be any functional
changes from these. I tested most, but not all, since they were all
pretty similar.

Signed-off-by: Robin Getz <robin.getz@analog.com>
---
 examples/Makefile             |  2 +-
 examples/ad9361-iiostream.c   |  4 ++--
 examples/ad9371-iiostream.c   |  6 ++---
 examples/adrv9009-iiostream.c |  6 ++---
 examples/dummy-iiostream.c    | 43 +++++++++++++++++++----------------
 examples/iio-monitor.c        | 10 ++++----
 6 files changed, 38 insertions(+), 33 deletions(-)

diff --git a/examples/Makefile b/examples/Makefile
index 4f23d2b39..8273ad49a 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -16,7 +16,7 @@
 
 TARGETS := ad9361-iiostream ad9371-iiostream adrv9009-iiostream dummy-iiostream iio-monitor
 
-CFLAGS = -Wall -DIIO_CHECK_RET
+CFLAGS = -Wall -Wextra -pedantic-errors -DIIO_CHECK_RET
 
 UNAME_S := $(shell uname -s)
 
diff --git a/examples/ad9361-iiostream.c b/examples/ad9361-iiostream.c
index 583868906..c5c449138 100644
--- a/examples/ad9361-iiostream.c
+++ b/examples/ad9361-iiostream.c
@@ -87,7 +87,7 @@ static void shutdown()
 
 static void handle_sig(int sig)
 {
-	printf("Waiting for process to finish...\n");
+	printf("Waiting for process to finish... Got signal %d\n", sig);
 	stop = true;
 }
 
@@ -134,7 +134,7 @@ static bool get_ad9361_stream_dev(struct iio_context *ctx, enum iodev d, struct
 }
 
 /* finds AD9361 streaming IIO channels */
-static bool get_ad9361_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn)
+static bool get_ad9361_stream_ch(__notused struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, struct iio_channel **chn)
 {
 	*chn = iio_device_find_channel(dev, get_ch_name("voltage", chid), d == TX);
 	if (!*chn)
diff --git a/examples/ad9371-iiostream.c b/examples/ad9371-iiostream.c
index e34a172e9..42063c184 100644
--- a/examples/ad9371-iiostream.c
+++ b/examples/ad9371-iiostream.c
@@ -85,7 +85,7 @@ static void shutdown()
 
 static void handle_sig(int sig)
 {
-	printf("Waiting for process to finish...\n");
+	printf("Waiting for process to finish... Got signal %d\n",sig);
 	stop = true;
 }
 
@@ -152,7 +152,7 @@ static bool get_ad9371_stream_dev(struct iio_context *ctx, enum iodev d, struct
 }
 
 /* finds AD9371 streaming IIO channels */
-static bool get_ad9371_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, char modify, struct iio_channel **chn)
+static bool get_ad9371_stream_ch(__notused struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, char modify, struct iio_channel **chn)
 {
 	*chn = iio_device_find_channel(dev, modify ? get_ch_name_mod("voltage", chid, modify) : get_ch_name("voltage", chid), d == TX);
 	if (!*chn)
@@ -201,7 +201,7 @@ bool cfg_ad9371_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg, en
 }
 
 /* simple configuration and streaming */
-int main (int argc, char **argv)
+int main (__notused int argc, __notused char **argv)
 {
 	// Streaming devices
 	struct iio_device *tx;
diff --git a/examples/adrv9009-iiostream.c b/examples/adrv9009-iiostream.c
index de73f3e9e..58b0805a1 100644
--- a/examples/adrv9009-iiostream.c
+++ b/examples/adrv9009-iiostream.c
@@ -85,7 +85,7 @@ static void shutdown()
 
 static void handle_sig(int sig)
 {
-	printf("Waiting for process to finish...\n");
+	printf("Waiting for process to finish... Got signal %d\n", sig);
 	stop = true;
 }
 
@@ -152,7 +152,7 @@ static bool get_adrv9009_stream_dev(struct iio_context *ctx, enum iodev d, struc
 }
 
 /* finds adrv9009 streaming IIO channels */
-static bool get_adrv9009_stream_ch(struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, char modify, struct iio_channel **chn)
+static bool get_adrv9009_stream_ch(__notused struct iio_context *ctx, enum iodev d, struct iio_device *dev, int chid, char modify, struct iio_channel **chn)
 {
 	*chn = iio_device_find_channel(dev, modify ? get_ch_name_mod("voltage", chid, modify) : get_ch_name("voltage", chid), d == TX);
 	if (!*chn)
@@ -197,7 +197,7 @@ bool cfg_adrv9009_streaming_ch(struct iio_context *ctx, struct stream_cfg *cfg,
 }
 
 /* simple configuration and streaming */
-int main (int argc, char **argv)
+int main (__notused int argc, __notused char **argv)
 {
 	// Streaming devices
 	struct iio_device *tx;
diff --git a/examples/dummy-iiostream.c b/examples/dummy-iiostream.c
index 5dedafd68..c9d967815 100644
--- a/examples/dummy-iiostream.c
+++ b/examples/dummy-iiostream.c
@@ -114,7 +114,7 @@ static struct iio_device *dev;
 static struct iio_context *ctx;
 static struct iio_buffer  *rxbuf;
 static struct iio_channel **channels;
-static int channel_count;
+static unsigned int channel_count;
 
 static bool stop;
 static bool has_repeat;
@@ -146,17 +146,17 @@ static void shutdown()
 
 static void handle_sig(int sig)
 {
-	printf("Waiting for process to finish...\n");
+	printf("Waiting for process to finish... got signal : %d\n", sig);
 	stop = true;
 }
 
-static ssize_t sample_cb(const struct iio_channel *chn, void *src, size_t bytes, void *d)
+static ssize_t sample_cb(const struct iio_channel *chn, void *src, size_t bytes, __notused void *d)
 {
 	const struct iio_data_format *fmt = iio_channel_get_data_format(chn);
-	unsigned int repeat = has_repeat ? fmt->repeat : 1;
+	unsigned int j, repeat = has_repeat ? fmt->repeat : 1;
 
 	printf("%s ", iio_channel_get_id(chn));
-	for (int j = 0; j < repeat; ++j) {
+	for (j = 0; j < repeat; ++j) {
 		if (bytes == sizeof(int16_t))
 			printf("%" PRIi16 " ", ((int16_t *)src)[j]);
 		else if (bytes == sizeof(int64_t))
@@ -166,7 +166,7 @@ static ssize_t sample_cb(const struct iio_channel *chn, void *src, size_t bytes,
 	return bytes * repeat;
 }
 
-static void usage(int argc, char *argv[])
+static void usage(__notused int argc, char *argv[])
 {
 	printf("Usage: %s [OPTION]\n", argv[0]);
 	printf("  -d\tdevice name (default \"iio_dummy_part_no\")\n");
@@ -227,13 +227,14 @@ int main (int argc, char **argv)
 	// Listen to ctrl+c and assert
 	signal(SIGINT, handle_sig);
 
-	unsigned int major, minor;
+	unsigned int i, j, major, minor;
 	char git_tag[8];
 	iio_library_get_version(&major, &minor, git_tag);
 	printf("Library version: %u.%u (git tag: %s)\n", major, minor, git_tag);
 
-	/* check for struct iio_data_format.repeat support */
-	has_repeat = major >= 0 && minor >= 8 ? true : false;
+	/* check for struct iio_data_format.repeat support
+	 * 0.8 has repeat support, don't need to check major, it's unsigned */
+	has_repeat = minor >= 8 ? true : false;
 
 	printf("* Acquiring IIO context\n");
 	assert((ctx = iio_create_default_context()) && "No context");
@@ -247,7 +248,7 @@ int main (int argc, char **argv)
 	}
 
 	printf("* Initializing IIO streaming channels:\n");
-	for (int i = 0; i < iio_device_get_channels_count(dev); ++i) {
+	for (i = 0; i < iio_device_get_channels_count(dev); ++i) {
 		struct iio_channel *chn = iio_device_get_channel(dev, i);
 		if (iio_channel_is_scan_element(chn)) {
 			printf("%s\n", iio_channel_get_id(chn));
@@ -263,7 +264,7 @@ int main (int argc, char **argv)
 		perror("Channel array allocation failed");
 		shutdown();
 	}
-	for (int i = 0; i < channel_count; ++i) {
+	for (i = 0; i < channel_count; ++i) {
 		struct iio_channel *chn = iio_device_get_channel(dev, i);
 		if (iio_channel_is_scan_element(chn))
 			channels[i] = chn;
@@ -277,7 +278,7 @@ int main (int argc, char **argv)
 	}
 
 	printf("* Enabling IIO streaming channels for buffered capture\n");
-	for (int i = 0; i < channel_count; ++i)
+	for (i = 0; i < channel_count; ++i)
 		iio_channel_enable(channels[i]);
 
 	printf("* Enabling IIO buffer trigger\n");
@@ -299,7 +300,11 @@ int main (int argc, char **argv)
 	while (!stop)
 	{
 		ssize_t nbytes_rx;
-		void *p_dat, *p_end;
+		/* we use a char pointer, rather than a void pointer, for p_dat & p_end
+		 * to ensure the compiler understands the size is a byte, and then we
+		 * can do math on it.
+		 */
+		char *p_dat, *p_end;
 		ptrdiff_t p_inc;
 		int64_t now_ts;
 
@@ -325,13 +330,13 @@ int main (int argc, char **argv)
 		switch (buffer_read_method)
 		{
 		case BUFFER_POINTER:
-			for (int i = 0; i < channel_count; ++i) {
+			for (i = 0; i < channel_count; ++i) {
 				const struct iio_data_format *fmt = iio_channel_get_data_format(channels[i]);
 				unsigned int repeat = has_repeat ? fmt->repeat : 1;
 
 				printf("%s ", iio_channel_get_id(channels[i]));
 				for (p_dat = iio_buffer_first(rxbuf, channels[i]); p_dat < p_end; p_dat += p_inc) {
-					for (int j = 0; j < repeat; ++j) {
+					for (j = 0; j < repeat; ++j) {
 						if (fmt->length/8 == sizeof(int16_t))
 							printf("%" PRIi16 " ", ((int16_t *)p_dat)[j]);
 						else if (fmt->length/8 == sizeof(int64_t))
@@ -355,9 +360,9 @@ int main (int argc, char **argv)
 		}
 		case CHANNEL_READ_RAW:
 		case CHANNEL_READ:
-			for (int i = 0; i < channel_count; ++i) {
+			for (i = 0; i < channel_count; ++i) {
 				uint8_t *buf;
-				size_t bytes;
+				size_t sample, bytes;
 				const struct iio_data_format *fmt = iio_channel_get_data_format(channels[i]);
 				unsigned int repeat = has_repeat ? fmt->repeat : 1;
 				size_t sample_size = fmt->length / 8 * repeat;
@@ -370,8 +375,8 @@ int main (int argc, char **argv)
 					bytes = iio_channel_read(channels[i], rxbuf, buf, sample_size * buffer_length);
 
 				printf("%s ", iio_channel_get_id(channels[i]));
-				for (int sample = 0; sample < bytes / sample_size; ++sample) {
-					for (int j = 0; j < repeat; ++j) {
+				for (sample = 0; sample < bytes / sample_size; ++sample) {
+					for (j = 0; j < repeat; ++j) {
 						if (fmt->length / 8 == sizeof(int16_t))
 							printf("%" PRIi16 " ", ((int16_t *)buf)[sample+j]);
 						else if (fmt->length / 8 == sizeof(int64_t))
diff --git a/examples/iio-monitor.c b/examples/iio-monitor.c
index 78057d250..96be7b23f 100644
--- a/examples/iio-monitor.c
+++ b/examples/iio-monitor.c
@@ -38,9 +38,9 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
 
-#define RED	020
-#define YELLOW	040
-#define BLUE	050
+#define RED	020u
+#define YELLOW	040u
+#define BLUE	050u
 
 static int selected = -1;
 
@@ -224,11 +224,11 @@ static struct iio_context *show_contexts_screen(void)
 	struct iio_context *ctx = NULL;
 	struct iio_scan_context *scan_ctx;
 	struct iio_context_info **info;
-	unsigned int num_contexts;
+	int num_contexts;
 	CDKSCREEN *screen;
 	CDKSCROLL *list;
 	const char *uri;
-	unsigned int i;
+	int i;
 	bool free_uri;
 	char **items;
 	int ret;