[cfe-dev] [Analyzer] Incorrect behavior of implicit copy assignment

scott constable sdconsta at syr.edu
Wed Aug 12 09:49:42 PDT 2015


Hi All,

I am analyzing the following code:

struct S1 {
S1() {}
S1(int x, int y, int z) : a(x), b(y), c(z) {}
int a;
int b;
int c;
};

void foo() {
S1 s2;
S1 s1(3, 0, 4);
s2 = s1;
}

with three callbacks: checkPostStmt, checkPreCall, and checkEndFunction. I
would expect the general order of analysis to go something like this:

PreCall(S1 default constructor)
EndFunction(S1 default constructor)
PreCall(S1 constructor)
Evaluate(S1 constructor)
EndFunction(S1 constructor)
PreCall(S1 copy assignment)
Evaluate(S1 copy assignment)
EndFunction(S1 copy assignment)
EndFunction(foo)

where Evaluate() is shorthand for a series of PostStmt's. If I write out an
explicit copy assignment operator:

S1 operator=(const S1 &s) {
        a = s.a; b = s.b; c = s.c;
}

then the analysis runs in the expected sequence. But when the copy
assignment operator is left implicit, I observe the following sequence:

PreCall(S1 default constructor)
EndFunction(S1 default constructor)
PreCall(S1 constructor)
Evaluate(S1 constructor)
EndFunction(S1 constructor)
PreCall(S1 copy assignment)
EndFunction(foo)
Evaluate(S1 copy assignment)
EndFunction(S1 copy assignment)

So the implicit copy assignment operator is actually being evaluated AFTER
foo() terminates. This in turn throws off my analyses.

~Scott Constable
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150812/d1a6c6c9/attachment.html>


More information about the cfe-dev mailing list