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

            Bug ID: 50866
           Summary: wrong code for -O of attribute(const or pure) for
                    overriden virtual base class method
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected]

#include <iostream>
struct A {
  virtual int c() __attribute__ ((const));
  virtual int p() __attribute__ ((pure ));
};
struct B : public A {
  virtual int c() override;
  virtual int p() override;
};
int A::c() { std::cout << "A::c()\n"; return 0x01; }
int A::p() { std::cout << "A::p()\n"; return 0x04; }
int B::c() { std::cout << "B::c()\n"; return 0x10; }
int B::p() { std::cout << "B::p()\n"; return 0x40; }
int main() {
  B b;
  A &a(b);
  return a.c() + a.c() + a.p() + a.p(); // 160==0xa0==0x10+0x10+0x40+0x40
}
-------------------------------------------------------------------------
for i in g++ clang++;do $i -o virtone virtone.C -Wall -g -O;./virtone;echo
$?;done
-------------------------------------------------------------------------
PASS: gcc-11.1.1-3.fc34.x86_64
B::c()
B::c()
B::p()
B::p()
160
-------------------------------------------------------------------------
FAIL: clang-12.0.0-2.fc34.x86_64
FAIL: clang version 13.0.0 91053e327ccd27cb1ee66a7d4954d456ceeed5f6
      Target: x86_64-unknown-linux-gnu
B::c()
B::p()
160
-------------------------------------------------------------------------
It is discussed here but I see no conclusion there:
  https://stackoverflow.com/a/18394716/2995591
The GCC behavior looks more useful to me.
The clang behavior is dangerous. One must make any 'const' or 'pure' virtual
methods also 'final' to be safe for future. If they cannot be 'final' one must
not use 'const'/'pure' for the base class method even despite it could be
useful.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to