10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Preprocessor.h"
21 callExpr(callee(functionDecl(hasName(
"::pthread_kill"))),
23 hasArgument(1, integerLiteral().bind(
"integer-literal")))
28static Preprocessor *
PP;
31 const auto IsSigterm = [](
const auto &KeyValue) ->
bool {
32 return KeyValue.first->getName() ==
"SIGTERM" &&
33 KeyValue.first->hasMacroDefinition();
35 const auto TryExpandAsInteger =
36 [](Preprocessor::macro_iterator It) -> std::optional<unsigned> {
37 if (It ==
PP->macro_end())
39 const MacroInfo *MI =
PP->getMacroInfo(It->first);
40 const Token &T = MI->tokens().back();
41 if (!T.isLiteral() || !T.getLiteralData())
43 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
46 constexpr unsigned AutoSenseRadix = 0;
47 if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
49 return IntValue.getZExtValue();
52 const auto SigtermMacro = llvm::find_if(
PP->macros(), IsSigterm);
57 const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>(
"thread-kill");
58 const auto *MatchedIntLiteral =
59 Result.Nodes.getNodeAs<IntegerLiteral>(
"integer-literal");
61 diag(MatchedExpr->getBeginLoc(),
62 "thread should not be terminated by raising the 'SIGTERM' signal");
67 const SourceManager &SM, Preprocessor *Pp, Preprocessor *ModuleExpanderPP) {
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
std::optional< unsigned > SigtermValue