2019-01-31 18:46:37 +08:00
|
|
|
//===--- ExceptionEscapeCheck.cpp - clang-tidy ----------------------------===//
|
2018-07-13 21:09:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2018-07-13 21:09:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ExceptionEscapeCheck.h"
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
2019-01-31 18:46:37 +08:00
|
|
|
namespace {
|
2018-07-13 21:09:40 +08:00
|
|
|
AST_MATCHER_P(FunctionDecl, isEnabled, llvm::StringSet<>,
|
|
|
|
FunctionsThatShouldNotThrow) {
|
|
|
|
return FunctionsThatShouldNotThrow.count(Node.getNameAsString()) > 0;
|
|
|
|
}
|
2019-01-31 18:46:37 +08:00
|
|
|
} // namespace
|
2018-07-13 21:09:40 +08:00
|
|
|
|
|
|
|
namespace tidy {
|
|
|
|
namespace bugprone {
|
|
|
|
ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
|
|
|
|
"FunctionsThatShouldNotThrow", "")),
|
|
|
|
RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
|
|
|
|
llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
|
|
|
|
IgnoredExceptionsVec;
|
|
|
|
StringRef(RawFunctionsThatShouldNotThrow)
|
|
|
|
.split(FunctionsThatShouldNotThrowVec, ",", -1, false);
|
|
|
|
FunctionsThatShouldNotThrow.insert(FunctionsThatShouldNotThrowVec.begin(),
|
|
|
|
FunctionsThatShouldNotThrowVec.end());
|
2019-01-31 18:46:37 +08:00
|
|
|
|
|
|
|
llvm::StringSet<> IgnoredExceptions;
|
2018-07-13 21:09:40 +08:00
|
|
|
StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false);
|
|
|
|
IgnoredExceptions.insert(IgnoredExceptionsVec.begin(),
|
|
|
|
IgnoredExceptionsVec.end());
|
2019-01-31 18:46:37 +08:00
|
|
|
Tracer.ignoreExceptions(std::move(IgnoredExceptions));
|
[clang-tidy] refactor ExceptionAnalyzer further to give ternary answer
Summary:
The analsis on the throwing behvaiour on functions and statements gave only
a binary answer whether an exception could occur and if yes which types are
thrown.
This refactoring allows keeping track if there is a unknown factor, because the
code calls to some functions with unavailable source code with no `noexcept`
information.
This 'potential Unknown' information is propagated properly and can be queried
separately.
Reviewers: lebedev.ri, aaron.ballman, baloghadamsoftware, alexfh
Reviewed By: lebedev.ri, baloghadamsoftware
Subscribers: xazax.hun, rnkovacs, a.sidorin, Szelethus, donat.nagy, dkrupp, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D57883
llvm-svn: 354517
2019-02-21 05:04:36 +08:00
|
|
|
Tracer.ignoreBadAlloc(true);
|
2018-07-13 21:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "FunctionsThatShouldNotThrow",
|
|
|
|
RawFunctionsThatShouldNotThrow);
|
|
|
|
Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
|
2018-10-04 23:49:25 +08:00
|
|
|
if (!getLangOpts().CPlusPlus || !getLangOpts().CXXExceptions)
|
|
|
|
return;
|
|
|
|
|
2018-07-13 21:09:40 +08:00
|
|
|
Finder->addMatcher(
|
2018-11-25 10:41:01 +08:00
|
|
|
functionDecl(anyOf(isNoThrow(), cxxDestructorDecl(),
|
|
|
|
cxxConstructorDecl(isMoveConstructor()),
|
|
|
|
cxxMethodDecl(isMoveAssignmentOperator()),
|
|
|
|
hasName("main"), hasName("swap"),
|
2019-01-31 18:46:37 +08:00
|
|
|
isEnabled(FunctionsThatShouldNotThrow)))
|
2018-07-13 21:09:40 +08:00
|
|
|
.bind("thrower"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
|
2019-01-31 18:46:37 +08:00
|
|
|
const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");
|
|
|
|
|
2018-07-13 21:09:40 +08:00
|
|
|
if (!MatchedDecl)
|
|
|
|
return;
|
|
|
|
|
[clang-tidy] refactor ExceptionAnalyzer further to give ternary answer
Summary:
The analsis on the throwing behvaiour on functions and statements gave only
a binary answer whether an exception could occur and if yes which types are
thrown.
This refactoring allows keeping track if there is a unknown factor, because the
code calls to some functions with unavailable source code with no `noexcept`
information.
This 'potential Unknown' information is propagated properly and can be queried
separately.
Reviewers: lebedev.ri, aaron.ballman, baloghadamsoftware, alexfh
Reviewed By: lebedev.ri, baloghadamsoftware
Subscribers: xazax.hun, rnkovacs, a.sidorin, Szelethus, donat.nagy, dkrupp, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D57883
llvm-svn: 354517
2019-02-21 05:04:36 +08:00
|
|
|
if (Tracer.analyze(MatchedDecl).getBehaviour() ==
|
|
|
|
utils::ExceptionAnalyzer::State::Throwing)
|
2019-01-31 18:46:37 +08:00
|
|
|
// FIXME: We should provide more information about the exact location where
|
|
|
|
// the exception is thrown, maybe the full path the exception escapes
|
|
|
|
diag(MatchedDecl->getLocation(),
|
|
|
|
"an exception may be thrown in function %0 "
|
|
|
|
|
|
|
|
"which should not throw exceptions")
|
|
|
|
<< MatchedDecl;
|
2018-07-13 21:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bugprone
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|