This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] fix PR36489 - respect deduced pointer types from auto as well
ClosedPublic

Authored by JonasToth on Jun 28 2018, 5:37 AM.

Details

Summary

The cppcoreguidelines-pro-bounds-pointer-arithmetic warns on all occassion where
pointer arithmetic is used, but does not check values where the pointer types
is deduced via `auto`. This patch adjusts this behaviour and solved
PR36489.

Diff Detail

Event Timeline

JonasToth created this revision.Jun 28 2018, 5:37 AM
alexfh added inline comments.Jun 28 2018, 6:44 AM
clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
24 ↗(On Diff #153297)

Can anyOf be pushed inside hasType? (hasType(anyOf(pointerType(), autoType(...))))

This revision is now accepted and ready to land.Jun 28 2018, 6:45 AM
aaron.ballman requested changes to this revision.Jun 28 2018, 6:58 AM
aaron.ballman added inline comments.
test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic-pr36489.cpp
4 ↗(On Diff #153297)

Can this also include tests for other deduced types, like decltype(expr) and decltype(auto)?

test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic.cpp
88–115

Spurious whitespace change.

This revision now requires changes to proceed.Jun 28 2018, 6:58 AM
JonasToth marked 3 inline comments as done.Jun 28 2018, 10:02 AM
JonasToth added inline comments.
clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
24 ↗(On Diff #153297)

Unfortunatly no. It is ambigous then.

JonasToth marked 2 inline comments as done.Jun 28 2018, 10:03 AM
  • address review comments
This revision is now accepted and ready to land.Jun 28 2018, 10:06 AM
JonasToth marked an inline comment as not done.Jun 28 2018, 10:08 AM
JonasToth added inline comments.
test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic-pr36489.cpp
4 ↗(On Diff #153297)

decltype(getPtr()) actually breaks the code. I have to look into this, but tomorrow or on the weekend.

JonasToth planned changes to this revision.Jun 28 2018, 10:09 AM
JonasToth marked an inline comment as not done.
JonasToth updated this revision to Diff 153457.Jun 29 2018, 2:55 AM
  • fix decltype deduction with new matcher

I had to introduce a new matcher in clang for DecltypeType to reach its
underlying type. It would be better to have hasType resolve all these issues
but i dont know how much work that would be.

We can discuss that in the patch for the matcher i guess.

This revision is now accepted and ready to land.Jun 29 2018, 2:55 AM
  • fix decltype deduction with new matcher

I had to introduce a new matcher in clang for DecltypeType to reach its
underlying type. It would be better to have hasType resolve all these issues
but i dont know how much work that would be.

I don't think we want to modify hasType() -- that would strip off too much type information if it automatically reached through to the underlying type. However, we may want to consider adding something like hasUnderlyingType() that checks the matcher against each level of type sugar. I'm not certain this would be required for your patch, however.

I don't think we want to modify hasType() -- that would strip off too much type information if it automatically reached through to the underlying type. However, we may want to consider adding something like hasUnderlyingType() that checks the matcher against each level of type sugar. I'm not certain this would be required for your patch, however.

Yes, an additional facility that does not care how the type got produced (auto, decltype, written, template, ...) and just finds all places with that type.

JonasToth marked 2 inline comments as done.Jul 23 2018, 9:34 AM

The tests do run now with decltype too.

Still ready to land?

aaron.ballman accepted this revision.Jul 23 2018, 9:43 AM

Yes, this looks good to me. Thanks!

rebase to master

This revision was automatically updated to reflect the committed changes.