61 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H11 12#include "../ClangTidyCheck.h"13#include "../utils/Matchers.h"14#include <optional>15 16namespace clang::tidy::bugprone {17 18/// Checks for functions that have safer, more secure replacements available, or19/// are considered deprecated due to design flaws. This check relies heavily on,20/// but is not exclusive to, the functions from the21/// Annex K. "Bounds-checking interfaces" of C11.22///23/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unsafe-functions.html24class UnsafeFunctionsCheck : public ClangTidyCheck {25public: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 43private: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) are49 /// 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 be54 /// suggested in diagnostics. This is filled and cached internally.55 std::optional<bool> IsAnnexKAvailable;56};57 58} // namespace clang::tidy::bugprone59 60#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNSAFEFUNCTIONSCHECK_H61