clang-tools 10.0.0

openmp/ExceptionEscapeCheck.cpp
Go to the documentation of this file.
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
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
41 void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
42 Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
43 }
44
45 void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
46 // Don't register the check if OpenMP is not enabled; the OpenMP pragmas are
47 // completely ignored then, so no OpenMP entires will be present in the AST.
48 if (!getLangOpts().OpenMP)
49 return;
50 // Similarly, if C++ Exceptions are not enabled, nothing to do.
51 if (!getLangOpts().CPlusPlus || !getLangOpts().CXXExceptions)
52 return;
53
54 Finder->addMatcher(ompExecutableDirective(
55 unless(isStandaloneDirective()),
56 hasStructuredBlock(stmt().bind("structured-block")))
57 .bind("directive"),
58 this);
59 }
60
61 void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
62 const auto *Directive =
63 Result.Nodes.getNodeAs<OMPExecutableDirective>("directive");
64 assert(Directive && "Expected to match some OpenMP Executable directive.");
65 const auto *StructuredBlock =
66 Result.Nodes.getNodeAs<Stmt>("structured-block");
67 assert(StructuredBlock && "Expected to get some OpenMP Structured Block.");
68
69 if (Tracer.analyze(StructuredBlock).getBehaviour() !=
70 utils::ExceptionAnalyzer::State::Throwing)
71 return; // No exceptions have been proven to escape out of the struc. block.
72
73 // FIXME: We should provide more information about the exact location where
74 // the exception is thrown, maybe the full path the exception escapes.
75
76 diag(StructuredBlock->getBeginLoc(),
77 "an exception thrown inside of the OpenMP '%0' region is not caught in "
78 "that same region")
79 << getOpenMPDirectiveName(Directive->getDirectiveKind());
80 }
81
82 } // namespace openmp
83 } // namespace tidy
84 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::utils::ExceptionAnalyzer::ExceptionInfo::getBehaviour
State getBehaviour() const
Definition: ExceptionAnalyzer.h:61
clang::tidy::openmp::ExceptionEscapeCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::tidy::utils::ExceptionAnalyzer::State::Throwing
The function can definitely throw given an AST.
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::ExceptionAnalyzer::analyze
ExceptionInfo analyze(const FunctionDecl *Func)
Definition: ExceptionAnalyzer.cpp:250
clang::tidy::openmp::ExceptionEscapeCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
clang::tidy::utils::ExceptionAnalyzer::ignoreBadAlloc
void ignoreBadAlloc(bool ShallIgnore)
Definition: ExceptionAnalyzer.h:126
clang::tidy::openmp::ExceptionEscapeCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: openmp/ExceptionEscapeCheck.cpp:41
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::utils::ExceptionAnalyzer::ignoreExceptions
void ignoreExceptions(llvm::StringSet<> ExceptionNames)
Definition: ExceptionAnalyzer.h:127
ExceptionEscapeCheck.h