<html>
    <head>
      <base href="https://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 --- - Add dead store related warnings"
   href="https://llvm.org/bugs/show_bug.cgi?id=24506">24506</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Add dead store related warnings
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>dblaikie@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>chandlerc@gmail.com, dgregor@apple.com, llvm-bugs@lists.llvm.org, nlewycky@google.com, rtrieu@google.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>A while ago this bug came up:

void func(bool *b) {
  b = false;
}

Where the user forgot to dereference 'b' before assigning. This is now warned
about (-Wbool-conversion) in < C++11, and a hard error in >= C++11 (C++11 and
up, only the literal 0, not all integer zero literals, is a valid null pointer
- so '\0' is also caught)

But this bug could still be written with integers:

void func1(int *i) {
  i = 0;
}

One way to catch this would be to implement a warning for dead stores. Any
situation in which a variable is written to, but never read. (the zero was
written, and then 'i' went out of scope - thus never read)

This wouldn't catch another case that came up, though:

void func2(bool b) {
  b = false;
  f1(b);
  f2(b);
  ...
}

The author intended the boolean to be a reference and to communicate back to
the caller some state about the execution of f1, f2, etc...

A dead store warning, extended somewhat, could catch this - if we treat the
input parameter 'b' as a store itself, then it is a dead store if we
unconditionally write to 'b' before we ever read from it. (as is the case
func2)

It may be the case that a general dead store warning (even with the further
generalization for parameter 'stores') would be too noisy for existing
codebases, consider common idioms like:

f(x); ++x
f(x); ++x

etc. People copy/paste these, and they may intentionally leave the ++x on the
last line for consistency.

So to get a good warning we might need to blacklist certain store operations to
be acceptably dead - or whitelist those (like parameter stores) that would
never be acceptably dead.

GCC has a simpler form of such a warning which wouldn't require control flow
analysis, -Wunused-but-set-parameter. Just looks for the parameter to be
assigned to in the function, but never to be read from at all. This would catch
the basic cases shown at the start, but not func2 or anything more involved.
May be worth implementing if someone wants some coverage but doesn't want to
pay the compile time penalty for CFG-based warnings.</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>