10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
20 void UseUncaughtExceptionsCheck::registerMatchers(MatchFinder *Finder) {
21 std::string MatchText =
"::std::uncaught_exception";
25 usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(hasName(MatchText))))
31 declRefExpr(to(functionDecl(hasName(MatchText))), unless(callExpr()))
32 .bind(
"decl_ref_expr"),
35 auto DirectCallToUncaughtException = callee(expr(ignoringImpCasts(
36 declRefExpr(hasDeclaration(functionDecl(hasName(MatchText)))))));
39 Finder->addMatcher(callExpr(DirectCallToUncaughtException,
40 unless(hasAncestor(initListExpr())))
45 Finder->addMatcher(callExpr(DirectCallToUncaughtException,
46 hasAncestor(initListExpr()))
47 .bind(
"init_call_expr"),
51 void UseUncaughtExceptionsCheck::check(
const MatchFinder::MatchResult &Result) {
52 SourceLocation BeginLoc;
53 SourceLocation EndLoc;
54 const CallExpr *C = Result.Nodes.getNodeAs<CallExpr>(
"init_call_expr");
55 bool WarnOnly =
false;
58 BeginLoc = C->getBeginLoc();
59 EndLoc = C->getEndLoc();
60 }
else if (
const auto *
E = Result.Nodes.getNodeAs<CallExpr>(
"call_expr")) {
61 BeginLoc =
E->getBeginLoc();
62 EndLoc =
E->getEndLoc();
63 }
else if (
const auto *D =
64 Result.Nodes.getNodeAs<DeclRefExpr>(
"decl_ref_expr")) {
65 BeginLoc = D->getBeginLoc();
66 EndLoc = D->getEndLoc();
69 const auto *U = Result.Nodes.getNodeAs<UsingDecl>(
"using_decl");
70 assert(U &&
"Null pointer, no node provided");
71 BeginLoc = U->getNameInfo().getBeginLoc();
72 EndLoc = U->getNameInfo().getEndLoc();
75 auto Diag = diag(BeginLoc,
"'std::uncaught_exception' is deprecated, use "
76 "'std::uncaught_exceptions' instead");
78 if (!BeginLoc.isMacroID()) {
80 Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
81 *Result.SourceManager, getLangOpts());
83 Text.consume_back(
"()");
84 int TextLength =
Text.size();
91 Diag << FixItHint::CreateInsertion(BeginLoc.getLocWithOffset(TextLength),
94 Diag << FixItHint::CreateReplacement(C->getSourceRange(),
95 "std::uncaught_exceptions() > 0");