Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Base class init error #22403

Open Quuxplusone opened 9 years ago

Quuxplusone commented 9 years ago
Bugzilla Link PR22404
Status REOPENED
Importance P normal
Reported by __vic (victor.dyachenko@protonmail.com)
Reported on 2015-01-30 07:42:31 -0800
Last modified on 2015-02-03 12:30:01 -0800
Version 3.5
Hardware PC Linux
CC anton@korobeynikov.info, dblaikie@gmail.com, dgregor@apple.com, llvm-bugs@lists.llvm.org, rnk@google.com, sepavloff@gmail.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Clang 3.5.1 fails to compile this code:

#include<sys/stat.h>

struct file_stat : public ::stat
{
    file_stat(const struct ::stat &s) : ::stat(s) {}
};

int main()
{
    struct ::stat s;
    file_stat s2(s);
}

with error:

clang-bug.cpp:5:43: error: member initializer 'stat' does not name a non-static
data member or base class
    file_stat(const struct ::stat &s) : ::stat(s) {}
                                          ^~~~~~~

GCC accepts this code silently. This workaround works for Clang:

struct file_stat : public ::stat
{
    typedef struct ::stat base;
    file_stat(const struct ::stat &s) : base(s) {}
};
Quuxplusone commented 9 years ago
Seems to work with ToT.

$ cat deriv.cpp
struct stat {
};
struct file_stat : public ::stat
{
    file_stat(const struct ::stat &s) : ::stat(s) {}
};

int main()
{
    struct ::stat s;
    file_stat s2(s);
}
blaikie@blaikie-linux:/tmp/dbginfo$ clang++-tot deriv.cpp -fsyntax-only
blaikie@blaikie-linux:/tmp/dbginfo$
Quuxplusone commented 9 years ago
What is "clang++-tot"? Is it bug in 3.5? In which version was it fixed?

What about code like this?

struct stat {
};
void stat() {}

struct file_stat : public ::stat
{
    file_stat(const struct ::stat &s) : ::stat(s) {}
};

int main()
{
    struct ::stat s;
    file_stat s2(s);
}
Quuxplusone commented 9 years ago
ToT means Top Of Trunk.
The second example indeed does not compile with ToT:

$ clang --version
clang version 3.7.0 (trunk 227734)
Target: x86_64-unknown-linux-gnu
Thread model: posix
$ clang ngs.cpp
ngs.cpp:7:43: error: member initializer 'stat' does not name a non-static data
member or base class
    file_stat(const struct ::stat &s) : ::stat(s) {}
                                          ^~~~~~~
1 error generated.

The message looks invalid.
Quuxplusone commented 9 years ago
A reduced example:

$ cat t.cpp
int stat(const char *path, struct stat *buf);
struct stat {};
struct file_stat : public ::stat {
  file_stat(const struct ::stat &s) : ::stat(s) {}
};
int main() {
  struct ::stat s;
  file_stat s2(s);
}

$ clang -cc1 t.cpp
t.cpp:4:41: error: member initializer 'stat' does not name a non-static data
member or base class
  file_stat(const struct ::stat &s) : ::stat(s) {}
                                        ^~~~~~~

Lookup for ::stat finds the function, not the struct, and the elaborated type
"struct ::stat" is not legal in an initializer list. The typedef workaround
makes sense.