| 1 |
//===--- ExceptionEscapeCheck.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 "ExceptionEscapeCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/AST/OpenMPClause.h" |
| 12 |
#include "clang/AST/Stmt.h" |
| 13 |
#include "clang/AST/StmtOpenMP.h" |
| 14 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 15 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 16 |
#include "clang/ASTMatchers/ASTMatchersMacros.h" |
| 17 |
|
| 18 |
using namespace clang::ast_matchers; |
| 19 |
|
| 20 |
namespace clang::tidy::openmp { |
| 21 |
|
| 22 |
ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name, |
| 23 |
ClangTidyContext *Context) |
| 24 |
: ClangTidyCheck(Name, Context), |
| 25 |
RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) { |
| 26 |
llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec, |
| 27 |
IgnoredExceptionsVec; |
| 28 |
|
| 29 |
llvm::StringSet<> IgnoredExceptions; |
| 30 |
StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false); |
| 31 |
llvm::transform(IgnoredExceptionsVec, IgnoredExceptionsVec.begin(), |
| 32 |
[](StringRef S) { return S.trim(); }); |
| 33 |
IgnoredExceptions.insert(IgnoredExceptionsVec.begin(), |
| 34 |
IgnoredExceptionsVec.end()); |
| 35 |
Tracer.ignoreExceptions(std::move(IgnoredExceptions)); |
| 36 |
Tracer.ignoreBadAlloc(true); |
| 37 |
} |
| 38 |
|
| 39 |
void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 40 |
Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions); |
| 41 |
} |
| 42 |
|
| 43 |
void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) { |
| 44 |
Finder->addMatcher(ompExecutableDirective( |
| 45 |
unless(isStandaloneDirective()), |
| 46 |
hasStructuredBlock(stmt().bind("structured-block"))) |
| 47 |
.bind("directive"), |
| 48 |
this); |
| 49 |
} |
| 50 |
|
| 51 |
void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) { |
| 52 |
const auto *Directive = |
| 53 |
Result.Nodes.getNodeAs<OMPExecutableDirective>("directive"); |
| 54 |
assert(Directive && "Expected to match some OpenMP Executable directive."); |
| 55 |
const auto *StructuredBlock = |
| 56 |
Result.Nodes.getNodeAs<Stmt>("structured-block"); |
| 57 |
assert(StructuredBlock && "Expected to get some OpenMP Structured Block."); |
| 58 |
|
| 59 |
if (Tracer.analyze(StructuredBlock).getBehaviour() != |
| 60 |
utils::ExceptionAnalyzer::State::Throwing) |
| 61 |
return; // No exceptions have been proven to escape out of the struc. block. |
| 62 |
|
| 63 |
// FIXME: We should provide more information about the exact location where |
| 64 |
// the exception is thrown, maybe the full path the exception escapes. |
| 65 |
|
| 66 |
diag(StructuredBlock->getBeginLoc(), |
| 67 |
"an exception thrown inside of the OpenMP '%0' region is not caught in " |
| 68 |
"that same region") |
| 69 |
<< getOpenMPDirectiveName(Directive->getDirectiveKind()); |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace clang::tidy::openmp |