10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Lex/PPCallbacks.h"
14 #include "clang/Lex/Preprocessor.h"
23 const char DiagWording[] =
24 "do not call %0; consider using exception handling instead";
26 class SetJmpMacroCallbacks :
public PPCallbacks {
27 SetLongJmpCheck &Check;
30 explicit SetJmpMacroCallbacks(SetLongJmpCheck &Check) : Check(Check) {}
32 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &
MD,
33 SourceRange
Range,
const MacroArgs *Args)
override {
34 const auto *II = MacroNameTok.getIdentifierInfo();
38 if (II->getName() ==
"setjmp")
39 Check.diag(
Range.getBegin(), DiagWording) << II;
44 void SetLongJmpCheck::registerPPCallbacks(
const SourceManager &SM,
46 Preprocessor *ModuleExpanderPP) {
49 PP->addPPCallbacks(std::make_unique<SetJmpMacroCallbacks>(*
this));
52 void SetLongJmpCheck::registerMatchers(MatchFinder *Finder) {
57 callExpr(callee(functionDecl(hasAnyName(
"setjmp",
"longjmp"))))
62 void SetLongJmpCheck::check(
const MatchFinder::MatchResult &Result) {
63 const auto *
E = Result.Nodes.getNodeAs<CallExpr>(
"expr");
64 diag(
E->getExprLoc(), DiagWording) << cast<NamedDecl>(
E->getCalleeDecl());