mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-19 03:25:54 -04:00

to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
67 lines
2.7 KiB
C++
67 lines
2.7 KiB
C++
//===--- ExceptionBaseclassCheck.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 "ExceptionBaseclassCheck.h"
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
namespace clang {
|
|
namespace tidy {
|
|
namespace hicpp {
|
|
|
|
void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
|
|
if (!getLangOpts().CPlusPlus)
|
|
return;
|
|
|
|
Finder->addMatcher(
|
|
cxxThrowExpr(
|
|
unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
|
|
// The thrown value is not derived from 'std::exception'.
|
|
has(expr(unless(
|
|
hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
|
|
isSameOrDerivedFrom(hasName("::std::exception")))))))))),
|
|
// This condition is always true, but will bind to the
|
|
// template value if the thrown type is templated.
|
|
anyOf(has(expr(
|
|
hasType(substTemplateTypeParmType().bind("templ_type")))),
|
|
anything()),
|
|
// Bind to the declaration of the type of the value that
|
|
// is thrown. 'anything()' is necessary to always suceed
|
|
// in the 'eachOf' because builtin types are not
|
|
// 'namedDecl'.
|
|
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");
|
|
assert(BadThrow && "Did not match the throw expression");
|
|
|
|
diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
|
|
"type %0 is not derived from "
|
|
"'std::exception'")
|
|
<< BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
|
|
|
|
if (const auto *Template =
|
|
Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
|
|
diag(BadThrow->getSubExpr()->getBeginLoc(),
|
|
"type %0 is a template instantiation of %1", DiagnosticIDs::Note)
|
|
<< BadThrow->getSubExpr()->getType()
|
|
<< Template->getReplacedParameter()->getDecl();
|
|
|
|
if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
|
|
diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
|
|
}
|
|
|
|
} // namespace hicpp
|
|
} // namespace tidy
|
|
} // namespace clang
|