[cfe-dev] [Analyzer] C++ Constructors Do Not Work

scott constable via cfe-dev cfe-dev at lists.llvm.org
Thu Sep 10 10:29:26 PDT 2015


Hi All,

It seems that the clang static analyzer does not correctly handle C++
constructors. For example, I have the following code:

struct S {
S(int x, int y, int z) {
a = x;
b = y;
c = z;
}
int a;
int b;
int c;
};

void objectCreate() {
S *newS = new S(12, 0, 15);
if (newS->b)
newS->c++;
else
newS->a++;
delete newS;
}

Since newS->b initializes to 0, the expression "newS->a++" should never
execute. However, the analyzer in fact generates two new states and
evaluates both branches. If I replace the newS->b condition with the
integer literal 0, then only the first branch is evaluated, as I would
expect. I dug into this further, and found that newS's constructor is
called AFTER objectCreate() has been evaluated. In other words, newS's
constructor is called after "delete newS". This is clearly the wrong
behavior.

This is not the first time I have observed C++ constructors being handled
incorrectly. I also posted several weeks ago with the same observation
regarding implicit copy constructors.

Any help would be so very appreciated, as this has been driving me crazy!

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


More information about the cfe-dev mailing list