| 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::tidy::bugprone { |
| 18 |
|
| 19 |
/// Checker for signal handler functions. |
| 20 |
/// |
| 21 |
/// For the user-facing documentation see: |
| 22 |
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/signal-handler.html |
| 23 |
class SignalHandlerCheck : public ClangTidyCheck { |
| 24 |
public: |
| 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 |
|
| 33 |
private: |
| 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 call |
| 37 |
/// graph scan). |
| 38 |
/// Bug reports are generated for the whole code body (no stop at the first |
| 39 |
/// found issue). For issues that are not in the code body, only one |
| 40 |
/// 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 another |
| 43 |
/// function) or the reference to the function (if it is used as a registered |
| 44 |
/// signal handler). This is the location where diagnostics are to be placed. |
| 45 |
/// \param ChainReporter A function that adds bug report notes to display the |
| 46 |
/// chain of called functions from signal handler registration to the current |
| 47 |
/// 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 |
std::function<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 |
std::function<void(bool)> ChainReporter); |
| 55 |
/// Returns true if a standard library function is considered |
| 56 |
/// asynchronous-safe. |
| 57 |
bool isStandardFunctionAsyncSafe(const FunctionDecl *FD) const; |
| 58 |
/// Add diagnostic notes to show the call chain of functions from a signal |
| 59 |
/// 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 contains |
| 62 |
/// the "path" (call chain) from the signal handler to the actual found |
| 63 |
/// function call. |
| 64 |
/// @param HandlerRef Reference to the signal handler function where it is |
| 65 |
/// registered as signal handler. |
| 66 |
/// @param SkipPathEnd If true the last item of the call chain (farthest away |
| 67 |
/// from the \c signal call) is omitted from note generation. |
| 68 |
void reportHandlerChain(const llvm::df_iterator<clang::CallGraphNode *> &Itr, |
| 69 |
const DeclRefExpr *HandlerRef, bool SkipPathEnd); |
| 70 |
|
| 71 |
clang::CallGraph CG; |
| 72 |
|
| 73 |
AsyncSafeFunctionSetKind AsyncSafeFunctionSet; |
| 74 |
llvm::StringSet<> ConformingFunctions; |
| 75 |
}; |
| 76 |
|
| 77 |
} // namespace clang::tidy::bugprone |
| 78 |
|
| 79 |
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H |