[PATCH] D108893: clang-tidy: introduce readability-containter-data-pointer check

Saleem Abdulrasool via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 29 10:43:22 PDT 2021


compnerd created this revision.
compnerd added a reviewer: aaron.ballman.
Herald added a subscriber: mgorny.
compnerd requested review of this revision.
Herald added a project: clang-tools-extra.

This introduces a new check, readability-containter-data-pointer.  This
check is meant to catch the cases where the user may be trying to
materialize the data pointer by taking the address of the 0-th member of
a container.  With C++11 or newer, the `data` member should be used for
this.  This provides the following benefits:

- `.data()` is easier to read than `&[0]`
- it avoids an unnecessary re-materialization of the pointer
  - this doesn't matter in the case of optimized code, but in the case of unoptimized code, this will be visible
- it avoids a potential invalid memory de-reference caused by the indexing when the container is empty (in debug mode, clang will normally optimize away the re-materialization in optimized builds).

The small potential behavioural change raises the question of where the
check should belong.  A reasoning of defense in depth applies here, and
this does an unchecked conversion, with the assumption that users can
use the static analyzer to catch cases where we can statically identify
an invalid memory de-reference.  For the cases where the static analysis
is unable to prove the size of the container, UBSan can be used to track
the invalid access.

Special thanks to Aaron Ballman for the discussion on whether this
check would be useful and where to place it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108893

Files:
  clang-tools-extra/clang-tidy/readability/CMakeLists.txt
  clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
  clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h
  clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-container-data-pointer.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108893.369330.patch
Type: text/x-patch
Size: 14597 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210829/4034a6c9/attachment-0001.bin>


More information about the cfe-commits mailing list