UseUncaughtExceptionsCheck.cpp
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===--- UseUncaughtExceptionsCheck.cpp - clang-tidy--------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "UseUncaughtExceptionsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace modernize {
void UseUncaughtExceptionsCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus17)
return;
std::string MatchText = "::std::uncaught_exception";
// Using declaration: warning and fix-it.
Finder->addMatcher(
usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(hasName(MatchText))))
.bind("using_decl"),
this);
// DeclRefExpr: warning, no fix-it.
Finder->addMatcher(
declRefExpr(to(functionDecl(hasName(MatchText))), unless(callExpr()))
.bind("decl_ref_expr"),
this);
// CallExpr: warning, fix-it.
Finder->addMatcher(callExpr(hasDeclaration(functionDecl(hasName(MatchText))),
unless(hasAncestor(initListExpr())))
.bind("call_expr"),
this);
// CallExpr in initialisation list: warning, fix-it with avoiding narrowing
// conversions.
Finder->addMatcher(callExpr(hasAncestor(initListExpr()),
hasDeclaration(functionDecl(hasName(MatchText))))
.bind("init_call_expr"),
this);
}
void UseUncaughtExceptionsCheck::check(const MatchFinder::MatchResult &Result) {
SourceLocation BeginLoc;
SourceLocation EndLoc;
const CallExpr *C = Result.Nodes.getNodeAs<CallExpr>("init_call_expr");
bool WarnOnly = false;
if (C) {
BeginLoc = C->getBeginLoc();
EndLoc = C->getEndLoc();
} else if (const auto *E = Result.Nodes.getNodeAs<CallExpr>("call_expr")) {
BeginLoc = E->getBeginLoc();
EndLoc = E->getEndLoc();
} else if (const auto *D =
Result.Nodes.getNodeAs<DeclRefExpr>("decl_ref_expr")) {
BeginLoc = D->getBeginLoc();
EndLoc = D->getEndLoc();
WarnOnly = true;
} else {
const auto *U = Result.Nodes.getNodeAs<UsingDecl>("using_decl");
assert(U && "Null pointer, no node provided");
BeginLoc = U->getNameInfo().getBeginLoc();
EndLoc = U->getNameInfo().getEndLoc();
}
auto Diag = diag(BeginLoc, "'std::uncaught_exception' is deprecated, use "
"'std::uncaught_exceptions' instead");
if (!BeginLoc.isMacroID()) {
StringRef Text =
Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
*Result.SourceManager, getLangOpts());
Text.consume_back("()");
int TextLength = Text.size();
if (WarnOnly) {
return;
}
if (!C) {
Diag << FixItHint::CreateInsertion(BeginLoc.getLocWithOffset(TextLength),
"s");
} else {
Diag << FixItHint::CreateReplacement(C->getSourceRange(),
"std::uncaught_exceptions() > 0");
}
}
}
} // namespace modernize
} // namespace tidy
} // namespace clang