LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 24132 - __declspec(property): double invocations of foo() when compiling foo()->propertyName
Summary: __declspec(property): double invocations of foo() when compiling foo()->prope...
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: -New Bugs (show other bugs)
Version: 3.6
Hardware: PC Windows NT
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-15 12:05 PDT by chenxu
Modified: 2015-11-25 08:45 PST (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description chenxu 2015-07-15 12:05:31 PDT
Code for repro is as follows. 

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.
Comment 1 Alexey Bataev 2015-10-13 23:13:48 PDT
Fixed in 250265.