Bug 358697 - valgrind.h: Some code remains even when defining NVALGRIND
Summary: valgrind.h: Some code remains even when defining NVALGRIND
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: general (show other bugs)
Version: 3.12 SVN
Platform: Compiled Sources Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-01-28 19:36 UTC by Matthias Schwarzott
Modified: 2017-03-30 12:14 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments
valgrind-improve-unused-parameter-on-r15763.patch (2.32 KB, patch)
2016-01-28 19:38 UTC, Matthias Schwarzott
Details
valgrind-3.12.0-valgrind_printf-simple-nvalgrind-fix-plus-testcase.patch (1.99 KB, patch)
2017-03-30 08:04 UTC, Matthias Schwarzott
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matthias Schwarzott 2016-01-28 19:36:00 UTC
Since revision r15762 the function VALGRIND_PRINTF will create some code, even when NVALGRIND is defined.

This is caused by using code with volatile to silence variable unused warning.
VALGRIND_PRINTF(const char *format, ...)
{
#if defined(NVALGRIND)
   if (format) *(volatile const char *)format;   /* avoid compiler warning */
    return 0;
#else /* NVALGRIND */
...

I suggest to use either simpler code to silence the variable unused warning.
E.g. (void)format;

But as Florian wrote, this could trigger some static analyzers.

Alternatively use the gcc construct __attribute__((__unused__)) and only use the simple version if this is not supported by the compiler.
One possibility is to conditionally define __attribute__ for all other compilers, so its usage is without conditionals.
If you do not want to define __attribute__ for namespace pollution, there could be a valgrind specific name, maybe __vg__attribute__ that is defined the same way, and for gcc equals to __attribute__.


Reproducible: Always
Comment 1 Matthias Schwarzott 2016-01-28 19:38:12 UTC
Created attachment 96891 [details]
valgrind-improve-unused-parameter-on-r15763.patch

This patch implements the __attribute__ usage.
But I am not sure about the ifdef code. It is just what was originally around the __attribute__ usage.
Comment 2 Matthias Schwarzott 2016-01-28 19:51:11 UTC
The simplest solution could be to use "(void)format" and protect this with an ifdef checking that we are not running under the problematic static code checker.
Comment 3 Matthias Schwarzott 2016-01-29 21:18:12 UTC
e.g. QT has a macro like this:

#define Q_UNUSED(x) (void)x;

To be used inside functions:
Q_UNUSED(format)
Comment 4 Matthias Schwarzott 2017-03-30 08:04:37 UTC
Created attachment 104809 [details]
valgrind-3.12.0-valgrind_printf-simple-nvalgrind-fix-plus-testcase.patch

This new patch fixes the problem in the simplest way: "(void)format;"
Additionally it adds a testcase to compile and run the existing vgprintf.c with NVALGRIND
Comment 5 Matthias Schwarzott 2017-03-30 08:05:51 UTC
Patch valgrind-3.12.0-valgrind_printf-simple-nvalgrind-fix-plus-testcase.patch has been tested with MSVC 2008 configured to warning level 4.
Comment 6 Julian Seward 2017-03-30 12:14:49 UTC
Committed, r16290.  Thanks for the patch.