1 //===--- SignalHandlerCheck.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_SIGNALHANDLERCHECK_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H 11 12 #include "../ClangTidyCheck.h" 13 #include "clang/Analysis/CallGraph.h" 14 #include "llvm/ADT/DepthFirstIterator.h" 15 #include "llvm/ADT/StringSet.h" 16 17 namespace clang { 18 namespace tidy { 19 namespace bugprone { 20 21 /// Checker for signal handler functions. 22 /// 23 /// For the user-facing documentation see: 24 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/signal-handler-check.html 25 class SignalHandlerCheck : public ClangTidyCheck { 26 public: 27 enum class AsyncSafeFunctionSetKind { Minimal, POSIX }; 28 29 SignalHandlerCheck(StringRef Name, ClangTidyContext *Context); 30 void storeOptions(ClangTidyOptions::OptionMap &Opts) override; 31 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override; 32 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 33 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 34 35 private: 36 /// Check if a function is allowed as a signal handler. 37 /// Should test the properties of the function, and check in the code body. 38 /// Should not check function calls in the code (this part is done by the call 39 /// graph scan). 40 /// @param FD The function to check. It may or may not have a definition. 41 /// @param CallOrRef Location of the call to this function (in another 42 /// function) or the reference to the function (if it is used as a registered 43 /// signal handler). This is the location where diagnostics are to be placed. 44 /// @return Returns true if a diagnostic was emitted for this function. 45 bool checkFunction(const FunctionDecl *FD, const Expr *CallOrRef); 46 /// Returns true if a standard library function is considered as 47 /// asynchronous-safe. 48 bool isStandardFunctionAsyncSafe(const FunctionDecl *FD) const; 49 /// Add diagnostic notes to show the call chain of functions from a signal 50 /// handler to a function that is called (directly or indirectly) from it. 51 /// Also add a note to the place where the signal handler is registered. 52 /// @param Itr Position during a call graph depth-first iteration. It contains 53 /// the "path" (call chain) from the signal handler to the actual found 54 /// function call. 55 /// @param HandlerRef Reference to the signal handler function where it is 56 /// registered as signal handler. 57 void reportHandlerChain(const llvm::df_iterator<clang::CallGraphNode *> &Itr, 58 const DeclRefExpr *HandlerRef); 59 60 clang::CallGraph CG; 61 62 AsyncSafeFunctionSetKind AsyncSafeFunctionSet; 63 const llvm::StringSet<> ConformingFunctions; 64 }; 65 66 } // namespace bugprone 67 } // namespace tidy 68 } // namespace clang 69 70 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H 71