clang-tools 10.0.0

NotNullTerminatedResultCheck.h
Go to the documentation of this file.
1 //===--- NotNullTerminatedResultCheck.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_NOT_NULL_TERMINATED_RESULT_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H
11
12 #include "../ClangTidy.h"
13
14 namespace clang {
15 namespace tidy {
16 namespace bugprone {
17
18 /// Finds function calls where it is possible to cause a not null-terminated
19 /// result. Usually the proper length of a string is 'strlen(src) + 1' or
20 /// equal length of this expression, because the null terminator needs an extra
21 /// space. Without the null terminator it can result in undefined behaviour
22 /// when the string is read.
23 ///
24 /// For the user-facing documentation see:
25 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-not-null-terminated-result.html
26 class NotNullTerminatedResultCheck : public ClangTidyCheck {
27 public:
28 NotNullTerminatedResultCheck(StringRef Name, ClangTidyContext *Context);
29 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
30 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
31 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
32 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
33 Preprocessor *ModuleExpanderPP) override;
34
35 private:
36 // If non-zero it is specifying if the target environment is considered to
37 // implement '_s' suffixed memory and string handler functions which are safer
38 // than older version (e.g. 'memcpy_s()'). The default value is '1'.
39 const int WantToUseSafeFunctions;
40
41 bool UseSafeFunctions = false;
42
43 void memoryHandlerFunctionFix(
44 StringRef Name, const ast_matchers::MatchFinder::MatchResult &Result);
45 void memcpyFix(StringRef Name,
46 const ast_matchers::MatchFinder::MatchResult &Result,
47 DiagnosticBuilder &Diag);
48 void memcpy_sFix(StringRef Name,
49 const ast_matchers::MatchFinder::MatchResult &Result,
50 DiagnosticBuilder &Diag);
51 void memchrFix(StringRef Name,
52 const ast_matchers::MatchFinder::MatchResult &Result);
53 void memmoveFix(StringRef Name,
54 const ast_matchers::MatchFinder::MatchResult &Result,
55 DiagnosticBuilder &Diag);
56 void strerror_sFix(const ast_matchers::MatchFinder::MatchResult &Result);
57 void ncmpFix(StringRef Name,
58 const ast_matchers::MatchFinder::MatchResult &Result);
59 void xfrmFix(StringRef Name,
60 const ast_matchers::MatchFinder::MatchResult &Result);
61 };
62
63 } // namespace bugprone
64 } // namespace tidy
65 } // namespace clang
66
67 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NOT_NULL_TERMINATED_RESULT_H
clang::tidy::bugprone::NotNullTerminatedResultCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: NotNullTerminatedResultCheck.cpp:543
clang::tidy::bugprone::NotNullTerminatedResultCheck
Finds function calls where it is possible to cause a not null-terminated result.
Definition: NotNullTerminatedResultCheck.h:26
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::bugprone::NotNullTerminatedResultCheck::NotNullTerminatedResultCheck
NotNullTerminatedResultCheck(StringRef Name, ClangTidyContext *Context)
Definition: NotNullTerminatedResultCheck.cpp:499
clang::tidy::bugprone::NotNullTerminatedResultCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: NotNullTerminatedResultCheck.cpp:790
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::bugprone::NotNullTerminatedResultCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: NotNullTerminatedResultCheck.cpp:504
clang::tidy::bugprone::NotNullTerminatedResultCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: NotNullTerminatedResultCheck.cpp:509