summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
blob: e528ae92df85221667db124d408771ebbf4c82ba (plain)
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
//===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ExceptionBaseclassCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

#include <iostream>

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace hicpp {

void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
  if (!getLangOpts().CPlusPlus)
    return;

  Finder->addMatcher(
      cxxThrowExpr(
          allOf(
              has(expr(unless(hasType(cxxRecordDecl(
                  isSameOrDerivedFrom(hasName("std::exception"))))))),
              eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything())))
          .bind("bad_throw"),
      this);
}

void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
  diag(BadThrow->getLocStart(),
       "throwing an exception whose type is not derived from 'std::exception'")
      << BadThrow->getSourceRange();

  const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl");
  if (TypeDecl != nullptr)
    diag(TypeDecl->getLocStart(), "type defined here", DiagnosticIDs::Note);
}

} // namespace hicpp
} // namespace tidy
} // namespace clang
OpenPOWER on IntegriCloud