SignalHandlerCheck.h 3.9 KB

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