This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Don't mark plain MS enums as fixed
ClosedPublic

Authored by rnk on Feb 8 2018, 6:21 PM.

Details

Summary

This fixes a flaw in our AST: PR27098

MSVC always gives plain enums the underlying type 'int'. Clang does this
as well, but we claim the enum is "fixed", as if the user actually wrote
': int'. It means we end up emitting spurious -Wsign-compare warnings on
code like this:

enum Vals { E1, E2, E3 };
bool f(unsigned v1, Vals v2) {
  return v1 == v2;
}

We think 'v2' can take on negative values because we think 'Vals' is
fixed. This fixes that.

Diff Detail

Repository
rC Clang

Event Timeline

rnk created this revision.Feb 8 2018, 6:21 PM
dberris accepted this revision.Feb 8 2018, 9:20 PM
dberris added a subscriber: dberris.

LGTM

This revision is now accepted and ready to land.Feb 8 2018, 9:20 PM

We've seen lots of spurious warnings from this. Thanks for the fix!

thakis added a subscriber: thakis.Feb 9 2018, 5:23 AM

Nice!

Test?

rnk updated this revision to Diff 133670.Feb 9 2018, 12:37 PM
  • Add test, update comments
rnk added a comment.Feb 9 2018, 12:40 PM

Nice!

Test?

Yeah, I forgot to get that in. From the test you can see that we have weirdly different behavior in C and C++ mode. Do people care? This is pre-existing behavior. My change makes the enum "unfixed" in C++, which changes the C++ behavior. Changing C mode to match C++ mode seems like it should be a separate change. Do people think we should do that as well?

rsmith accepted this revision.Feb 9 2018, 5:52 PM

Thanks! I'd noticed this weirdness but wasn't sure what we could do about it without breaking MS compat. I like this approach a lot.

If we want to change the C behavior too, I think that should be a separate change. How does MSVC behave in C mode?

rnk added a comment.Feb 12 2018, 9:12 AM

Thanks! I'd noticed this weirdness but wasn't sure what we could do about it without breaking MS compat. I like this approach a lot.

Great, sorry for the delay.

If we want to change the C behavior too, I think that should be a separate change. How does MSVC behave in C mode?

They warn in both C and C++ on the code below:

enum PosEnum { A_a = 0, A_b = 1, A_c = 10 };
int test_pos(enum PosEnum a, unsigned uv) { return a < uv; }

I picked this back up because clang-cl was giving -Wsign-comparison warnings in code like this:

unsigned DiagID = ...;
EXPECT_EQ(DiagID, diag::warn_foo);

I think there has been a recent growth in gtest usage, so this was starting to get really annoying. The source-level fix would be to give the diag enums a fixed type, but I didn't feel like doing that to literally every enum in LLVM and clang. I guess the MSVC build will just be noisy unless we disable that warning.

This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.