This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix crash and handle AttributedTypes in 'bugprone-easily-swappable-parameters'
ClosedPublic

Authored by whisperity on Jul 20 2021, 7:10 AM.

Details

Summary

@vabridgers identified a way to crash the check by running on code that involve AttributedTypes. This patch fixes the check to first and foremost not crash, but also improves the logic handling qualifiers.

If the types contain any additional (not just CVR) qualifiers that are not the same, they will not be deemed mixable. The logic for CVR-mixing, and the QualifiersMix check option remain unchanged.

Diff Detail

Event Timeline

whisperity created this revision.Jul 20 2021, 7:10 AM
whisperity requested review of this revision.Jul 20 2021, 7:10 AM

The rename from CommonType to CoreType could probably be done as a separate NFC to make the review a bit easier, if we decide that's a good terminology switch. I'm not certain switching from common to core makes things more clear though; what's the distinction trying to be made?

clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp
476–477
2199

I don't know that "core type" is a term of art that will convey much meaning to users in a diagnostic message. Do we need to change the wording of the diagnostic?

clang-tools-extra/test/clang-tidy/checkers/bugprone-easily-swappable-parameters-len2.cpp
362–364

Yeah, the diagnostic here is a bit unfortunate. This was an existing deficiency with the check though, right? I assume it also impacts references and not just pointers?

clang-tools-extra/test/clang-tidy/checkers/bugprone-easily-swappable-parameters-qualifiermixing.cpp
122

Should probably have a FIXME comment here as well given the lack of a pointer.

This change fixed the downstream crash. Thanks for the quick turnaround!

whisperity marked 4 inline comments as done.
  • Remove orthogonal/half-baked changes.
  • Fix a mistake in the logic changing only a temporary state.
  • Add a missing FIXME.

The rename from CommonType to CoreType could probably be done as a separate NFC to make the review a bit easier, if we decide that's a good terminology switch. I'm not certain switching from common to core makes things more clear though; what's the distinction trying to be made?

Not much, just tried to dance around the fact that the diagnostic emitted for this is, put simply, crappy. I'm coming up with a better solution, and indeed, let's fix the issue at hand now and focus on the improvements later.

clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp
476–477

Actually, the suggestion is also bad. getQualifiers() returns a copy on which adding is a moot operation... Turns out you can use ASTContext to create a specifically qualified type for you.

Now if there was a way to express this in the type of getQualifiers(), to warn you "Don't make the mistake of thinking this would CHANGE anything!"...

476–477

Sorry, I mean, not the suggestion per se, but these 4 new lines of code contained multiple issues.

2199

The wording needs to be changed, but not in this particular way. I started working on a follow-up refactoring patch in general and some changes made it into this diff. I'll revert these from this patch and will work everything related to that into a new one.

clang-tools-extra/test/clang-tidy/checkers/bugprone-easily-swappable-parameters-len2.cpp
362–364

I will look into this (and improving the diagnostics in general) in a follow-up patch.

whisperity edited the summary of this revision. (Show Details)

NFC Fix bad phrasing in comments (e.g. still mentioning core type and having a misleading negated sentence.)

aaron.ballman accepted this revision.Jul 21 2021, 9:21 AM

LGTM!

clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp
476–477

Ah, this looks much better, thank you!

This revision is now accepted and ready to land.Jul 21 2021, 9:21 AM