Building with libc++ and gcc: -Wmaybe-uninitalized

82 views Asked by At

I am running a build with gcc and libc++ (llvm). Since update to debian bookworm I get:

/usr/lib/llvm-14/include/c++/v1/regex:1384:8: warning: ‘<unnamed>.std::__1::__state<char>::__at_first_’ may be used uninitialized [-Wmaybe-uninitialized]

(along with a lot of other logs)

I have dumbed the problem down to this example:

example.cpp:

#include <regex>

int doIt()
{
   std::regex r("bla");
   return 0;
}

compiler call:

/usr/bin/g++ -Wmaybe-uninitialized -O1 -nostdinc++ -isystem /usr/lib/llvm-14/include/c++/v1/ -c example.cpp

Needed apt packages: g++, libc++-14-dev

When I remove the optimization (-O1), then the issue is gone. If I use all the separate optimizations from this list, the issue is gone. When i use the std::regex constructor with no args, the issue is gone.

Questions:

  1. Am I doing something wrong with compiler settings?
  2. Is this a bug? Is it a bug for gcc or libc++?
  3. How can I fix this without adding #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" to all includes of <regex>?
1

There are 1 answers

3
theSparky On

Using WSL2 Debian Bookworm, confirmed the same issue.

Seems to be a minor issue of __at_first_ not being initialized in LLVM at

https://github.com/llvm/llvm-project/blob/17fc78e7a4e178482650f2a1c18954823c5cd4eb/libcxx/include/regex#L1462

because

~$> sudo sed -i 's/          __node_(nullptr), __flags_() {}/          __node_(nullptr), __flags_(),__at_first_(false) {}/' /usr/lib/llvm-14/include/c++/v1/regex

allowed the build command you used to compile as expected:

~$> /usr/bin/g++ -Wmaybe-uninitialized -O1 -nostdinc++ -isystem /usr/lib/llvm-14/include/c++/v1/ -c example.cpp
~$>

However, for me, linking was only possible with:

~$> /usr/bin/g++ -Wmaybe-uninitialized -O1 example.cpp
~$> ./a.out
Text contains the phrase 'regular expressions'
Found 20 words
...