[cfe-dev] Clang issue with __declspec(property)
chen xu
chenxuz at outlook.com
Thu Jul 9 09:38:41 PDT 2015
When using clang (3.6.0, on Ubuntu 15.04) to build a C++ class with __declspec(property), we observed a strange behavior, please see sample code below.
00 #include <stdio.h>
01 class Test1 {
02 private:
03 int x_;
04 public:
05 Test1(int x) : x_(x) {}
06 __declspec(property(get=get_x)) int X;
07 int get_x() const { return x_; }
08 static Test1* GetTest1() { printf("create, "); return new Test1(10); }
09 };
10 int main(int argc, char** argv) {
11 int x = Test1::GetTest1()->X;
12 printf("x=%d\n",x);
13 }
The execution output is like:
create, create, x=10
Basically, the compiler builds the code line 11 into
0x000000000040077f call 0x4007c0 <Test1::GetTest1()>
0x0000000000400784 mov QWORD PTR [rbp-0x20],rax
0x0000000000400788 call 0x4007c0 <Test1::GetTest1()>
0x000000000040078d mov rdi,rax
0x0000000000400790 call 0x400830 <Test1::get_x() const>
0x0000000000400795 movabs rdi,0x4008e4
0x000000000040079f mov DWORD PTR [rbp-0x14],eax
As we notice, Test1::GetTest1() is called twice. The result of the 1st call is stored into a compiler-allocated stack variable, and seems never used later; The result of the 2nd call is passed as "this" pointer to Test1::get_x(). Is this an expected behavior? Since our real GetTest1() is not idempotent, being called twice is causing incorrect program behavior. Could any folk working on __declspec(property) feature of clang take a look? Thanks a lot in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150709/68285ba1/attachment.html>
More information about the cfe-dev
mailing list