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

    <tr>
        <th>Summary</th>
        <td>
            Unrelated errors introduce additional runtime diagnostics by skipping CFG construction
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            clang:diagnostics
      </td>
    </tr>

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

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

<pre>
    Consider: https://gcc.godbolt.org/z/dq6q8xxhY
```
#include <string.h>
struct Foo {
    virtual ~Foo();
    int x;
};
#error
constexpr bool alwaysFalse = false;
void maywarn(Foo *p) {
    if constexpr (alwaysFalse) {
        memset(p, 0, sizeof(*p));
    }
    if (alwaysFalse) {
        memset(p, 0, sizeof(*p)); // warns
    }
}
```

This produces this -Wdynamic-class-memaccess warning:
```
<source>:15:16: warning: destination for this 'memset' call is a pointer to dynamic class 'Foo'; vtable pointer will be overwritten [-Wdynamic-class-memaccess]
        memset(p, 0, sizeof(*p));
        ~~~~~~ ^
<source>:15:16: note: explicitly cast the pointer to silence this warning
        memset(p, 0, sizeof(*p));
               ^
               (void*)
```

This goes away if you remove the `#error`.

This causes issues because this latent warning can be present in header files in a warning-clean codebase, and it only appears when developers later introduce an error. The extra warning can obscure the actual problem because, with -Werror, developers end up seeing two errors.

These two conditionals prevent Clang from doing CFG-based warnings in the presence of errors:
* https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/Sema.cpp#L2224
* https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/AnalysisBasedWarnings.cpp#L2227

To fix the bug, we need to modify those conditionals to either:
* Always construct a CFG for analysis even in the presence of errors
* Don't issue any diagnostics about possibly unreachable code in the presence of other errors

I've prototyped both changes, and they break a lot of Clang tests, and I'm filing this issue to track followup.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1Vk2T4jYQ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