This is an archive of the discontinued LLVM Phabricator instance.

[Diagnostics] Warn if enumeration type mismatch in conditional expression
ClosedPublic

Authored by xbolva00 on Sep 23 2019, 8:29 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

xbolva00 created this revision.Sep 23 2019, 8:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 23 2019, 8:29 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
xbolva00 updated this revision to Diff 221346.Sep 23 2019, 8:30 AM

Added test file.

xbolva00 edited the summary of this revision. (Show Details)Sep 23 2019, 8:31 AM
xbolva00 marked an inline comment as done.Sep 23 2019, 8:42 AM
xbolva00 added inline comments.
test/Sema/warn-conditional-emum-types-mismatch.c
19 ↗(On Diff #221346)

Gcc warns here, but Clang does not warn when A != C..

So not sure here..

aaron.ballman added inline comments.Sep 25 2019, 9:27 AM
lib/Sema/SemaChecking.cpp
11264 ↗(On Diff #221346)

const auto * (and below as well) since the type is spelled out in the initialization.

11272–11274 ↗(On Diff #221346)

Would it make sense to use !LHSEnumType->getDecl()->hasNameForLinkage() here? It seems like that's the situation we care about.

11785 ↗(On Diff #221346)

Might make sense to name the new function CheckConditionalWithEnumTypes() to match the local style better.

test/Sema/warn-conditional-emum-types-mismatch.c
15 ↗(On Diff #221346)

Spurious '

19 ↗(On Diff #221346)

My gut reaction is that I think Clang should warn here as well because the code pattern is confusing, but I'd also say that if there's a lot of false positives where the code is sensible, it may make sense to suppress the diagnostic. One situation I was thinking of where you could run into something like this is:

enum {
  STATUS_SUCCESS,
  STATUS_FAILURE,
  ...
  MAX_BASE_STATUS_CODE
};

enum ExtendedStatusCodes {
  STATUS_SOMETHING_INTERESTING = MAX_BASE_STATUS_CODE + 1000,
  ...
};

int whatever(void) {
  return some_condition() ? STATUS_SOMETHING_INTERESTING : STATUS_SUCCESS;
}
xbolva00 updated this revision to Diff 221860.Sep 25 2019, 4:31 PM

addressed review notes

xbolva00 marked 6 inline comments as done.Sep 25 2019, 4:31 PM
xbolva00 added inline comments.
test/Sema/warn-conditional-emum-types-mismatch.c
19 ↗(On Diff #221346)

+1

aaron.ballman accepted this revision.Sep 30 2019, 12:40 PM

LGTM aside from a commenting request.

test/Sema/warn-conditional-emum-types-mismatch.c
19 ↗(On Diff #221346)

Can you add some comments to the test case explaining that we purposefully differ from GCC here and why?

This revision is now accepted and ready to land.Sep 30 2019, 12:40 PM
xbolva00 updated this revision to Diff 222478.Sep 30 2019, 12:52 PM
xbolva00 marked an inline comment as done.

Added comment why we differ

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptSep 30 2019, 12:54 PM