10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Preprocessor.h"
20 void BadSignalToKillThreadCheck::registerMatchers(MatchFinder *Finder) {
22 callExpr(allOf(callee(functionDecl(hasName(
"::pthread_kill"))),
24 hasArgument(1, integerLiteral().bind(
"integer-literal")))
29 static Preprocessor *
PP;
31 void BadSignalToKillThreadCheck::check(
const MatchFinder::MatchResult &Result) {
32 const auto IsSigterm = [](
const auto &KeyValue) ->
bool {
33 return KeyValue.first->getName() ==
"SIGTERM";
35 const auto TryExpandAsInteger =
36 [](Preprocessor::macro_iterator It) -> Optional<unsigned> {
37 if (It ==
PP->macro_end())
39 const MacroInfo *MI =
PP->getMacroInfo(It->first);
40 const Token &T = MI->tokens().back();
41 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
44 constexpr
unsigned AutoSenseRadix = 0;
45 if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
47 return IntValue.getZExtValue();
50 const auto SigtermMacro = llvm::find_if(
PP->macros(), IsSigterm);
52 if (!SigtermValue && !(SigtermValue = TryExpandAsInteger(SigtermMacro)))
55 const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>(
"thread-kill");
56 const auto *MatchedIntLiteral =
57 Result.Nodes.getNodeAs<IntegerLiteral>(
"integer-literal");
58 if (MatchedIntLiteral->getValue() == *SigtermValue) {
59 diag(MatchedExpr->getBeginLoc(),
60 "thread should not be terminated by raising the 'SIGTERM' signal");
64 void BadSignalToKillThreadCheck::registerPPCallbacks(
65 const SourceManager &SM, Preprocessor *pp, Preprocessor *ModuleExpanderPP) {