llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Unrelated errors introduce additional runtime diagnostics by skipping CFG construction

Open rnk opened this issue 4 years ago • 16 comments

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.

rnk avatar Oct 10 '22 23:10 rnk

@llvm/issue-subscribers-good-first-issue

llvmbot avatar Oct 10 '22 23:10 llvmbot

Why does -Wdynamic-class-memaccess even do a reachability analysis? This seems like a regular type-based warning, for which we typically don't check reachability. But there are lots of users of DiagRuntimeBehavior/DiagIfReachable, and potentially all of them are affected by this.

Always construct a CFG for analysis even in the presence of errors

Might be difficult. Especially if the error is in the same function, or affects the function in some way.

Don't issue any diagnostics about possibly unreachable code in the presence of other errors

That seems like the right approach to me. We do the same for -Wthread-safety-analysis, and probably other CFG-based warning.

aaronpuchert avatar Nov 01 '22 19:11 aaronpuchert

@rnk @aaronpuchert is this still open to work?

chaitanyav avatar Apr 20 '23 18:04 chaitanyav

@rnk @aaronpuchert is this still open to work?

Yes, it seems.

xgupta avatar May 06 '23 17:05 xgupta

@rnk @aaronpuchert Hello, is this issue open to work? It's my first time contributing to llvm!

youngsun4786 avatar May 17 '24 05:05 youngsun4786

I'm not aware of anyone working on this.

aaronpuchert avatar May 21 '24 23:05 aaronpuchert

Is this still open?

sonar3d avatar Jan 03 '26 10:01 sonar3d

Hi, I would love to work on this issue

shivam-dhir avatar Jan 22 '26 06:01 shivam-dhir

go ahead...

xgupta avatar Jan 22 '26 08:01 xgupta

I clicked the godbolt link to confirm this issue is still open and available. I think the original instructions are probably still relevant, although the line references have drifted. It's mostly a matter of tweaking those conditionals and iterating on the test suite failures and updating them.

rnk avatar Jan 22 '26 17:01 rnk

@shivam-dhir Are you still working on the issue? Thanks!

HarryYuCS avatar Aug 27 '26 05:08 HarryYuCS

i wanna work on this..

sangam151 avatar Sep 03 '26 14:09 sangam151

i wanna work on this..

We don't assign until a PR is created and we can check it is headed in the right direction, although announcing you are working on it can prevent duplicative work.

shafik avatar Sep 03 '26 17:09 shafik

working on this

dineshtavva23 avatar Oct 18 '26 08:10 dineshtavva23

Hi, I'm looking at this for the CS3423 Compilers II hackathon at IIT Hyderabad. I'd like to try the approach aaronpuchert suggested (skip the reachability-based diagnostics when other errors exist) and fix up the tests that break. Is this still open? If it's fine, I'll start on a PR and mention this issue in it.

anupchavan avatar Oct 20 '26 08:10 anupchavan

17. Hi, I'm looking at this for the CS3423 Compilers II hackathon at IIT Hyderabad. I'd like to try the approach aaronpuchert suggested (skip the reachability-based diagnostics when other errors exist) and fix up the tests that break. Is this still open? If it's fine, I'll start on a PR and mention this issue in it.

Clang issues are not good hackathon problems, a new contributor will not be able to get through the process in a day. New contributors can take weeks to land a first contributions.

shafik avatar Oct 20 '26 16:10 shafik