Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:19

0001 //===--- UnsafeFunctionsCheck.h - clang-tidy --------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 #include "../utils/Matchers.h"
0014 #include <optional>
0015 
0016 namespace clang::tidy::bugprone {
0017 
0018 /// Checks for functions that have safer, more secure replacements available, or
0019 /// are considered deprecated due to design flaws. This check relies heavily on,
0020 /// but is not exclusive to, the functions from the
0021 /// Annex K. "Bounds-checking interfaces" of C11.
0022 ///
0023 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/unsafe-functions.html
0024 class UnsafeFunctionsCheck : public ClangTidyCheck {
0025 public:
0026   UnsafeFunctionsCheck(StringRef Name, ClangTidyContext *Context);
0027   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0028 
0029   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0030   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0031 
0032   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0033                            Preprocessor *ModuleExpanderPP) override;
0034   void onEndOfTranslationUnit() override;
0035 
0036   struct CheckedFunction {
0037     std::string Name;
0038     matchers::MatchesAnyListedNameMatcher::NameMatcher Pattern;
0039     std::string Replacement;
0040     std::string Reason;
0041   };
0042 
0043 private:
0044   const std::vector<CheckedFunction> CustomFunctions;
0045 
0046   // If true, the default set of functions are reported.
0047   const bool ReportDefaultFunctions;
0048   /// If true, additional functions from widely used API-s (such as POSIX) are
0049   /// added to the list of reported functions.
0050   const bool ReportMoreUnsafeFunctions;
0051 
0052   Preprocessor *PP = nullptr;
0053   /// Whether "Annex K" functions are available and should be
0054   /// suggested in diagnostics. This is filled and cached internally.
0055   std::optional<bool> IsAnnexKAvailable;
0056 };
0057 
0058 } // namespace clang::tidy::bugprone
0059 
0060 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H