ExceptionEscapeCheck.cpp 2.9 KB

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