darconeous / libnyoci

A flexible CoAP stack for embedded devices and computers. RFC7252 compatible.
Other
27 stars 10 forks source link

Defining NYOCI_SINGLETON breaks build #14

Open snej opened 6 years ago

snej commented 6 years ago

NYOCI_SINGLETON doesn't seem to be working; if I define it I get a compile error. There are also a large number (about 50?) of warnings, which harsh my mellow because I'm a zealot about -Werror.

In GCC when building for ESP32 I get one error and a warning:

libnyoci/nyoci-transaction.c: In function 'nyoci_internal_delete_transaction_':
libnyoci/nyoci-transaction.c:122:6: error: the address of 'gNyociInstance' will always evaluate as 'true' [-Werror=address]
  if (!nyoci_get_current_instance()) {
      ^
libnyoci/nyoci-transaction.c: In function 'nyoci_transaction_new_msg_id':
libnyoci/nyoci-transaction.c:198:12: error: 'self' undeclared (first use in this function)
   (void**)&self->transactions,
            ^

In Clang (Xcode) building for macOS, I get the above, and also many warnings of the form:

In file included from libnyoci/nyoci.c:43:
In file included from libnyoci/libnyoci.h:166:
In file included from plat-net/posix/nyoci-plat-net.h:82:
libnyoci/nyoci-plat-net-func.h:128:33: error: this function declaration is not a prototype [-Werror,-Wstrict-prototypes]
NYOCI_API_EXTERN nyoci_status_t nyoci_plat_process(nyoci_t self);
                                ^
libnyoci/nyoci-plat-net-func.h:44:53: note: expanded from macro 'nyoci_plat_process'
#define nyoci_plat_process(self)                nyoci_plat_process()

and a lesser number of corresponding no-prototype warnings like this:

libnyoci/nyoci.c:128:1: error: no previous prototype for function 'nyoci_init' [-Werror,-Wmissing-prototypes]
nyoci_init(nyoci_t self) {
^
In file included from libnyoci/nyoci.c:43:
libnyoci/libnyoci.h:132:27: note: expanded from macro 'nyoci_init'
#define nyoci_init(self)                nyoci_init()

The source of the problem is that a C prototype of a zero-arg function has to have (void), not just (). Silencing or ignoring these warnings would be dangerous because without prototypes there will be no parameter checking of calls to the affected functions!

darconeous commented 6 years ago

A while back, NYOCI_EMBEDDED was used to enable the singleton behavior, and I had it tested in Travis and my own build-in-docker script. However, I've since separated the behavior of NYOCI_EMBEDDED and NYOCI_SINGLETON and forgot to add it to the test suite. So I expect that there might be some code rot.

However, testing after adding --enable-singleton to my etc/build_in_docker.sh script, it seems to be working OK.

Given the way that NYOCI_SINGLETON works, the warning on line 122 of libnyoci/nyoci-transaction.c is somewhat unavoidable. I really wish there was a way to collapse that warning for specific expressions, but I could easily change that code to not trigger that warning.

The error on line 198 is because I'm not currently testing NYOCI_SINGLETON without also using NYOCI_EMBEDDED: If you uncomment NYOCI_SINGLETON_SELF_HOOK and move it within the #if NYOCI_TRANSACTIONS_USE_BTREE block then it should work fine. There may be other errors though.

The trick that NYOCI_SINGLETON uses to remove the first arguments of both the prototypes and calls only works if the () no-argument syntax is acceptable for a prototype. I don't think there is really a good way around it other than to not use NYOCI_SINGLETON or to turn off that warning.

snej commented 6 years ago

OK! I'll probably just not use singleton mode, then; I'm not targeting devices where squeezing four bytes out of a stack frame is important. (My ESP32 dev board has ~400KB of RAM.)

darconeous commented 6 years ago

That sounds entirely reasonable. The mode was largely for devices like the CC2530 with a very limited stack size (less than 256 bytes!).

That being said, on the drive in to work today I think I may have figured out a way to make it work without disabling those warnings. I'll give that some more thought.

In any case, I'll use this issue to track fixing the error you pointed out.