clang-tools  10.0.0git
UseNoexceptCheck.cpp
Go to the documentation of this file.
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 UseNoexceptCheck::UseNoexceptCheck(StringRef Name, ClangTidyContext *Context)
20  : ClangTidyCheck(Name, Context),
21  NoexceptMacro(Options.get("ReplacementString", "")),
22  UseNoexceptFalse(Options.get("UseNoexceptFalse", true)) {}
23 
25  Options.store(Opts, "ReplacementString", NoexceptMacro);
26  Options.store(Opts, "UseNoexceptFalse", UseNoexceptFalse);
27 }
28 
29 void UseNoexceptCheck::registerMatchers(MatchFinder *Finder) {
30  if (!getLangOpts().CPlusPlus11)
31  return;
32 
33  Finder->addMatcher(
34  functionDecl(
35  cxxMethodDecl(
36  hasTypeLoc(loc(functionProtoType(hasDynamicExceptionSpec()))),
37  anyOf(hasOverloadedOperatorName("delete[]"),
38  hasOverloadedOperatorName("delete"), cxxDestructorDecl()))
39  .bind("del-dtor"))
40  .bind("funcDecl"),
41  this);
42 
43  Finder->addMatcher(
44  functionDecl(
45  hasTypeLoc(loc(functionProtoType(hasDynamicExceptionSpec()))),
46  unless(anyOf(hasOverloadedOperatorName("delete[]"),
47  hasOverloadedOperatorName("delete"),
48  cxxDestructorDecl())))
49  .bind("funcDecl"),
50  this);
51 
52  Finder->addMatcher(
53  parmVarDecl(anyOf(hasType(pointerType(pointee(parenType(innerType(
54  functionProtoType(hasDynamicExceptionSpec())))))),
55  hasType(memberPointerType(pointee(parenType(innerType(
56  functionProtoType(hasDynamicExceptionSpec()))))))))
57  .bind("parmVarDecl"),
58  this);
59 }
60 
61 void UseNoexceptCheck::check(const MatchFinder::MatchResult &Result) {
62  const FunctionProtoType *FnTy = nullptr;
63  bool DtorOrOperatorDel = false;
64  SourceRange Range;
65 
66  if (const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl")) {
67  DtorOrOperatorDel = Result.Nodes.getNodeAs<FunctionDecl>("del-dtor");
68  FnTy = FuncDecl->getType()->getAs<FunctionProtoType>();
69  if (const auto *TSI = FuncDecl->getTypeSourceInfo())
70  Range =
71  TSI->getTypeLoc().castAs<FunctionTypeLoc>().getExceptionSpecRange();
72  } else if (const auto *ParmDecl =
73  Result.Nodes.getNodeAs<ParmVarDecl>("parmVarDecl")) {
74  FnTy = ParmDecl->getType()
75  ->getAs<Type>()
76  ->getPointeeType()
77  ->getAs<FunctionProtoType>();
78 
79  if (const auto *TSI = ParmDecl->getTypeSourceInfo())
80  Range = TSI->getTypeLoc()
81  .getNextTypeLoc()
82  .IgnoreParens()
83  .castAs<FunctionProtoTypeLoc>()
84  .getExceptionSpecRange();
85  }
86  CharSourceRange CRange = Lexer::makeFileCharRange(
87  CharSourceRange::getTokenRange(Range), *Result.SourceManager,
88  Result.Context->getLangOpts());
89 
90  assert(FnTy && "FunctionProtoType is null.");
91  bool IsNoThrow = FnTy->isNothrow();
92  StringRef ReplacementStr =
93  IsNoThrow
94  ? NoexceptMacro.empty() ? "noexcept" : NoexceptMacro.c_str()
95  : NoexceptMacro.empty()
96  ? (DtorOrOperatorDel || UseNoexceptFalse) ? "noexcept(false)"
97  : ""
98  : "";
99 
100  FixItHint FixIt;
101  if ((IsNoThrow || NoexceptMacro.empty()) && CRange.isValid())
102  FixIt = FixItHint::CreateReplacement(CRange, ReplacementStr);
103 
104  diag(Range.getBegin(), "dynamic exception specification '%0' is deprecated; "
105  "consider %select{using '%2'|removing it}1 instead")
106  << Lexer::getSourceText(CRange, *Result.SourceManager,
107  Result.Context->getLangOpts())
108  << ReplacementStr.empty() << ReplacementStr << FixIt;
109 }
110 
111 } // namespace modernize
112 } // namespace tidy
113 } // namespace clang
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< FixItHint > FixIt
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
NodeType Type
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.