bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pacify -Wmissing-variable-declarations in unit tests.


From: Bruno Haible
Subject: Re: Pacify -Wmissing-variable-declarations in unit tests.
Date: Sun, 28 Apr 2024 12:11:41 +0200

Hi Collin,

> Can I apply the attached patch which adds the 'static' specifier to
> global variables in unit tests?

No! While adding 'static' would be no harm in some tests (such as
tests/jit/test-cache.c, tests/test-argp-version-etc.c, tests/test-argp.c),
in other tests the purpose of the global variables is to defeat compiler
optimizations.
Maybe in 10 years, everything will be compiled with '-flto' and thus
compiler optimizations on global variables will be the same as on 'static'
variables. But we're not there yet, and thus it helps us avoid compiler
optimizations that would make the unit test a no-op.

(You just discovered how tedious it is to investigate a compiler's
behaviour that causes a test failure. A compiler optimization that turns
a unit test into a no-op is even worse: You then notice, by chance, that
the unit test has not been effective for two years or so...)

> When building Coreutils I noticed that GCC 14's
> -Wmissing-variable-declarations option spams the test output.

There are two ways to deal with this:

a) Listen to the message in the banner

## ---------------------------------------------------- ##
## ------------------- Gnulib tests ------------------- ##
## You can ignore compiler warnings in this directory.  ##
## ---------------------------------------------------- ##

b) Exploit the existing GL_CFLAG_GNULIB_WARNINGS mechanism to annihilate
this type of warning.

Recall that [1]
  Many GCC warning options usually don’t point to mistakes in the code;
  these warnings enforce a certain programming style.

> I'm used to marking things static even in small single file programs

Yup, this is a programming style.
  - Other developers do it differently.
  - In many test cases, this programming style invites the compiler
    to cause trouble.

Therefore it's not the programming style that we use in the tests.

Bruno

[1] https://www.gnu.org/software/gnulib/manual/html_node/manywarnings.html







reply via email to

[Prev in Thread] Current Thread [Next in Thread]