11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Frontend/CompilerInstance.h" 14 #include "clang/Lex/PPCallbacks.h" 15 #include "clang/Lex/Preprocessor.h" 23 const char SetLongJmpCheck::DiagWording[] =
24 "do not call %0; consider using exception handling instead";
27 class SetJmpMacroCallbacks :
public PPCallbacks {
28 SetLongJmpCheck &Check;
31 explicit SetJmpMacroCallbacks(SetLongJmpCheck &Check) : Check(Check) {}
33 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
34 SourceRange
Range,
const MacroArgs *Args)
override {
35 const auto *II = MacroNameTok.getIdentifierInfo();
39 if (II->getName() ==
"setjmp")
40 Check.diag(Range.getBegin(), Check.DiagWording) << II;
45 void SetLongJmpCheck::registerPPCallbacks(CompilerInstance &Compiler) {
48 if (!getLangOpts().CPlusPlus)
53 Compiler.getPreprocessor().addPPCallbacks(
54 llvm::make_unique<SetJmpMacroCallbacks>(*
this));
57 void SetLongJmpCheck::registerMatchers(MatchFinder *Finder) {
60 if (!getLangOpts().CPlusPlus)
66 Finder->addMatcher(callExpr(callee(functionDecl(anyOf(hasName(
"setjmp"),
67 hasName(
"longjmp")))))
72 void SetLongJmpCheck::check(
const MatchFinder::MatchResult &Result) {
73 const auto *E = Result.Nodes.getNodeAs<CallExpr>(
"expr");
74 diag(E->getExprLoc(), DiagWording) << cast<NamedDecl>(E->getCalleeDecl());
CharSourceRange Range
SourceRange for the file name.