ExceptionEscapeCheck.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/ASTMatchers/ASTMatchFinder.h"
  11. #include "llvm/ADT/SmallSet.h"
  12. #include "llvm/ADT/StringSet.h"
  13. using namespace clang::ast_matchers;
  14. namespace clang {
  15. namespace {
  16. AST_MATCHER_P(FunctionDecl, isEnabled, llvm::StringSet<>,
  17. FunctionsThatShouldNotThrow) {
  18. return FunctionsThatShouldNotThrow.count(Node.getNameAsString()) > 0;
  19. }
  20. } // namespace
  21. namespace tidy::bugprone {
  22. ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
  23. ClangTidyContext *Context)
  24. : ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
  25. "FunctionsThatShouldNotThrow", "")),
  26. RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
  27. llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
  28. IgnoredExceptionsVec;
  29. StringRef(RawFunctionsThatShouldNotThrow)
  30. .split(FunctionsThatShouldNotThrowVec, ",", -1, false);
  31. FunctionsThatShouldNotThrow.insert(FunctionsThatShouldNotThrowVec.begin(),
  32. FunctionsThatShouldNotThrowVec.end());
  33. llvm::StringSet<> IgnoredExceptions;
  34. StringRef(RawIgnoredExceptions).split(IgnoredExceptionsVec, ",", -1, false);
  35. IgnoredExceptions.insert(IgnoredExceptionsVec.begin(),
  36. IgnoredExceptionsVec.end());
  37. Tracer.ignoreExceptions(std::move(IgnoredExceptions));
  38. Tracer.ignoreBadAlloc(true);
  39. }
  40. void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  41. Options.store(Opts, "FunctionsThatShouldNotThrow",
  42. RawFunctionsThatShouldNotThrow);
  43. Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
  44. }
  45. void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
  46. Finder->addMatcher(
  47. functionDecl(anyOf(isNoThrow(), cxxDestructorDecl(),
  48. cxxConstructorDecl(isMoveConstructor()),
  49. cxxMethodDecl(isMoveAssignmentOperator()),
  50. hasName("main"), hasName("swap"),
  51. isEnabled(FunctionsThatShouldNotThrow)))
  52. .bind("thrower"),
  53. this);
  54. }
  55. void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
  56. const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");
  57. if (!MatchedDecl)
  58. return;
  59. if (Tracer.analyze(MatchedDecl).getBehaviour() ==
  60. utils::ExceptionAnalyzer::State::Throwing)
  61. // FIXME: We should provide more information about the exact location where
  62. // the exception is thrown, maybe the full path the exception escapes
  63. diag(MatchedDecl->getLocation(), "an exception may be thrown in function "
  64. "%0 which should not throw exceptions")
  65. << MatchedDecl;
  66. }
  67. } // namespace tidy::bugprone
  68. } // namespace clang