clang-tools  16.0.0git
SignalHandlerCheck.h
Go to the documentation of this file.
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
26 public:
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  /// Bug reports are generated for the whole code body (no stop at the first
41  /// found issue). For issues that are not in the code body, only one
42  /// bug report is generated.
43  /// \param FD The function to check. It may or may not have a definition.
44  /// \param CallOrRef Location of the call to this function (in another
45  /// function) or the reference to the function (if it is used as a registered
46  /// signal handler). This is the location where diagnostics are to be placed.
47  /// \param ChainReporter A function that adds bug report notes to display the
48  /// chain of called functions from signal handler registration to the current
49  /// function. This is called at every generated bug report.
50  /// The bool parameter is used like \c SkipPathEnd in \c reportHandlerChain .
51  /// \return Returns true if a diagnostic was emitted for this function.
52  bool checkFunction(const FunctionDecl *FD, const Expr *CallOrRef,
53  std::function<void(bool)> ChainReporter);
54  /// Similar as \c checkFunction but only check for C++14 rules.
55  bool checkFunctionCPP14(const FunctionDecl *FD, const Expr *CallOrRef,
56  std::function<void(bool)> ChainReporter);
57  /// Returns true if a standard library function is considered
58  /// asynchronous-safe.
59  bool isStandardFunctionAsyncSafe(const FunctionDecl *FD) const;
60  /// Add diagnostic notes to show the call chain of functions from a signal
61  /// handler to a function that is called (directly or indirectly) from it.
62  /// Also add a note to the place where the signal handler is registered.
63  /// @param Itr Position during a call graph depth-first iteration. It contains
64  /// the "path" (call chain) from the signal handler to the actual found
65  /// function call.
66  /// @param HandlerRef Reference to the signal handler function where it is
67  /// registered as signal handler.
68  /// @param SkipPathEnd If true the last item of the call chain (farthest away
69  /// from the \c signal call) is omitted from note generation.
70  void reportHandlerChain(const llvm::df_iterator<clang::CallGraphNode *> &Itr,
71  const DeclRefExpr *HandlerRef, bool SkipPathEnd);
72 
73  clang::CallGraph CG;
74 
75  AsyncSafeFunctionSetKind AsyncSafeFunctionSet;
76  llvm::StringSet<> ConformingFunctions;
77 };
78 
79 } // namespace bugprone
80 } // namespace tidy
81 } // namespace clang
82 
83 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H
clang::tidy::bugprone::SignalHandlerCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SignalHandlerCheck.cpp:371
clang::tidy::bugprone::SignalHandlerCheck
Checker for signal handler functions.
Definition: SignalHandlerCheck.h:25
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:53
clang::tidy::bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind::Minimal
@ Minimal
clang::tidy::bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind
AsyncSafeFunctionSetKind
Definition: SignalHandlerCheck.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::tidy::bugprone::SignalHandlerCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: SignalHandlerCheck.cpp:350
clang::tidy::bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind::POSIX
@ POSIX
clang::tidy::bugprone::SignalHandlerCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SignalHandlerCheck.cpp:346
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::SignalHandlerCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SignalHandlerCheck.cpp:355
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:375
clang::tidy::bugprone::SignalHandlerCheck::SignalHandlerCheck
SignalHandlerCheck(StringRef Name, ClangTidyContext *Context)
Definition: SignalHandlerCheck.cpp:332