r215118 - Model temporary destructors from logical operators with known values.

Manuel Klimek klimek at google.com
Thu Aug 7 09:05:52 PDT 2014


Author: klimek
Date: Thu Aug  7 11:05:51 2014
New Revision: 215118

URL: http://llvm.org/viewvc/llvm-project?rev=215118&view=rev
Log:
Model temporary destructors from logical operators with known values.

If the truth value of a LHS is known, we can build the knowledge whether
a temporary destructor is executed or not into the CFG. This is needed
by the return type analysis.

Modified:
    cfe/trunk/lib/Analysis/CFG.cpp
    cfe/trunk/test/SemaCXX/return-noreturn.cpp

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=215118&r1=215117&r2=215118&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Thu Aug  7 11:05:51 2014
@@ -3631,12 +3631,23 @@ CFGBlock *CFGBuilder::VisitBinaryOperato
     BinaryOperator *E, TempDtorContext &Context) {
   if (E->isLogicalOp()) {
     VisitForTemporaryDtors(E->getLHS(), false, Context);
-    // We do not know at CFG-construction time whether the right-hand-side was
-    // executed, thus we add a branch node that depends on the temporary
-    // constructor call.
+    TryResult LHSVal = tryEvaluateBool(E->getLHS());
+    bool RHSNotExecuted = (E->getOpcode() == BO_LAnd && LHSVal.isFalse()) ||
+                          (E->getOpcode() == BO_LOr && LHSVal.isTrue());
+    if (RHSNotExecuted) {
+      return Block;
+    }
+
     TempDtorContext RHSContext(/*IsConditional=*/true);
     VisitForTemporaryDtors(E->getRHS(), false, RHSContext);
-    InsertTempDtorDecisionBlock(RHSContext);
+
+    // If the LHS is known, and the RHS is not executed, we returned above.
+    // Thus, once we arrive here, and the LHS is known, we also know that the
+    // RHS was executed and can execute the RHS unconditionally (that is, we
+    // don't insert a decision block).
+    if (!LHSVal.isKnown())
+      InsertTempDtorDecisionBlock(RHSContext);
+
     return Block;
   }
 

Modified: cfe/trunk/test/SemaCXX/return-noreturn.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/return-noreturn.cpp?rev=215118&r1=215117&r2=215118&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/return-noreturn.cpp (original)
+++ cfe/trunk/test/SemaCXX/return-noreturn.cpp Thu Aug  7 11:05:51 2014
@@ -183,6 +183,22 @@ int testTernaryConditionalNoreturnFalseB
   value ? Return() : (NoReturn() || NoReturn());
 } // expected-warning {{control may reach end of non-void function}}
 
+int testStaticallyExecutedLogicalOrBranch() {
+  false || NoReturn();
+}
+
+int testStaticallyExecutedLogicalAndBranch() {
+  true && NoReturn();
+}
+
+int testStaticallySkippedLogicalOrBranch() {
+  true || NoReturn();
+} // expected-warning {{control reaches end of non-void function}}
+
+int testStaticallySkppedLogicalAndBranch() {
+  false && NoReturn();
+} // expected-warning {{control reaches end of non-void function}}
+
 
 #if __cplusplus >= 201103L
 namespace LambdaVsTemporaryDtor {





More information about the cfe-commits mailing list