Page 1 of 1

chprintf(&SD1, ...) generates type mismatch warning

Posted: Mon Oct 28, 2013 9:20 am
by genosensor
chprintf(&SD1, ...) works fine. However, the compiler (gcc) complains that:

Code: Select all

main.c: In function 'main':
main.c:30:3: warning: passing argument 1 of 'chprintf' from incompatible pointer type [enabled by default]
/home/brent/chibios/os/various/chprintf.h:40:8: note: expected 'struct BaseSequentialStream *' but argument is of type 'const struct SerialDriver *'

I cannot see any way avoid this type mismatch, because...
The SerialDriver is laid out in memory so as to be compatible with the BaseSequentialStream, however it does not contain any references to the BaseSequentialStream typedef. Instead the struct layouts are defined in #define macros shared between the two types. However, this leaves the compiler unconvinced that the shared base types are, indeed, the same type.

Is there any elegant way to silence this warning without resorting to an explicit typecast or disabling it with a gcc command-line option?

If not, I'm thinking that something like:

Code: Select all

chprintf(&SD1.baseStream, ...)

would work if the SerialDriver (and other derived types) were defined in terms of BaseSequentialStream.

with something like:

Code: Select all

struct SerialDriver {
  serialDriverVMT  vmt;
  BaseSequentialStream  baseStream;   //includes base class vmt and data
  serialDriverData  data;
};

This memory layout should handle multiple levels of inheritance while keeping the entire vmt of derived classes contiguous.
Note that vmt and data need not be defined as single fields in the struct, so long as all the vmt entries appear before the baseStream and the data fields of the derived class appear after it.
The big change from the current scheme is that every derived class's methods appear first in its vmt, followed by those of the base classes. This allows the base class struct(s) to exist (unchanged) within the struct of any classes derived from it.
You lose the clumsy macros and gain type safety.

Is this worth considering?

Re: chprintf(&SD1, ...) generates type mismatch warning

Posted: Mon Oct 28, 2013 9:59 am
by Giovanni
It is worth considering but not in the current stable branch it would break too many things, it could go in version 3.0 where a lot of other things changed.

Giovanni