Aaron Ballman | fd78cc8 | 2015-10-09 20:42:44 +0000 | [diff] [blame] | 1 | //===--- ThrowByValueCatchByReferenceCheck.h - clang-tidy--------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H |
| 12 | |
| 13 | #include "../ClangTidy.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 17 | namespace misc { |
Aaron Ballman | fd78cc8 | 2015-10-09 20:42:44 +0000 | [diff] [blame] | 18 | |
| 19 | ///\brief checks for locations that do not throw by value |
| 20 | // or catch by reference. |
| 21 | // The check is C++ only. It checks that all throw locations |
| 22 | // throw by value and not by pointer. Additionally it |
| 23 | // contains an option ("CheckThrowTemporaries", default value "true") that |
| 24 | // checks that thrown objects are anonymous temporaries. It is also |
| 25 | // acceptable for this check to throw string literals. |
| 26 | // This test checks that exceptions are caught by reference |
| 27 | // and not by value or pointer. It will not warn when catching |
| 28 | // pointer to char, wchar_t, char16_t or char32_t. This is |
| 29 | // due to not warning on throwing string literals. |
| 30 | class ThrowByValueCatchByReferenceCheck : public ClangTidyCheck { |
| 31 | public: |
| 32 | ThrowByValueCatchByReferenceCheck(StringRef Name, ClangTidyContext *Context); |
| 33 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 34 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 35 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 36 | |
| 37 | private: |
| 38 | void diagnoseThrowLocations(const CXXThrowExpr *throwExpr); |
| 39 | void diagnoseCatchLocations(const CXXCatchStmt *catchStmt, |
| 40 | ASTContext &context); |
| 41 | bool isFunctionParameter(const DeclRefExpr *declRefExpr); |
| 42 | bool isCatchVariable(const DeclRefExpr *declRefExpr); |
| 43 | bool isFunctionOrCatchVar(const DeclRefExpr *declRefExpr); |
| 44 | const bool CheckAnonymousTemporaries; |
| 45 | }; |
| 46 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 47 | } // namespace misc |
Aaron Ballman | fd78cc8 | 2015-10-09 20:42:44 +0000 | [diff] [blame] | 48 | } // namespace tidy |
| 49 | } // namespace clang |
| 50 | |
| 51 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H |