Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: Add MSVC flags for Wall, Wno-sign-compare and Werror #472

Merged
merged 5 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ if (WITH_TESTS)
endif()

if (MSVC)
# Avoid annoying warnings from Visual Studio
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)

add_compile_options(/W4 /wd4018 /wd4200 /wd4127 /wd4100)
# C4018: signed/unsigned mismatch ; same as -Wno-sign-compare
# C4200: nonstandard extension used : zero-sized array in struct (usb.h)
# C4127: conditional expression is constant (IIO_ERROR and IIO_DEBUG macros)
# C4100: unreferenced parameter; same as -Wno-unused-parameter

if(DEFINED ENV{CI} AND DEFINED ENV{APPVEYOR})
message(STATUS "Running in an AppVeyor environment, setting -Werror")
add_compile_options(/WX)
endif()
set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll.a" ".a" ".lib")
endif()
Expand Down Expand Up @@ -101,6 +108,7 @@ endif()

include(CheckSymbolExists)
check_symbol_exists(strdup "string.h" HAS_STRDUP)
check_symbol_exists(strndup "string.h" HAS_STRNDUP)
check_symbol_exists(strerror_r "string.h" HAS_STRERROR_R)
check_symbol_exists(newlocale "locale.h" HAS_NEWLOCALE)

Expand Down
27 changes: 19 additions & 8 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ static bool device_is_high_speed(const struct iio_device *dev)
struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev,
size_t samples_count, bool cyclic)
{
int ret = -EINVAL;
ssize_t ret = -EINVAL;
struct iio_buffer *buf;
unsigned int sample_size = iio_device_get_sample_size(dev);
ssize_t sample_size = iio_device_get_sample_size(dev);

if (!sample_size || !samples_count)
goto err_set_errno;

if (sample_size < 0) {
ret = sample_size;
goto err_set_errno;
}

buf = malloc(sizeof(*buf));
if (!buf) {
ret = -ENOMEM;
goto err_set_errno;
}

buf->dev_sample_size = sample_size;
buf->dev_sample_size = (unsigned int) sample_size;
buf->length = sample_size * samples_count;
buf->dev = dev;
buf->mask = calloc(dev->words, sizeof(*buf->mask));
Expand Down Expand Up @@ -87,8 +92,11 @@ struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev,
}
}

buf->sample_size = iio_device_get_sample_size_mask(dev,
buf->mask, dev->words);
ret = iio_device_get_sample_size_mask(dev, buf->mask, dev->words);
if (ret < 0)
goto err_close_device;

buf->sample_size = (unsigned int) ret;
buf->data_length = buf->length;
return buf;

Expand All @@ -99,7 +107,7 @@ struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev,
err_free_buf:
free(buf);
err_set_errno:
errno = -ret;
errno = -(int)ret;
return NULL;
}

Expand All @@ -126,6 +134,7 @@ ssize_t iio_buffer_refill(struct iio_buffer *buffer)
{
ssize_t read;
const struct iio_device *dev = buffer->dev;
ssize_t ret;

if (buffer->dev_is_high_speed) {
read = dev->ctx->ops->get_buffer(dev, &buffer->buffer,
Expand All @@ -137,8 +146,10 @@ ssize_t iio_buffer_refill(struct iio_buffer *buffer)

if (read >= 0) {
buffer->data_length = read;
buffer->sample_size = iio_device_get_sample_size_mask(dev,
buffer->mask, dev->words);
ret = iio_device_get_sample_size_mask(dev, buffer->mask, dev->words);
if (ret < 0)
return ret;
buffer->sample_size = (unsigned int)ret;
}
return read;
}
Expand Down
16 changes: 8 additions & 8 deletions channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,33 +743,33 @@ int iio_channel_attr_read_double(const struct iio_channel *chn,
int iio_channel_attr_write_longlong(const struct iio_channel *chn,
const char *attr, long long val)
{
ssize_t ret;
int ret;
char buf[1024];
iio_snprintf(buf, sizeof(buf), "%lld", val);
ret = iio_channel_attr_write(chn, attr, buf);
ret = (int) iio_channel_attr_write(chn, attr, buf);
return ret < 0 ? ret : 0;
}

int iio_channel_attr_write_double(const struct iio_channel *chn,
const char *attr, double val)
{
ssize_t ret;
int ret;
char buf[1024];

ret = (ssize_t) write_double(buf, sizeof(buf), val);
ret = write_double(buf, sizeof(buf), val);
if (!ret)
ret = iio_channel_attr_write(chn, attr, buf);
ret = (int) iio_channel_attr_write(chn, attr, buf);
return ret < 0 ? ret : 0;
}

int iio_channel_attr_write_bool(const struct iio_channel *chn,
const char *attr, bool val)
{
ssize_t ret;
int ret;
if (val)
ret = iio_channel_attr_write_raw(chn, attr, "1", 2);
ret = (int) iio_channel_attr_write_raw(chn, attr, "1", 2);
else
ret = iio_channel_attr_write_raw(chn, attr, "0", 2);
ret = (int) iio_channel_attr_write_raw(chn, attr, "0", 2);
return ret < 0 ? ret : 0;
}

Expand Down
20 changes: 5 additions & 15 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "iio-config.h"
#include "iio-private.h"
#include "sort.h"
#include "network.h"

#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -328,22 +327,13 @@ struct iio_context * iio_create_context_from_uri(const char *uri)

struct iio_context * iio_create_default_context(void)
{
char *hostname = getenv("IIOD_REMOTE");
char *hostname = iio_getenv("IIOD_REMOTE");
struct iio_context * ctx;

if (hostname) {
if (strlen(hostname) > 2 && strlen(hostname) < MAXHOSTNAMELEN + 4) {
struct iio_context *ctx;

ctx = iio_create_context_from_uri(hostname);
if (ctx)
return ctx;
}
#ifdef HAVE_DNS_SD
/* If the environment variable is an empty string, we will
* discover the server using ZeroConf */
if (!hostname[0])
return iio_create_network_context(NULL);
#endif
ctx = iio_create_context_from_uri(hostname);
free(hostname);
return ctx;
}

return iio_create_local_context();
Expand Down
20 changes: 10 additions & 10 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ int iio_device_attr_write_longlong(const struct iio_device *dev,
iio_snprintf(buf, sizeof(buf), "%lld", val);
ret = iio_device_attr_write(dev, attr, buf);

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_attr_write_double(const struct iio_device *dev,
Expand All @@ -673,7 +673,7 @@ int iio_device_attr_write_double(const struct iio_device *dev,
ret = (ssize_t) write_double(buf, sizeof(buf), val);
if (!ret)
ret = iio_device_attr_write(dev, attr, buf);
return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_attr_write_bool(const struct iio_device *dev,
Expand All @@ -686,7 +686,7 @@ int iio_device_attr_write_bool(const struct iio_device *dev,
else
ret = iio_device_attr_write(dev, attr, "0");

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_buffer_attr_read_longlong(const struct iio_device *dev,
Expand Down Expand Up @@ -737,7 +737,7 @@ int iio_device_buffer_attr_write_longlong(const struct iio_device *dev,
iio_snprintf(buf, sizeof(buf), "%lld", val);
ret = iio_device_buffer_attr_write(dev, attr, buf);

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_buffer_attr_write_double(const struct iio_device *dev,
Expand All @@ -749,7 +749,7 @@ int iio_device_buffer_attr_write_double(const struct iio_device *dev,
ret = (ssize_t) write_double(buf, sizeof(buf), val);
if (!ret)
ret = iio_device_buffer_attr_write(dev, attr, buf);
return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_buffer_attr_write_bool(const struct iio_device *dev,
Expand All @@ -762,7 +762,7 @@ int iio_device_buffer_attr_write_bool(const struct iio_device *dev,
else
ret = iio_device_buffer_attr_write(dev, attr, "0");

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

ssize_t iio_device_debug_attr_read(const struct iio_device *dev,
Expand Down Expand Up @@ -853,7 +853,7 @@ int iio_device_debug_attr_write_longlong(const struct iio_device *dev,
iio_snprintf(buf, sizeof(buf), "%lld", val);
ret = iio_device_debug_attr_write(dev, attr, buf);

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_debug_attr_write_double(const struct iio_device *dev,
Expand All @@ -865,7 +865,7 @@ int iio_device_debug_attr_write_double(const struct iio_device *dev,
ret = (ssize_t) write_double(buf, sizeof(buf), val);
if (!ret)
ret = iio_device_debug_attr_write(dev, attr, buf);
return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_debug_attr_write_bool(const struct iio_device *dev,
Expand All @@ -878,7 +878,7 @@ int iio_device_debug_attr_write_bool(const struct iio_device *dev,
else
ret = iio_device_debug_attr_write_raw(dev, attr, "0", 2);

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_identify_filename(const struct iio_device *dev,
Expand Down Expand Up @@ -930,7 +930,7 @@ int iio_device_reg_write(struct iio_device *dev,
address, value);
ret = iio_device_debug_attr_write(dev, "direct_reg_access", buf);

return ret < 0 ? ret : 0;
return (int) (ret < 0 ? ret : 0);
}

int iio_device_reg_read(struct iio_device *dev,
Expand Down
1 change: 1 addition & 0 deletions iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#cmakedefine WITH_LOCAL_CONFIG
#cmakedefine HAS_PIPE2
#cmakedefine HAS_STRDUP
#cmakedefine HAS_STRNDUP
#cmakedefine HAS_STRERROR_R
#cmakedefine HAS_NEWLOCALE
#cmakedefine HAS_PTHREAD_SETNAME_NP
Expand Down
7 changes: 5 additions & 2 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#ifdef _MSC_BUILD
#define inline __inline
#define iio_snprintf sprintf_s
#define iio_sscanf sscanf_s
#else
#define iio_snprintf snprintf
#define iio_sscanf sscanf
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -286,8 +288,8 @@ struct iio_context * local_create_context(void);
struct iio_context * network_create_context(const char *hostname);
struct iio_context * xml_create_context_mem(const char *xml, size_t len);
struct iio_context * xml_create_context(const char *xml_file);
struct iio_context * usb_create_context(unsigned int bus, unsigned int address,
unsigned int interface);
struct iio_context * usb_create_context(unsigned int bus, uint16_t address,
uint16_t interface);
struct iio_context * usb_create_context_from_uri(const char *uri);
struct iio_context * serial_create_context_from_uri(const char *uri);

Expand All @@ -314,6 +316,7 @@ unsigned int find_channel_modifier(const char *s, size_t *len_p);

char *iio_strdup(const char *str);
size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);
char * iio_getenv (char * envvar);

int iio_context_add_attr(struct iio_context *ctx,
const char *key, const char *value);
Expand Down
8 changes: 4 additions & 4 deletions iiod-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ int iiod_client_get_version(struct iiod_client *client, void *desc,

iio_mutex_lock(client->lock);

ret = ops->write(pdata, desc, "VERSION\r\n", sizeof("VERSION\r\n") - 1);
ret = (int) ops->write(pdata, desc, "VERSION\r\n", sizeof("VERSION\r\n") - 1);
if (ret < 0) {
iio_mutex_unlock(client->lock);
return ret;
}

ret = ops->read_line(pdata, desc, buf, sizeof(buf));
ret = (int) ops->read_line(pdata, desc, buf, sizeof(buf));
iio_mutex_unlock(client->lock);

if (ret < 0)
Expand Down Expand Up @@ -325,7 +325,7 @@ static int iiod_client_discard(struct iiod_client *client, void *desc,

ret = iiod_client_read_all(client, desc, buf, read_len);
if (ret < 0)
return ret;
return (int) ret;

to_discard -= (size_t) ret;
} while (to_discard);
Expand Down Expand Up @@ -588,7 +588,7 @@ static int iiod_client_read_mask(struct iiod_client *client,
IIO_DEBUG("Reading mask\n");

for (i = words, ptr = buf; i > 0; i--) {
sscanf(ptr, "%08" PRIx32, &mask[i - 1]);
iio_sscanf(ptr, "%08" PRIx32, &mask[i - 1]);
IIO_DEBUG("mask[%lu] = 0x%08" PRIx32 "\n",
(unsigned long)(i - 1), mask[i - 1]);

Expand Down
16 changes: 14 additions & 2 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -1230,12 +1230,24 @@ static int handle_protected_scan_element_attr(struct iio_channel *chn,
char endian, sign;

if (strchr(buf, 'X')) {
sscanf(buf, "%ce:%c%u/%uX%u>>%u", &endian, &sign,
iio_sscanf(buf, "%ce:%c%u/%uX%u>>%u",
#ifdef _MSC_BUILD
&endian, sizeof(endian),
&sign, sizeof(sign),
#else
&endian, &sign,
#endif
&chn->format.bits, &chn->format.length,
&chn->format.repeat, &chn->format.shift);
} else {
chn->format.repeat = 1;
sscanf(buf, "%ce:%c%u/%u>>%u", &endian, &sign,
iio_sscanf(buf, "%ce:%c%u/%u>>%u",
#ifdef _MSC_BUILD
&endian, sizeof(endian),
&sign, sizeof(sign),
#else
&endian, &sign,
#endif
&chn->format.bits, &chn->format.length,
&chn->format.shift);
}
Expand Down
Loading