Skip to content
jorgeazevedo edited this page Jul 27, 2012 · 12 revisions

Blocks and their respective GUIs compile with a bunch of compiler warning flags. This is a list and explanation of what they do.

**Heads up: ** -Wall -Wextra -W might be a better approach if possible. I don't know what -W but I should figure it out.

Blocks

Besides the default -Wall imposed by Xenomai [1], we also add

-Wextra -Wundef -Wpointer-arith\
-Wcast-align -Wwrite-strings -Wswitch-enum -Wformat=2 -Wdisabled-optimization\
-Wmissing-include-dirs -Wfloat-equal -Wstrict-prototypes -Wstrict-overflow=5\
-Wswitch-default -Wconversion -Wsign-conversion

However, these are the ones I haven't rejected yet:

-Wundef -Wpointer-arith\
-Wcast-align -Wswitch-enum -Wdisabled-optimization\
-Wmissing-include-dirs -Wfloat-equal -Wstrict-prototypes

TODO

Implementing all these warning flags is actual very invasive to the code and has to be done with extreme care. Here's what I did in the past:

commit db3ab87c26b77e5bbfc4596008e2e00788e007bb Author: Jorge Azevedo jorge.amado.azevedo@gmail.com Date: Thu Jul 26 18:51:14 2012 +0100

Fix -Wstrict-prototypes and -Wstrict-overflow=5

Forgot some prototypes on the last commit.
The strict-overflow is only a _partial_ fix because it requires
a complete reworking of the Matrix type. Namely, it must use
size_t instead of unsigned shorts for its rows and columns. And
all the lib must adhere to this convention.

commit fc25dc248fddd4f186c48b74342782c131e53a11 Author: Jorge Azevedo jorge.amado.azevedo@gmail.com Date: Thu Jul 26 18:34:14 2012 +0100

Fix -Wstrict-prototypes warnings

My usage of function() and function(void) was inconsistent.
Also fixed in settings.h, which means I should submit it to the
author as a patch.

commit 3a6ce1c4e16509f675e2160efc0995f5d8cb9b53 Author: Jorge Azevedo jorge.amado.azevedo@gmail.com Date: Thu Jul 26 18:26:09 2012 +0100

Fix -Wfloat-equal warnings

There was a comparisson to zero on a floating point number in the
matrix library. I changed it to "< 0.001" as a hack and left
references for further studying. This really isn't my issue as I'm
not the original programmer of the library.

commit 45aaffbeeb64e16f07269c3488eb11f9162b4ef8 Author: Jorge Azevedo jorge.amado.azevedo@gmail.com Date: Thu Jul 26 17:42:32 2012 +0100

Fix -Wwrite-strings warnings

Mostly using const char whenever string literals are passed.

Gui

Besides the default -Wall imposed by Qt, we also add

-Wextra -Wundef -Wpointer-arith -Wcast-align\
-Wwrite-strings -Wcast-qual -Wswitch-enum -Wformat=2 -Wctor-dtor-privacy\
-Wdisabled-optimization -Wmissing-include-dirs -Woverloaded-virtual\
-Wsign-promo -Wstrict-null-sentinel -Wno-unused

#Rejected

I really screwed up with this one. During initial string argument parsing in rt_block_io, I have an entangled mess of const/non-const char and literals.

-Wwrite-strings

I'm just being lazy with this one. I've been at it for hours, and frankly I'm afraid of touching the code so much without unit testing in place

-Wconversion Wsign-conversion

This is actually useful most of the time, but the settings lib has a switch without a default I don't know what to do.

 -Wswitch-default

This one requires an overall rework of the Matrix type. Namely, instead of unsigned shorts for rows and columns, use size_t (or ssize_t). If this is a Linux only type it's creating an unnecessary portability issue. The Matrix lib was originally developped in Windows, for the record.

-Wstrict-overflow=5

An input string is passed on to printf in matrix_print_pretty(), so I cannot use this one. And I check if it's valid before passing it to printf, by the way.

-Wformat=2

This one is pulling -Wunused-parameter which is a pain because I have unused parameters in every block.

-Wextra

I'm very tempeted to put this one in:

-Werror

These can't be used because they make other libs blow up, but should be used for code cleanup:

-pedantic -Wredundant-decls -Wno-unused -Wunreachable-code

This one might be usefull for profiling:

 -finstrument-functions

I should probably implement this one (http://tinymicros.com/blog/index.php?s=ftrapv https://gist.github.com/1004768):

-ftrapv

Doesn't allow Matrix to be returned. Should be passed by the caller as a reference. This one is complicated because it overuses the heap and is an unnecessary performance bottleneck [3]:

-Waggregate-return

Needs patch on strmap. Maybe I should make this and even submit the patch:

-Wshadow -Wsign-conversion

This makes you turn off the warning manually, which sucks, but should be implemented in the library:

-Wno-unused

Invalid:

-Wnoexcept

Invalid for C:

-Wctor-dtor-privacy -Woverloaded-virtual -Wsign-promo -Wstrict-null-sentinel

Invalid for C++:

-Wstrict-prototypes

This throws an error on the common Qt convention Class::Class(QWidget *parent,) :

-Wshadow

Qt libs go bananas:

-Wfloat-equal -Wstrict-overflow=5 -Wswitch-default -Wconversion -Wunreachable-code -Wsign-conversion

Xenomai goes bananas:

-pedantic -Wold-style-cast -Wcast-qual

Quick reference

  • Wextra, -Wall: essential.
  • Wfloat-equal: useful because usually testing floating-point numbers for equality is bad.
  • Wundef: warn if an uninitialized identifier is evaluated in an #if directive.
  • Wshadow: warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.
  • Wpointer-arith: warn if anything depends upon the size of a function or of void.
  • Wcast-align: warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries.
  • Wstrict-prototypes: warn if a function is declared or defined without specifying the argument types.
  • Wstrict-overflow=5: warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. (The value 5 may be too strict, see the manual page.)
  • Wwrite-strings: give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning.
  • Waggregate-return: warn if any functions that return structures or unions are defined or called.
  • Wcast-qual: warn whenever a pointer is cast to remove a type qualifier from the target type*.
  • Wswitch-default: warn whenever a switch statement does not have a default case*.
  • Wswitch-enum: warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration*.
  • Wconversion: warn for implicit conversions that may alter a value*.
  • Wunreachable-code: warn if the compiler detects that code will never be executed*.
  • Wformat=2: Extra format checks on printf/scanf functions

References

[1] xeno-config --skin=native --cflags yields -I/usr/include/xenomai -D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__

[1] http://stackoverflow.com/questions/5088460/flags-to-enable-thorough-and-verbose-g-warnings/9862800#9862800

[2] http://stackoverflow.com/questions/3375697/useful-gcc-flags-for-c

[3] http://www.cplusplus.com/forum/general/38007/