ContainerDataPointerCheck.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //===--- ContainerDataPointerCheck.h - clang-tidy ---------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERDATAPOINTERCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERDATAPOINTERCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::readability {
  12. /// Checks whether a call to `operator[]` and `&` can be replaced with a call to
  13. /// `data()`.
  14. ///
  15. /// This only replaces the case where the offset being accessed through the
  16. /// subscript operation is a known constant 0. This avoids a potential invalid
  17. /// memory access when the container is empty. Cases where the constant is not
  18. /// explicitly zero can be addressed through the clang static analyzer, and
  19. /// those which cannot be statically identified can be caught using UBSan.
  20. class ContainerDataPointerCheck : public ClangTidyCheck {
  21. public:
  22. ContainerDataPointerCheck(StringRef Name, ClangTidyContext *Context);
  23. bool isLanguageVersionSupported(const LangOptions &LO) const override {
  24. return LO.CPlusPlus11;
  25. }
  26. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  27. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  28. std::optional<TraversalKind> getCheckTraversalKind() const override {
  29. return TK_IgnoreUnlessSpelledInSource;
  30. }
  31. };
  32. } // namespace clang::tidy::readability
  33. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONTAINERDATAPOINTERCHECK_H