xref: /llvm-project/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.h (revision 0b8866d15ac5806a980d2ff2ea63240d8acfa778)
1 //===--- UnsafeFunctionsCheck.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_UNSAFEFUNCTIONSCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "../utils/Matchers.h"
14 #include <optional>
15 
16 namespace clang::tidy::bugprone {
17 
18 /// Checks for functions that have safer, more secure replacements available, or
19 /// are considered deprecated due to design flaws. This check relies heavily on,
20 /// but is not exclusive to, the functions from the
21 /// Annex K. "Bounds-checking interfaces" of C11.
22 ///
23 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/unsafe-functions.html
24 class UnsafeFunctionsCheck : public ClangTidyCheck {
25 public:
26   UnsafeFunctionsCheck(StringRef Name, ClangTidyContext *Context);
27   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
28 
29   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31 
32   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
33                            Preprocessor *ModuleExpanderPP) override;
34   void onEndOfTranslationUnit() override;
35 
36   struct CheckedFunction {
37     std::string Name;
38     matchers::MatchesAnyListedNameMatcher::NameMatcher Pattern;
39     std::string Replacement;
40     std::string Reason;
41   };
42 
43 private:
44   const std::vector<CheckedFunction> CustomFunctions;
45 
46   // If true, the default set of functions are reported.
47   const bool ReportDefaultFunctions;
48   /// If true, additional functions from widely used API-s (such as POSIX) are
49   /// added to the list of reported functions.
50   const bool ReportMoreUnsafeFunctions;
51 
52   Preprocessor *PP = nullptr;
53   /// Whether "Annex K" functions are available and should be
54   /// suggested in diagnostics. This is filled and cached internally.
55   std::optional<bool> IsAnnexKAvailable;
56 };
57 
58 } // namespace clang::tidy::bugprone
59 
60 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H
61