<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/111092>111092</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            -Wunreachable-code triggers undesirable warnings when log-streaming into a sometimes-disabled logging macro
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          pbos
      </td>
    </tr>
</table>

<pre>
    In Chromium / abseil we have some logging macros that are sometimes compiled as no-ops. This sort of boils to a more complicated version of this (which I wanted to simplify our implementation to):

```
#include <iostream>

#define LAZY_STREAM(stream, condition) \
  switch (0) \
  case 0:                              \
 default:                             \
    if (!(condition)) \
      ;                                \
    else \
      (stream)

#define ENABLE_LOGGING 0
#define MAYBE_LOG() LAZY_STREAM(std::cout, ENABLE_LOGGING)

int main() {
  MAYBE_LOG();
  MAYBE_LOG() << "hello";  // error: code will never be executed [-Werror,-Wunreachable-code]
}
```

Here when `ENABLE_LOGGING` is false `MAYBE_LOG()` gets correctly compiled into nothing. `MAYBE_LOG() << "hello";` however triggers dead-code warnings for the `<< "hello";` bit when logging is disabled at compile time.

https://godbolt.org/z/f9EPMordr

No idea what heuristic upgrades would make sense here. Special casing `<<` makes sense to me, as may exonerating binary operators with a macro LHS (the dead-codiness comes from inside the macro). Unsure about whether `MAYBE_LOG(), foo;` should warn on dead foo or not.

chromium and absl currently spells this differently and gets away with streaming logs to dead code:

```
#define LAZY_STREAM(stream, condition)                                  \
 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
```

This seems to be sufficient to confuse clang enough to not issue dead-code warnings to `(stream)` even though it's part of the compile-time-known dead branch of the ternary.

Changing our implementation of LAZY_STREAM to the former results in dead-code warnings ~everywhere in chromium. Ideally we shouldn't make it harder for the compiler to deduce that this is indeed dead code, but that it should be OK since it originates from a macro (or some other heuristic that lets us do this). crrev.com/c/5905021
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVk1v2zgT_jX0ZWBDpvx58CFx4jZ4k_bFtrtF91JQ4kjiluIYJGXXe9jfvhjKrhPHbbGG4UTizHA-nuchVQimdogrMb0V07uB6mJDfrUtKAwK0ofVg4N146k1XQtCbkAVAY2FPUKjdgiBWgRLdW1cDa0qPQWIjYqgfL8YTYsBSmq3xqIGFcDRkLZhBB8bEyCQj0AVFGRsgEigoCWPycGaUkXUsEMfDDk2i-wj5GLfmLKBB9grxxaRIBh2qA5AnQf-H1t0UUV2jCTkUuQ3IrsT2el3lh2__aPMjSttpxFEvjYUokfVivz-hY_MNVbGITze_Pn5y4ePv93fPAm5OBrLNZTktOE9hVyCmK57P4CwN7FsOPPsYqVUASET-Q389HP20FipzsZfeTzbAsBUvLWQYyEXz1O8yCX55bc_z-QiNNqAr2KcW7K83r_7dze3j_dfHt-_efPw7g1kl-tPN59v03JKe_mq35qnmd-U1EVu-8twF7saF6FVxh1DifntKdeLXUT-oxXGhMjXIKRs0FoSUqY-CblhTqD35HkgJWmEvbEWHO7QQ4GA37DsGKNiejv81FvK9fBT5zyqslGFxSG7iendMen53XWEpt-36BH2DToQs-yi7FkGJkCl0kRm2WV1swxqjMxF77GM9nBmpXGRwFFsjKtHV3yv188RG9qnSqM3dY0-gEalh30flHfG1QEq8hCblNMP4xQm9mWdtMQE0CZwezSoeEoVWE5GzxvSxLgNjIY0ipp0QTaOyNdCbv4WclMt7___RF77507vCIxGBXtWqgY7b0I0JXTb2iuNAfbUWQ2t-ooQ0AWEBj2O4MMWS6Msc5ZT_F4PF8DG4WgdCVpkXKoArToAfiOHXkV2KoxT_gC05RfkA-xNbFj1WDvh8e0HZg9369RI4zAkAcUAlacWjAtGY-pochJyOYLfXeg8giqoS42MDfprIJBrqIiOTQ9NqpMHBeTSjrwK5BkML9pcns4A5TSfARbKznt0DKOwRcvi3aSZVRUe37NpQpzaq0NfZy8L3AdLddL7tGkiwK_0-b9I7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