<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Dec 22, 2011, at 6:02 AM, Xu Zhongxing wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">Hi Ted,<br><br>What do you mean by "does not properly model C++ destructors at all"?<br>Could you give me a test case? Thanks.</span></blockquote></div><br><div>Certainly. Consider:</div><div><br></div><div><div>$ cat /tmp/test.cpp</div><div>int foo();</div><div>int bar();</div><div><br></div><div>class A {</div><div>public:</div><div> A(int x);</div><div> ~A();</div><div> operator bool();</div><div>};</div><div><br></div><div>class B {</div><div>public:</div><div> B(int x);</div><div> ~B();</div><div> operator bool();</div><div>};</div><div><br></div><div>class C {</div><div>public:</div><div> C(int x);</div><div> ~C();</div><div> operator bool();</div><div>};</div><div><br></div><div>int test(int x, int y) {</div><div> if (C b = A(x) || B(y))</div><div> return foo();</div><div> return bar();</div><div>}</div></div><div><br></div><div><br></div><div>Let's look at the CFG without destructors:</div><div><br></div><div>$ clang -cc1 -analyze -analyzer-checker=debug.DumpCFG /tmp/test.cpp</div><div> [B6 (ENTRY)]</div><div> Succs (1): B4</div><div><br></div><div> [B1]</div><div> 1: bar</div><div> 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void))</div><div> 3: [B1.2]()</div><div> 4: return [B1.3];</div><div> Preds (1): B3</div><div> Succs (1): B0</div><div><br></div><div> [B2]</div><div> 1: foo</div><div> 2: [B2.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void))</div><div> 3: [B2.2]()</div><div> 4: return [B2.3];</div><div> Preds (1): B3</div><div> Succs (1): B0</div><div><br></div><div> [B3]</div><div> 1: [B4.8] || [B5.8]</div><div> 2: [B3.1] (ImplicitCastExpr, IntegralCast, int)</div><div> 3: [B3.2] (CXXConstructExpr, class C)</div><div> 4: [B3.3] (ImplicitCastExpr, ConstructorConversion, class C)</div><div> 5: [B3.4] (BindTemporary)</div><div> 6: [B3.5] (ImplicitCastExpr, NoOp, const class C)</div><div> 7: [B3.6]</div><div> 8: C b = A(x).operator _Bool() || B(y).operator _Bool();</div><div> 9: b</div><div> 10: [B3.9].operator _Bool</div><div> 11: [B3.10]()</div><div> 12: [B3.11] (ImplicitCastExpr, UserDefinedConversion, _Bool)</div><div> T: if [B3.12]</div><div> Preds (2): B5 B4</div><div> Succs (2): B2 B1</div><div><br></div><div> [B4]</div><div> 1: x</div><div> 2: [B4.1] (ImplicitCastExpr, LValueToRValue, int)</div><div> 3: [B4.2] (CXXConstructExpr, class A)</div><div> 4: [B4.3] (BindTemporary)</div><div> 5: A([B4.4]) (CXXFunctionalCastExpr, ConstructorConversion, class A)</div><div> 6: [B4.5].operator _Bool</div><div> 7: [B4.6]()</div><div> 8: [B4.7] (ImplicitCastExpr, UserDefinedConversion, _Bool)</div><div> T: [B4.8] || ...</div><div> Preds (1): B6</div><div> Succs (2): B3 B5</div><div><br></div><div> [B5]</div><div> 1: y</div><div> 2: [B5.1] (ImplicitCastExpr, LValueToRValue, int)</div><div> 3: [B5.2] (CXXConstructExpr, class B)</div><div> 4: [B5.3] (BindTemporary)</div><div> 5: B([B5.4]) (CXXFunctionalCastExpr, ConstructorConversion, class B)</div><div> 6: [B5.5].operator _Bool</div><div> 7: [B5.6]()</div><div> 8: [B5.7] (ImplicitCastExpr, UserDefinedConversion, _Bool)</div><div> Preds (1): B4</div><div> Succs (1): B3</div><div><br></div><div> [B0 (EXIT)]</div><div> Preds (2): B1 B2</div><div><br></div><div><br></div><div>This CFG behaves as expected. We have a single block with the '||' operation as a terminator (block 4), and a single block that merges the values from the LHS and RHS expressions (block B3) which is then terminated with the 'if' branch terminator. My motivation for looking at this CFG was to further optimize this so that block B5 was seen as dominating block B2. This would improve the quality of some flow-sensitive (but path-insensitive) analyses in the frontend and also simplify the CFG.</div><div><br></div><div>The CFG gets significantly more complicated (and wrong) when we add implicit destructors:</div><div><br></div><div><div>$ clang -cc1 -analyze -cfg-add-implicit-dtors -analyzer-checker=debug.DumpCFG /tmp/test.cpp</div><div><br></div><div> [B8 (ENTRY)]</div><div> Succs (1): B6</div><div><br></div><div> [B1]</div><div> 1: [B3.2].~C() (Implicit destructor)</div><div> 2: bar</div><div> 3: [B1.2] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void))</div><div> 4: [B1.3]()</div><div> 5: return [B1.4];</div><div> Preds (1): B3</div><div> Succs (1): B0</div><div><br></div><div> [B2]</div><div> 1: foo</div><div> 2: [B2.1] (ImplicitCastExpr, FunctionToPointerDecay, int (*)(void))</div><div> 3: [B2.2]()</div><div> 4: return [B2.3];</div><div> 5: [B3.2].~C() (Implicit destructor)</div><div> Preds (1): B3</div><div> Succs (1): B0</div><div><br></div><div> [B3]</div><div> 1: ~A() (Temporary object destructor)</div><div> 2: C b = A(x).operator _Bool() || B(y).operator _Bool();</div><div> 3: b</div><div> 4: [B3.3].operator _Bool</div><div> 5: [B3.4]()</div><div> 6: [B3.5] (ImplicitCastExpr, UserDefinedConversion, _Bool)</div><div> T: if [B3.6]</div><div> Preds (2): B4 B5</div><div> Succs (2): B2 B1</div><div><br></div><div> [B4]</div><div> 1: ~B() (Temporary object destructor)</div><div> Preds (1): B5</div><div> Succs (1): B3</div><div><br></div><div> [B5]</div><div> 1: [B6.8] || [B7.8]</div><div> 2: [B5.1] (ImplicitCastExpr, IntegralCast, int)</div><div> 3: [B5.2] (CXXConstructExpr, class C)</div><div> 4: [B5.3] (ImplicitCastExpr, ConstructorConversion, class C)</div><div> 5: [B5.4] (BindTemporary)</div><div> 6: [B5.5] (ImplicitCastExpr, NoOp, const class C)</div><div> 7: [B5.6]</div><div> 8: ~C() (Temporary object destructor)</div><div> T: [B6.8] || ...</div><div> Preds (2): B7 B6</div><div> Succs (2): B3 B4</div><div><br></div><div> [B6]</div><div> 1: x</div><div> 2: [B6.1] (ImplicitCastExpr, LValueToRValue, int)</div><div> 3: [B6.2] (CXXConstructExpr, class A)</div><div> 4: [B6.3] (BindTemporary)</div><div> 5: A([B6.4]) (CXXFunctionalCastExpr, ConstructorConversion, class A)</div><div> 6: [B6.5].operator _Bool</div><div> 7: [B6.6]()</div><div> 8: [B6.7] (ImplicitCastExpr, UserDefinedConversion, _Bool)</div><div> T: [B6.8] || ...</div><div> Preds (1): B8</div><div> Succs (2): B5 B7</div><div><br></div><div> [B7]</div><div> 1: y</div><div> 2: [B7.1] (ImplicitCastExpr, LValueToRValue, int)</div><div> 3: [B7.2] (CXXConstructExpr, class B)</div><div> 4: [B7.3] (BindTemporary)</div><div> 5: B([B7.4]) (CXXFunctionalCastExpr, ConstructorConversion, class B)</div><div> 6: [B7.5].operator _Bool</div><div> 7: [B7.6]()</div><div> 8: [B7.7] (ImplicitCastExpr, UserDefinedConversion, _Bool)</div><div> Preds (1): B6</div><div> Succs (1): B5</div><div><br></div><div> [B0 (EXIT)]</div><div> Preds (2): B1 B2</div><div><br></div><div><br></div><div>Notice that blocks B6 and B5 have the same terminator, which itself is suspect. The rationale is that the temporary for 'B(y)' should only be destructed when we evaluated the RHS of the logical operation. This is good enough for the static analyzer, but the CFG itself doesn't explicitly capture this dependency. This can be seen by observing that there is a path in the CFG from the entry block to the exit block where destructor ~B is called but the constructor B() is NOT called. While the static analyzer will likely get this right and recover that information (since it does enough value flow analysis to understand control-dependent branches), this unnecessarily drops a bunch of control-dependencies from the CFG which will degrade the quality of CFG-based analyses in the frontend.</div><div><br></div><div>The second, and more glaring problem, is that ~C is called twice along a path, both as an implicit destructor (in B1 and B2 respectively) and a destructor for a temporary (in B5). I may be missing something here, but this doesn't show up in the LLVM IR (which clearly shows a single destructor call to ~C).</div><div><br></div><div>Also, look at where the destructors are called. In block B1, ~C is correctly called before the call to bar(), but in block B2, the destructor appears after the return statement. Instead, it should appear immediately after the call to foo().</div></div></body></html>