<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - -Wunused-value and ternary operator gives unexpected behavior"
   href="http://llvm.org/bugs/show_bug.cgi?id=17835">17835</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-Wunused-value and ternary operator gives unexpected behavior
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kaduk-llvm@mit.edu
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This is using a trunk snapshot from FreeBSD's lang/clang-devel port, it seems
to be LLVM svn r193887.  The behavior is slightly different in older versions
of clang, but still unexpected/confusing.

This program:
%%%%%%
int main(int argc, char **argv) {
    (1 < 2) ? 3 : 4;
    return 0;
}
%%%%%%
(correctly) gives a warning: expression result unused (-Wunused-value),
pointing to the '3'.  However, if the '<' is changed to '>', the warning still
points to the '3' (and not the '4' as one might expect, given that the
conditional expression is a constant).
It seems that C99 requires that the two branches of the ternary expression have
the same (or compatible) type, so "by the spec" it suffices to check the return
value of just one branch.  However, if I apply a '(void)' cast to either the
'3' or the '4', with either the '<' or '>' version of the test program, no
warning is generated, even with -Wall.  If the warning points to only the '3'
value, it is nonintuitive that a cast on the '4' branch will quiesce the
warning.

I'm not really sure what the best resolution to this situation is -- from
looking at <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Dead branch warning elimination doesn't trigger for global initialisation"
   href="show_bug.cgi?id=10030">bug 10030</a> it seems that maybe one should expect CFG analysis in this
scope, but I am unfamiliar with the details.
Another thing to consider would be a warning if the types of the two branches
of the ternary expression were incompatible, that would make the behavior with
(void) casts easier to understand.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>