brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · 324b2c8 Raw
81 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_SIGNALHANDLERCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H11 12#include "../ClangTidyCheck.h"13#include "clang/Analysis/CallGraph.h"14#include "llvm/ADT/DepthFirstIterator.h"15#include "llvm/ADT/StringSet.h"16 17namespace clang::tidy::bugprone {18 19/// Checker for signal handler functions.20///21/// For the user-facing documentation see:22/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/signal-handler.html23class SignalHandlerCheck : public ClangTidyCheck {24public:25  enum class AsyncSafeFunctionSetKind { Minimal, POSIX };26 27  SignalHandlerCheck(StringRef Name, ClangTidyContext *Context);28  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;29  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;30  void registerMatchers(ast_matchers::MatchFinder *Finder) override;31  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;32 33private:34  /// Check if a function is allowed as a signal handler.35  /// Should test the properties of the function, and check in the code body.36  /// Should not check function calls in the code (this part is done by the call37  /// graph scan).38  /// Bug reports are generated for the whole code body (no stop at the first39  /// found issue). For issues that are not in the code body, only one40  /// bug report is generated.41  /// \param FD The function to check. It may or may not have a definition.42  /// \param CallOrRef Location of the call to this function (in another43  /// function) or the reference to the function (if it is used as a registered44  /// signal handler). This is the location where diagnostics are to be placed.45  /// \param ChainReporter A function that adds bug report notes to display the46  /// chain of called functions from signal handler registration to the current47  /// function. This is called at every generated bug report.48  /// The bool parameter is used like \c SkipPathEnd in \c reportHandlerChain .49  /// \return Returns true if a diagnostic was emitted for this function.50  bool checkFunction(const FunctionDecl *FD, const Expr *CallOrRef,51                     llvm::function_ref<void(bool)> ChainReporter);52  /// Similar as \c checkFunction but only check for C++14 rules.53  bool checkFunctionCPP14(const FunctionDecl *FD, const Expr *CallOrRef,54                          llvm::function_ref<void(bool)> ChainReporter);55  /// Returns true if a standard library function is considered56  /// asynchronous-safe.57  bool isStandardFunctionAsyncSafe(const FunctionDecl *FD) const;58  /// Add diagnostic notes to show the call chain of functions from a signal59  /// handler to a function that is called (directly or indirectly) from it.60  /// Also add a note to the place where the signal handler is registered.61  /// @param Itr Position during a call graph depth-first iteration. It contains62  /// the "path" (call chain) from the signal handler to the actual found63  /// function call.64  /// @param HandlerRef Reference to the signal handler function where it is65  /// registered as signal handler.66  /// @param SkipPathEnd If true the last item of the call chain (farthest away67  /// from the \c signal call) is omitted from note generation.68  void69  reportHandlerChain(const llvm::df_iterator<const clang::CallGraphNode *> &Itr,70                     const DeclRefExpr *HandlerRef, bool SkipPathEnd);71 72  clang::CallGraph CG;73 74  AsyncSafeFunctionSetKind AsyncSafeFunctionSet;75  llvm::StringSet<> ConformingFunctions;76};77 78} // namespace clang::tidy::bugprone79 80#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H81