[CDRIVER-3701] Calling topology TRACE macro with no formatted args emits compiler warning Created: 03/Jun/20  Updated: 28/Oct/23  Resolved: 04/Jun/20

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: None
Fix Version/s: 1.17.0-beta2, 1.17.0

Type: Bug Priority: Minor - P4
Reporter: Jeremy Mikola Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to PHPC-1632 Reduce default keepalive time to alig... Closed
is related to CDRIVER-3625 Multi-threaded scanning should scan s... Closed

 Description   

I observed the following build warning when compiling libmongoc for PHPC:

In file included from phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology.c:31:0:
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology.c: In function ‘mongoc_topology_rescan_srv’:
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-trace-private.h:39:19: warning: too many arguments for format [-Wformat-extra-args]
                   "TRACE: %s():%d " msg,  \
                   ^
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology.c:640:4: note: in expansion of macro ‘TRACE’
    TRACE ("Polling for SRV records", NULL);
    ^~~~~
In file included from phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology-description.c:21:0:
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology-description.c: In function ‘mongoc_topology_description_handle_ismaster’:
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-trace-private.h:39:19: warning: too many arguments for format [-Wformat-extra-args]
                   "TRACE: %s():%d " msg,  \
                   ^
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology-description.c:2001:10: note: in expansion of macro ‘TRACE’
          TRACE ("wrong set name", NULL);
          ^~~~~

This appears to have been introduced in 370fc18 for CDRIVER-3625 and pertains to how TRACE is defined in mongoc-trace-private.h as a proxy to mongoc_log. The macro's msg argument is always expected but subsequent arguments are taken with .... Removing the NULL argument resulted in an outright error:

 
In file included from phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology.c:31:0:
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology.c: In function ‘mongoc_topology_rescan_srv’:
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-trace-private.h:42:30: error: expected expression before ‘)’ token
                   __VA_ARGS__);           \
                              ^
phpc/src/libmongoc/src/libmongoc/src/mongoc/mongoc-topology.c:640:4: note: in expansion of macro ‘TRACE’
    TRACE ("Polling for SRV records");
    ^~~~~

If we can't fix this directly, perhaps it'd be best to simply suppress any -Wformat-extra-args warnings around the TRACE macro.



 Comments   
Comment by Githook User [ 04/Jun/20 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3701 fix TRACE macro warnings
Branch: r1.17
https://github.com/mongodb/mongo-c-driver/commit/7114f57f9914b7b97995123ac843d3eeeae3e45d

Comment by Githook User [ 04/Jun/20 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3701 fix TRACE macro warnings
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/ccb5bfa2de66b5f13305c8da9139ae3fab8c2174

Comment by Jeremy Mikola [ 04/Jun/20 ]

https://github.com/mongodb/mongo-c-driver/pull/629

Comment by Jeremy Mikola [ 03/Jun/20 ]

According to MSDN docs for Variadic Macros, Windows compilers will truncate the comma automatically. For GCC, there is a custom syntax (##) available to do the same, but I'm not sure how portable that is for other platforms.

The blog post Detect Empty Macro Arguments does a deep dive into what is probably a more portable way to detect this, but it may be simpler to just create a separate TRACE_NO_ARGS macro that omits the final ....

Generated at Wed Feb 07 21:18:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.