ThrowByValueCatchByReferenceCheck.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===--- ThrowByValueCatchByReferenceCheck.h - clang-tidy--------*- C++ -*-===//
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::misc {
  12. ///checks for locations that do not throw by value
  13. // or catch by reference.
  14. // The check is C++ only. It checks that all throw locations
  15. // throw by value and not by pointer. Additionally it
  16. // contains an option ("CheckThrowTemporaries", default value "true") that
  17. // checks that thrown objects are anonymous temporaries. It is also
  18. // acceptable for this check to throw string literals.
  19. // This test checks that exceptions are caught by reference
  20. // and not by value or pointer. It will not warn when catching
  21. // pointer to char, wchar_t, char16_t or char32_t. This is
  22. // due to not warning on throwing string literals.
  23. class ThrowByValueCatchByReferenceCheck : public ClangTidyCheck {
  24. public:
  25. ThrowByValueCatchByReferenceCheck(StringRef Name, ClangTidyContext *Context);
  26. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  27. return LangOpts.CPlusPlus;
  28. }
  29. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  30. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  31. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  32. private:
  33. void diagnoseThrowLocations(const CXXThrowExpr *ThrowExpr);
  34. void diagnoseCatchLocations(const CXXCatchStmt *CatchStmt,
  35. ASTContext &Context);
  36. bool isFunctionParameter(const DeclRefExpr *DeclRefExpr);
  37. bool isCatchVariable(const DeclRefExpr *DeclRefExpr);
  38. bool isFunctionOrCatchVar(const DeclRefExpr *DeclRefExpr);
  39. const bool CheckAnonymousTemporaries;
  40. const bool WarnOnLargeObject;
  41. const uint64_t MaxSizeOptions; // The raw value read from the options.
  42. uint64_t MaxSize; // No `const` because we have to set it in two steps.
  43. };
  44. } // namespace clang::tidy::misc
  45. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H