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