StringviewNullptrCheck.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //===--- StringviewNullptrCheck.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_BUGPRONE_STRINGVIEWNULLPTRCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STRINGVIEWNULLPTRCHECK_H
  10. #include "../utils/TransformerClangTidyCheck.h"
  11. namespace clang::tidy::bugprone {
  12. /// Checks for various ways that the `const CharT*` constructor of
  13. /// `std::basic_string_view` can be passed a null argument and replaces them
  14. /// with the default constructor in most cases. For the comparison operators,
  15. /// braced initializer list does not compile so instead a call to `.empty()` or
  16. /// the empty string literal are used, where appropriate.
  17. ///
  18. /// This prevents code from invoking behavior which is unconditionally
  19. /// undefined. The single-argument `const CharT*` constructor does not check
  20. /// for the null case before dereferencing its input. The standard is slated to
  21. /// add an explicitly-deleted overload to catch some of these cases:
  22. /// wg21.link/p2166
  23. ///
  24. /// To catch the additional cases of `NULL` (which expands to `__null`) and
  25. /// `0`, first run the ``modernize-use-nullptr`` check to convert the callers
  26. /// to `nullptr`.
  27. ///
  28. /// For the user-facing documentation see:
  29. /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/stringview-nullptr.html
  30. class StringviewNullptrCheck : public utils::TransformerClangTidyCheck {
  31. public:
  32. StringviewNullptrCheck(StringRef Name, ClangTidyContext *Context);
  33. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  34. return LangOpts.CPlusPlus17;
  35. }
  36. };
  37. } // namespace clang::tidy::bugprone
  38. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STRINGVIEWNULLPTRCHECK_H