[llvm-bugs] [Bug 38447] New: -Wuninitialized false positive when passing a value to a nameless argument

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Aug 4 20:11:57 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38447

            Bug ID: 38447
           Summary: -Wuninitialized false positive when passing a value to
                    a nameless argument
           Product: clang
           Version: 6.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: dcoughlin at apple.com
          Reporter: plroskin at gmail.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 20641
  --> https://bugs.llvm.org/attachment.cgi?id=20641&action=edit
Example source

I'm using a class that reads data into data members of other classes. To ensure
that the correct data is read into the field, the field is passed as an
argument to the data reader.

Only the type of the field is used. The original value of the field is not used
by the data reader. That should be clear to the compiler from the fact that the
data reader doesn't name its argument in its implementation, which is visible
to the complier.

Following is a greatly simplified implementation.


struct DataReader
{
    template <class T>
    T operator()(const T&) const
    {
      return read_data(T{});
    }

    int read_data(const int&) const
    {
        return 1;
    }

    long read_data(const long&) const
    {
        return 2;
    }
};

class MyData
{
    explicit MyData(const DataReader& data_reader):
        field_int{data_reader(field_int)},
        field_long{data_reader(field_long)}
    {}

    const int field_int;
    const long field_long;
};


clang 6.0.1 reports warnings:

example.cpp:23:31: warning: field 'field_int' is uninitialized when used here
[-Wuninitialized]
        field_int{data_reader(field_int)},
                              ^
example.cpp:24:32: warning: field 'field_long' is uninitialized when used here
[-Wuninitialized]
        field_long{data_reader(field_long)}
                               ^

No version of gcc (from 4.9 to 8.1) reports any warnings for that code. A
resent version of icc doesn't report any warnings either.

As a result, I have to disable -Wuninitialized for the clang build.

The code base has a large number of the DataReader users, and I really want to
keep the calls simple, i.e. no pragma in every file, no decltype in every file,
and no macros that would obscure the fact that I'm initializing data members.

The maybe_unused attribute doesn't help. I need to tell the compile that the
argument is definitely not used. But it should be clear already. Nameless
arguments cannot be used.

clang 3.8, 3.9, 4.0.1 and 5.0.2 suffer from that issue as well.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180805/27790002/attachment-0001.html>


More information about the llvm-bugs mailing list