clang-tools  15.0.0git
StringviewNullptrCheck.h
Go to the documentation of this file.
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 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STRINGVIEWNULLPTRCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STRINGVIEWNULLPTRCHECK_H
11 
12 #include "../utils/TransformerClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace bugprone {
17 
18 /// Checks for various ways that the `const CharT*` constructor of
19 /// `std::basic_string_view` can be passed a null argument and replaces them
20 /// with the default constructor in most cases. For the comparison operators,
21 /// braced initializer list does not compile so instead a call to `.empty()` or
22 /// the empty string literal are used, where appropriate.
23 ///
24 /// This prevents code from invoking behavior which is unconditionally
25 /// undefined. The single-argument `const CharT*` constructor does not check
26 /// for the null case before dereferencing its input. The standard is slated to
27 /// add an explicitly-deleted overload to catch some of these cases:
28 /// wg21.link/p2166
29 ///
30 /// To catch the additional cases of `NULL` (which expands to `__null`) and
31 /// `0`, first run the ``modernize-use-nullptr`` check to convert the callers
32 /// to `nullptr`.
33 ///
34 /// For the user-facing documentation see:
35 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/stringview-nullptr.html
37 public:
38  StringviewNullptrCheck(StringRef Name, ClangTidyContext *Context);
39 
40  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
41  return LangOpts.CPlusPlus17;
42  }
43 };
44 
45 } // namespace bugprone
46 } // namespace tidy
47 } // namespace clang
48 
49 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STRINGVIEWNULLPTRCHECK_H
clang::tidy::bugprone::StringviewNullptrCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: StringviewNullptrCheck.h:40
clang::tidy::bugprone::StringviewNullptrCheck::StringviewNullptrCheck
StringviewNullptrCheck(StringRef Name, ClangTidyContext *Context)
Definition: StringviewNullptrCheck.cpp:300
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::tidy::utils::TransformerClangTidyCheck
A base class for defining a ClangTidy check based on a RewriteRule.
Definition: TransformerClangTidyCheck.h:39
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:366
clang::tidy::bugprone::StringviewNullptrCheck
Checks for various ways that the const CharT* constructor of std::basic_string_view can be passed a n...
Definition: StringviewNullptrCheck.h:36