clang-tools  15.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  /// @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
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:337
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:314
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:310
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:323
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:366
clang::tidy::bugprone::SignalHandlerCheck::SignalHandlerCheck
SignalHandlerCheck(StringRef Name, ClangTidyContext *Context)
Definition: SignalHandlerCheck.cpp:300