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