<div dir="ltr">And one more question: what do you think that the impact would be of submitting this change without fixing the logical operator liveliness problem? Would that only cause noreturn destructor-based results to be sometimes broken (better than the current always broken behavior), or do you think this change is likely to introduce regressions in other areas?</div>

<div class="gmail_extra"><br clear="all"><div><span style="color:rgb(153,153,153)">-Alex</span><br></div>
<br><br><div class="gmail_quote">On Mon, Mar 24, 2014 at 11:29 AM, Alex McCarthy <span dir="ltr"><<a href="mailto:alexmc@google.com" target="_blank">alexmc@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr">That's very helpful, thanks Jordan!<div><br></div><div><span>Pavel's attempt to fix the liveliness problems was <a href="http://llvm-reviews.chandlerc.com/rL189090" target="_blank">http://llvm-reviews.chandlerc.com/rL189090</a>, correct? I'll give that a more thorough read.</span></div>


<div><span><br></span></div><div><span>I take it that from the "sometimes-y-ness" of the bug, we don't have any better regression test coverage than what's already in temporaries.cpp? In particular, we don't have any test cases covering the looping misbehavior you described?</span></div>


<div><span><br></span></div><div><span>Would you mind speeding up my search by pointing me at 1) the IRGen code that does this sort of variable synthesis 2) similar code in the clang analyzer that does any sort of variable synthesis? (I think Ted said we do something similar to track which objects have been constructed so we know which destructors need to be called)</span></div>


<div><span><br></span></div><div><span>Thanks again for your help!</span></div></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888"><br clear="all"><div><span style="color:rgb(153,153,153)">-Alex</span><br>

</div></font></span><div><div class="h5">
<br><br><div class="gmail_quote">On Mon, Mar 24, 2014 at 10:15 AM, Manuel Klimek <span dir="ltr"><<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div>On Mon, Mar 24, 2014 at 6:09 PM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div><div>On Mar 24, 2014, at 9:29 , Alex McCarthy <<a href="mailto:alexmc@google.com" target="_blank">alexmc@google.com</a>> wrote:</div>



<br><blockquote type="cite"><div dir="ltr"><span>I tried to debug this a bit more last week, and I'm still stumped. Ted, Jordan or Anna, do you have time to take a deeper look at this? Anna, would you mind clarifying <a href="http://llvm.org/bugs/show_bug.cgi?id=18159" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=18159</a> (details below)?</span><div>





<br></div><div>Here are two test cases similar to the ones I added to temp-obj-dtors-cfg-output.cpp and temporaries.cpp: </div><div><br></div><div><div>  int testConsistencyNestedSimple2(bool value) {</div><div>    if (value) {</div>





<div>      if (!value || check(NoReturnDtor())) {</div><div>        return 1; // impossible</div><div>      }</div><div>    }</div><div>    return 0;</div><div>  }</div><div><br></div><div>  int testConsistencyNestedComplex2(bool value) {</div>





<div>    if (value) {</div><div>      if (!value || !value || check(NoReturnDtor())) {</div><div>        return 1; // impossible</div><div>      }</div><div>    }</div><div>    return 0;</div><div>  }</div></div><div><br>





</div><div>Since NoReturnDtor is an object with a temporary that never returns, it's impossible for either function to return 1. Before this patch, I think both test cases would fail or crash with destructor analysis turned on. This patch makes the simple case pass, but the compex case fails. I don't understand why.</div>



</div></blockquote><div><br></div></div><div>Take a look at the CFG for testConsistencyNestedComplex2:</div><div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



int testConsistencyNestedComplex2(bool value)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)"><b> [B10 (ENTRY)]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">



   Succs <span style="color:#4c2f2d">(1):</span> B9</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B1]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: 0</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   2: return [B1.1];</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(2):</span> B3 B9</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">



   Succs <span style="color:#4c2f2d">(1):</span> B0</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B2]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: 1</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   2: return [B2.1];</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(1):</span> B3</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">



   Succs <span style="color:#4c2f2d">(1):</span> B0</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B3]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,165,0);background-color:rgb(223,219,196)">   T: if [B5.1]</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">



   Preds <span style="color:#4c2f2d">(1):</span> B5</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">   Succs <span style="color:#4c2f2d">(2):</span> B2 B1</div>



<div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B4]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: ~NoReturnDtor() (Temporary object destructor)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">



   Preds <span style="color:#4c2f2d">(1):</span> B5</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">   Succs <span style="color:#4c2f2d">(1):</span> B0</div>



<div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B5]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: [B8.3] || [B7.3] || [B6.6]</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,165,0);background-color:rgb(223,219,196)">



   T: (Temp Dtor) [B8.3] || [B7.3] || ...</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(3):</span> B6 B7 B8</div>



<div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">   Succs <span style="color:#4c2f2d">(2):</span> B3 B4</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px">



<br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)"><b> [B6]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   1: check</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   2: [B6.1] (ImplicitCastExpr, FunctionToPointerDecay, _Bool (*)(struct NoReturnDtor &&))</div>



<div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   3: NoReturnDtor() (CXXConstructExpr, struct NoReturnDtor)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   4: [B6.3] (BindTemporary)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   5: [B6.4]</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   6: [B6.2]([B6.5])</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(1):</span> B7</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">



   Succs <span style="color:#4c2f2d">(1):</span> B5</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B7]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: value</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   2: [B7.1] (ImplicitCastExpr, LValueToRValue, _Bool)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   3: ![B7.2]</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,165,0);background-color:rgb(223,219,196)">



   T: [B8.3] || [B7.3] || ...</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(1):</span> B8</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">



   Succs <span style="color:#4c2f2d">(2):</span> B5 B6</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B8]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: value</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   2: [B8.1] (ImplicitCastExpr, LValueToRValue, _Bool)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   3: ![B8.2]</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,165,0);background-color:rgb(223,219,196)">



   T: [B8.3] || ...</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(1):</span> B9</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">



   Succs <span style="color:#4c2f2d">(2):</span> B5 B7</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B9]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">   1: value</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)">



   2: [B9.1] (ImplicitCastExpr, LValueToRValue, _Bool)</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,165,0);background-color:rgb(223,219,196)">   T: if [B9.2]</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">



   Preds <span style="color:#4c2f2d">(1):</span> B10</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(192,46,192);background-color:rgb(223,219,196)">   Succs <span style="color:#4c2f2d">(2):</span> B8 B1</div>



<div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196);min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(150,150,0);background-color:rgb(223,219,196)">



<b> [B0 (EXIT)]</b></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(67,41,212);background-color:rgb(223,219,196)">   Preds <span style="color:#4c2f2d">(3):</span> B1 B2 B4</div><div><br></div><div>


Notice block B4, where the temporary destructor is run. It's called when we get to B5 and see that the second-to-last || condition was false.</div>
<div><br></div><div>What gets us to B5? Every single part of the || chain, because it's the only way to get to the actual if in B3. But by the time we go there, we've lost the values of the source expressions, just like you said. IIRC, without temporary destructors this isn't important, because we can figure out which branch we came from based on the previous block edge. But now that's not going to work.</div>



<div><br></div><div>What Pavel tried is making all the values in the || chain stay live, so that we could decide whether or not to branch based on that, instead of based on how we got there. But that created a different problem: if we go directly from the first || condition to the final one, <i>we don't have the second one yet.</i> Worse, if we're in a loop, the liveness analysis (sometimes?) gets totally confused, and considers the value of the second expression from the <i>last</i> time through the loop to still be live. Which then does the wrong thing.</div>



<div><br></div><div>That's <a href="http://llvm.org/bugs/show_bug.cgi?id=18159" target="_blank">PR18159</a>.</div><div><br></div><div>(And that "sometimes" is killer. We had a terrible time trying to reduce the test case—various things would perturb it one way or another to make the problem appear or not.)</div>



<div><br></div><div>A while ago Ted mentioned that we should just synthesize shadow bool variables of some kind to track whether or not something's been initialized. I'm starting to think more and more that that's the right idea. (It's basically what IRGen does.) We don't exactly have an implementation for it, though.</div>



</div></div></div></blockquote><div><br></div></div></div><div>If I remember correctly that was also Chandler's favorite opinion from the start (just do whatever IRGen does).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><div>
<div style="word-wrap:break-word"><div><div><div>Anyway, I think that's what's not working in your test cases. I'll look through the patch to see if there are more pieces we can commit in advance. (The extraction of getRegionForConstructedObject seems like good general cleanup anyway.)</div>



<div><br></div><div>Jordan</div></div><div><br></div><br><blockquote type="cite"><div><div><div dir="ltr">

<div><br></div><div>I've attached dumps of clang analyzer's exploded graph state while processing these functions with my patch with destructor analysis turned on and off. (the diagrams in the pdfs are very tiny, you'll need something with serious zoom like the mac Preview app to read them). I think the bug *might* be caused by constraints being dropped when we analyze new blocks: in the Simple cases, we maintain a constraint on value being [1, 255] until we've completely finished processing the if statement and we've executed its terminator. In the complex case, we forget the constraints on value when transitioning between blocks before we've finished the if statement. I *think* this is when we enter the new block created to contain the temporary. But I'm still not sure how to read this, or whether this is a red herring.</div>





<div><br></div><div><span>If this is a problem where we're forgetting constraints on variables, this might be <a href="http://llvm.org/bugs/show_bug.cgi?id=18159" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=18159</a> which Anna filed in December. The bug says that this problem can be reproduced by dumping live variables and looking at the output: I've run these test cases with </span>debug.DumpLiveVars but I'm not sure what to look for. Anna, could you please add more detail to the bug, or potentially add some test cases that pass or fail if this bug is fixed or still present?</div>





<div><br></div><div>Thanks for your help, all. Let me know if you want to do some physical or virtual in-person debugging. I'm moving pretty slowly due to unfamiliarity with the tools, codebase, and compilers in general, and I'm sure a half hour of your experienced eyes on the problem would go a long way :)</div>





<div><br></div><div><br></div></div><div class="gmail_extra"><br clear="all"><div><span style="color:rgb(153,153,153)">-Alex</span><br></div>
<br><br><div class="gmail_quote">On Sat, Mar 22, 2014 at 5:33 PM, Alexander Kornienko <span dir="ltr"><<a href="mailto:alexfh@google.com" target="_blank">alexfh@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div dir="ltr">I'm waiting impatiently for this patch to land ;)<div>Any progress here? Jordan, AFAIU the ball is on your side?<div><div><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 6, 2014 at 8:47 PM, Alex McCarthy <span dir="ltr"><<a href="mailto:alexmc@google.com" target="_blank">alexmc@google.com</a>></span> wrote:<br>






<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks for taking a look, Jordan, I really appreciate the extra eyes. Please find an updated patch attached.<div>






<br></div><div>I misread the test in temporaries.cpp, thanks. I've restored the test so it's testing the right thing: it still fails and I'm currently stumped. I've produced some simpler test cases that are much easier to analyze, but I still don't understand how this incorrect behavior is triggered. I've also added more CFG dumps to temp-obj-dtors-cfg-output.cpp.test to see if anything stands out. I've also added a lot of debug output which clearly has to be removed before submission. I'd really appreciate any more advice you have here, and I'll keep (slowly) debugging in the mean time.</div>









<div><br></div><div>The array processing while loop I added to ExprEngineCxx.cpp's ::VisitCXXDestructor was an attempt to fix the crash that I found when processing initializer_lists of temporary objects. I added a new test to dtor-xx11.cpp to cover this case:<br>








<div><br></div><div>namespace Cxx11BraceInit {</div><div>  struct Foo {</div><div>    ~Foo() {}</div><div>  };</div><div><br></div><div>  void testInitializerList() {</div><div>    for (Foo foo : {Foo(), Foo()}) {}</div>








<div>  }</div><div>}</div></div><div><br></div><div>Without the array special casing, clang crashes while parsing this:</div><div>clang: third_party/llvm/llvm/tools/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp:322: void clang::ento::ExprEngine::VisitCXXDestructor(clang::QualType, const clang::ento::MemRegion *, const clang::Stmt *, bool, clang::ento::ExplodedNode *, clang::ento::ExplodedNodeSet &): Assertion `RecordDecl && "Only CXXRecordDecls destructors can be processed"' failed.<br>









</div><div><br></div><div>I've updated the special casing to be a bit simpler, but I think it (or something like it) is necessary to avoid crashing on initializer_lists. What do you think?</div><div><br></div><div>Your recommendation of skipping destructors in VisitCXXConstructExpr worked perfectly: I've updated getRegionForConstructedObject in ExprEngineCXX.cpp to recurse when it finds a destructor, and that fixed the spurious garbage return value warnings, which now means that this patch eliminates all large scale false positives on this project's codebase. Thanks for your help!</div>








<div><br></div><div>If there are any parts of this change that you'd like to split out and individually commit (like the dtor handling in PathDiagnostic.cpp) that sounds fine to me. I'm happy with whatever patching combination you'd prefer if you help me through the splitting and committing process.</div>









<div><br></div><div>Thanks again for your help with this!</div></div><div class="gmail_extra"><span><font color="#888888"><br clear="all"><div><span style="color:rgb(153,153,153)">-Alex</span><br></div></font></span><div>






<div>
<br><br><div class="gmail_quote">On Tue, Mar 4, 2014 at 7:45 PM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">








<div style="word-wrap:break-word"><div>Wow, thanks for working on this, Alex. Unfortunately I think there may be a few more problems than simply turning things back on. In particular, from test/Analysis/temporaries.cpp:</div>








<div><br></div><div><div>     if (compute(i == 5 &&</div><div>                 (i == 4 || i == 4 ||</div><div>                  compute(i == 5 && (i == 4 || check(NoReturnDtor()))))) ||</div><div>         i != 4) {</div>








<div>-      clang_analyzer_eval(true); // no warning, unreachable code</div><div>+      clang_analyzer_eval(true); // expected-warning{{TRUE}} (possible when i=6)</div><div>     }</div></div><div><br></div><div>i cannot equal 6 at this point in the code; testConsistencyNested has a line earlier that says "if (i != 5) return". So we're not getting the correct behavior here—either the destructor isn't ending up in the right place in the CFG, or something is invalidating the value of 'i' that shouldn't be. I would guess it's the former, since this series of tests were designed to check Pavel's reworking of the CFG.</div>








<div><br></div><div>This part confuses me:</div><div><br></div><div><div>+    while (const ArrayType *ArrayType = Ctx.getAsArrayType(ObjectType)) {</div><div>+      ObjectType = ArrayType->getElementType();</div><div>







+      VisitCXXDestructor(ObjectType, Dest, S, IsBaseDtor, Pred, Dst);</div>
<div>+    }</div></div><div><br></div><div>For a multidimensional array of, say, Foo, this visits "array", "array[0]", "array[0][0]", etc, down to the single element case. In addition, visiting "array[0]" will <i>also</i> trigger a destruction of "array[0][0]", etc., since this loop happens as the very first thing in VisitCXXDestructor.</div>








<div><br></div><div>I would just leave the FIXME as is here, and not worry about this.</div><div><br></div><div><div>   // FIXME: We need to run the same destructor on every element of the array.</div><div>   // This workaround will just run the first destructor (which will still</div>








<div>   // invalidate the entire array).</div></div><div><br></div><div>And then we have your new test case:</div><div><br></div><div><div>+  //TODO: figure out why this case causes an unexpected "Undefined or garbage value returned to caller" warning</div>








<div>+  bool testNamedCustomDestructor() {</div><div>+    if (CheckCustomDestructor c = CheckCustomDestructor())</div><div>+      return true;</div><div>+    return false;</div><div>+  }</div></div><div><br></div><div>First of all, nicely discovered. I'm not immediately sure what's wrong here, but let's take a look at the CFG:</div>








<div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(76,47,45);background-color:rgb(223,219,196)"><div style="margin:0px;color:rgb(150,150,0)"><b> [B3]</b></div><div style="margin:0px">   1: CheckCustomDestructor() (CXXConstructExpr, struct CheckCustomDestructor)</div>








<div style="margin:0px">   2: [B3.1] (BindTemporary)</div><div style="margin:0px">   3: [B3.2] (ImplicitCastExpr, NoOp, const struct CheckCustomDestructor)</div><div style="margin:0px">   4: [B3.3]</div><div style="margin:0px">








   5: [B3.4] (CXXConstructExpr, struct CheckCustomDestructor)</div><div style="margin:0px">   6: ~CheckCustomDestructor() (Temporary object destructor)</div><div style="margin:0px">   7: CheckCustomDestructor c = CheckCustomDestructor();</div>








</div></div><div><br></div><div>B3.1 is the actual creation of the temporary. B3.5 is the copy constructor required by the C++ standard to copy the temporary into 'c'. B3.6 is the destruction of the temporary, and B3.7 is the actual VarDecl for 'c'. (The block then goes on to call 'operator bool' on 'c' and then do the if-branch.)</div>








<div><br></div><div>The current handling of constructors for VarDecls is a bit hacky. If you look in ExprEngine::VisitCXXConstructExpr, you'll notice it tries to look ahead to the next CFG element to see if it's constructing a particular variable. If so, it sets the target region to that variable. The trouble is, there's now a destructor between B3.5 and B3.7, so my guess is that the analyzer has decided it's <i>not</i> constructing a variable. I am okay with cheating right now by skipping over destructor CFG elements in VisitCXXConstructExpr, but I haven't thought about if there's a better way to tell that a particular CXXConstructExpr goes with a particular VarDecl.</div>








<div><br></div><div>As far as this part goes...</div><div><br></div><div><div>-  case CFGElement::TemporaryDtor:</div><div>+  case CFGElement::TemporaryDtor: {</div><div>+    const CFGTemporaryDtor &Dtor = Source.castAs<CFGTemporaryDtor>();</div>








<div>+    return PathDiagnosticLocation(Dtor.getBindTemporaryExpr(), SM, CallerCtx);</div><div>+  }</div></div><div><br></div><div>...that seems like a good obvious change, and I'm happy to commit that (or for you to commit that) without turning anything else on. :-)</div>








<div><br></div><div>I know this is a lot to throw at you at once, but please continue to e-mail me with questions and progress. I'm very happy that someone is able to put time into this.</div><div><br></div><div>Jordan</div>








<div><br></div><br><div><div><div>On Mar 2, 2014, at 0:41 , Alex McCarthy <<a href="mailto:alexmc@google.com" target="_blank">alexmc@google.com</a>> wrote:</div><br></div><blockquote type="cite">

<div><div dir="ltr">Hi all,<div><br></div><div><span>I'm running clang's static analyzer on a C++ codebase at Google. I saw a roughly a 50% false positive rate which stemmed from the analyzer not recognizing temporary object destructors: this issue is discussed in some length in another thread, which mentions a similar error rate on the Chromium codebase: <a href="http://comments.gmane.org/gmane.comp.compilers.clang.devel/33901" target="_blank">http://comments.gmane.org/gmane.comp.compilers.clang.devel/33901</a></span></div>










<div><br></div><div><span>Starting from Pavel's work which was reverted in <a href="http://llvm-reviews.chandlerc.com/rL186925" target="_blank">http://llvm-reviews.chandlerc.com/rL186925</a> , I've put together a new patch (see attachment) that attempts to fix temporary object destructor handling.</span></div>










<div><span><br></span></div><div><span>This new patch fixes all of the new regression tests added after Pavel's change was reverted, notably including <a href="http://llvm-reviews.chandlerc.com/rL187133" target="_blank">http://llvm-reviews.chandlerc.com/rL187133</a> . I've also fixed some other crashes, including a crash when processing an array of temporary objects use in a C++11 initializer_list, covered by a new regression test in </span>cfe/test/Analysis/dtor-cxx11.cpp . And most importantly, running clang with this patch eliminates the 50% false positive rate I saw previously (from ~800 warnings to ~400 across the ~1700 file codebase).</div>










<div><span><br></span></div><div><span><br></span></div><div><span>Now for the bad news:</span></div><div><span><br></span></div><div><span>This patch introduces a new regression which wasn't covered by existing tests: named temporaries declared and used within if statements are considered dead while they're still being used, which results in "</span>Undefined or garbage value returned to caller" errors. I've added regression tests to test/Analysis/dtor.cpp to cover this case, which currently fail. I've also updated test/Analysis/temp-obj-dtors-cfg-output.cpp with relevant CFG dumps to try to debug the problem. This new false positive is much nosier than the ones this patch fixes: the only advantage to the current patch as-is is that the garbage return value warnings are emitted in a small collection of header files, making them much easier to ignore en masse.</div>










<div><br></div><div>I don't have any compiler experience, so I'm moving slowly in the clang codebase and could use some help understanding where to look next. I've mostly been handling each crash or error as I find it, but I don't have a high level understanding of the impact or context of my change. In particular, I don't know how to read the CFG dumps I've generated, so I'm not sure where things are going wrong. Ted, Jordan, and Anna: Manuel Klimek mentioned that you've looked into this issue at length. Do have any advice on what I'm doing wrong, or could you suggest other approaches I might be able to try? Anything you can think of that can speed up my search for a fix would be greatly appreciated.</div>










<div><br></div><div>If we can get this patch working, it should address the following issues:</div><div><a href="http://llvm.org/bugs/show_bug.cgi?id=15599" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=15599</a> </div>










<div><span><a href="http://llvm.org/bugs/show_bug.cgi?id=16664" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=16664</a></span></div><div><span></span><span><a href="http://llvm.org/bugs/show_bug.cgi?id=18159" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=18159</a> (not sure, this bug is referenced by a newly fixed test in </span>test/Analysis/temporaries.cpp)</div>










<div><br></div><div>Thanks for your help,<br clear="all"><div><span style="color:rgb(153,153,153)">-Alex McCarthy</span><br></div>
</div></div>
</div><span><temporary-destructors.patch></span></blockquote></div></div></blockquote></div></div></div></div></blockquote></div><br>
</div></div></div></div></div>
</blockquote></div><br></div>
</div></div><span><ClangSimpleNoDtors.pdf></span><span><ClangComplexNoDtors.pdf></span><span><ClangSimple.pdf></span><span><ClangComplex.pdf></span></blockquote></div><br></div><br></div></div>_______________________________________________<br>




cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>