1 //===--- UseNoexceptCheck.cpp - clang-tidy---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "UseNoexceptCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/Lex/Lexer.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang {
16 namespace tidy {
17 namespace modernize {
18
19 namespace {
AST_MATCHER(NamedDecl,isValid)20 AST_MATCHER(NamedDecl, isValid) { return !Node.isInvalidDecl(); }
21 } // namespace
22
UseNoexceptCheck(StringRef Name,ClangTidyContext * Context)23 UseNoexceptCheck::UseNoexceptCheck(StringRef Name, ClangTidyContext *Context)
24 : ClangTidyCheck(Name, Context),
25 NoexceptMacro(Options.get("ReplacementString", "")),
26 UseNoexceptFalse(Options.get("UseNoexceptFalse", true)) {}
27
storeOptions(ClangTidyOptions::OptionMap & Opts)28 void UseNoexceptCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
29 Options.store(Opts, "ReplacementString", NoexceptMacro);
30 Options.store(Opts, "UseNoexceptFalse", UseNoexceptFalse);
31 }
32
registerMatchers(MatchFinder * Finder)33 void UseNoexceptCheck::registerMatchers(MatchFinder *Finder) {
34 Finder->addMatcher(
35 functionDecl(
36 isValid(),
37 hasTypeLoc(loc(functionProtoType(hasDynamicExceptionSpec()))),
38 optionally(cxxMethodDecl(anyOf(hasAnyOverloadedOperatorName(
39 "delete[]", "delete"),
40 cxxDestructorDecl()))
41 .bind("del-dtor")))
42 .bind("funcDecl"),
43 this);
44
45 Finder->addMatcher(
46 parmVarDecl(anyOf(hasType(pointerType(pointee(parenType(innerType(
47 functionProtoType(hasDynamicExceptionSpec())))))),
48 hasType(memberPointerType(pointee(parenType(innerType(
49 functionProtoType(hasDynamicExceptionSpec()))))))))
50 .bind("parmVarDecl"),
51 this);
52 }
53
check(const MatchFinder::MatchResult & Result)54 void UseNoexceptCheck::check(const MatchFinder::MatchResult &Result) {
55 const FunctionProtoType *FnTy = nullptr;
56 bool DtorOrOperatorDel = false;
57 SourceRange Range;
58
59 if (const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl")) {
60 DtorOrOperatorDel = Result.Nodes.getNodeAs<FunctionDecl>("del-dtor");
61 FnTy = FuncDecl->getType()->getAs<FunctionProtoType>();
62 if (const auto *TSI = FuncDecl->getTypeSourceInfo())
63 Range =
64 TSI->getTypeLoc().castAs<FunctionTypeLoc>().getExceptionSpecRange();
65 } else if (const auto *ParmDecl =
66 Result.Nodes.getNodeAs<ParmVarDecl>("parmVarDecl")) {
67 FnTy = ParmDecl->getType()
68 ->getAs<Type>()
69 ->getPointeeType()
70 ->getAs<FunctionProtoType>();
71
72 if (const auto *TSI = ParmDecl->getTypeSourceInfo())
73 Range = TSI->getTypeLoc()
74 .getNextTypeLoc()
75 .IgnoreParens()
76 .castAs<FunctionProtoTypeLoc>()
77 .getExceptionSpecRange();
78 }
79
80 assert(FnTy && "FunctionProtoType is null.");
81 if (isUnresolvedExceptionSpec(FnTy->getExceptionSpecType()))
82 return;
83
84 assert(Range.isValid() && "Exception Source Range is invalid.");
85
86 CharSourceRange CRange = Lexer::makeFileCharRange(
87 CharSourceRange::getTokenRange(Range), *Result.SourceManager,
88 Result.Context->getLangOpts());
89
90 bool IsNoThrow = FnTy->isNothrow();
91 StringRef ReplacementStr =
92 IsNoThrow
93 ? NoexceptMacro.empty() ? "noexcept" : NoexceptMacro.c_str()
94 : NoexceptMacro.empty()
95 ? (DtorOrOperatorDel || UseNoexceptFalse) ? "noexcept(false)"
96 : ""
97 : "";
98
99 FixItHint FixIt;
100 if ((IsNoThrow || NoexceptMacro.empty()) && CRange.isValid())
101 FixIt = FixItHint::CreateReplacement(CRange, ReplacementStr);
102
103 diag(Range.getBegin(), "dynamic exception specification '%0' is deprecated; "
104 "consider %select{using '%2'|removing it}1 instead")
105 << Lexer::getSourceText(CRange, *Result.SourceManager,
106 Result.Context->getLangOpts())
107 << ReplacementStr.empty() << ReplacementStr << FixIt;
108 }
109
110 } // namespace modernize
111 } // namespace tidy
112 } // namespace clang
113