<div dir="ltr">David took a look at this. If you could file a PR for it, that'd be great.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 9, 2015 at 9:38 AM, chen xu <span dir="ltr"><<a href="mailto:chenxuz@outlook.com" target="_blank">chenxuz@outlook.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><div dir="ltr">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.<br> <br><font face="Courier New,sans-serif">00  #include <stdio.h></font><br><font face="Courier New,sans-serif">01  class Test1 {</font><br><font face="Courier New,sans-serif">02  private:<br>03      int x_;<br>04  public:<br>05      Test1(int x) : x_(x) {}<br>06      __declspec(property(get=get_x)) int X;<br>07      int get_x() const { return x_; }</font><br><font face="Courier New,sans-serif">08      static Test1* GetTest1() { printf("create, "); return new Test1(10); }</font><br><font face="Courier New,sans-serif">09  };</font><br><font face="Courier New,sans-serif">10  int main(int argc, char** argv) {<br>11      int x = Test1::GetTest1()->X;<br>12      printf("x=%d\n",x);<br>13  }</font><br> <br>The execution output is like: <br>create, create, x=10<br> <br>Basically, the compiler builds the code line 11 into<br><font color="#555555" face="Consolas" size="2"><font color="#555555" face="Consolas" size="2"><font color="#555555" face="Consolas" size="2"><font face="Courier New,sans-serif">0x000000000040077f  call 0x4007c0 <Test1::GetTest1()> </font><br><font face="Courier New,sans-serif">
0x0000000000400784  mov QWORD PTR [rbp-0x20],rax </font><br><font face="Courier New,sans-serif">
0x0000000000400788  call 0x4007c0 <Test1::GetTest1()> </font><br><font face="Courier New,sans-serif">
0x000000000040078d  mov rdi,rax </font><br><font face="Courier New,sans-serif">
0x0000000000400790  call 0x400830 <Test1::get_x() const> </font><br><font face="Courier New,sans-serif">
0x0000000000400795  movabs rdi,0x4008e4 </font><br><font face="Courier New,sans-serif">
0x000000000040079f  mov DWORD PTR [rbp-0x14],eax </font><br></font> <br></font></font>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. <br> <br>                                           </div></div>
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>