Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame] | 1 | //===--- UnusedRAIICheck.h - clang-tidy -------------------------*- C++ -*-===// |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 6658055 | 2015-03-09 16:52:33 +0000 | [diff] [blame^] | 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSEDRAIICHECK_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSEDRAIICHECK_H |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 12 | |
| 13 | #include "../ClangTidy.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 17 | namespace misc { |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 18 | |
| 19 | /// \brief Finds temporaries that look like RAII objects. |
| 20 | /// |
| 21 | /// The canonical example for this is a scoped lock. |
| 22 | /// \code |
| 23 | /// { |
| 24 | /// scoped_lock(&global_mutex); |
| 25 | /// critical_section(); |
| 26 | /// } |
| 27 | /// \endcode |
| 28 | /// The destructor of the scoped_lock is called before the critical_section is |
| 29 | /// entered, leaving it unprotected. |
| 30 | /// |
| 31 | /// We apply a number of heuristics to reduce the false positive count of this |
| 32 | /// check: |
| 33 | /// - Ignore code expanded from macros. Testing frameworks make heavy use of |
| 34 | /// this. |
| 35 | /// - Ignore types with no user-declared constructor. Those are very unlikely |
| 36 | /// to be RAII objects. |
| 37 | /// - Ignore objects at the end of a compound statement (doesn't change behavior). |
| 38 | /// - Ignore objects returned from a call. |
| 39 | class UnusedRAIICheck : public ClangTidyCheck { |
| 40 | public: |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 41 | UnusedRAIICheck(StringRef Name, ClangTidyContext *Context) |
| 42 | : ClangTidyCheck(Name, Context) {} |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 43 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 44 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 45 | }; |
| 46 | |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 47 | } // namespace misc |
Benjamin Kramer | da3658e | 2014-07-23 11:49:46 +0000 | [diff] [blame] | 48 | } // namespace tidy |
| 49 | } // namespace clang |
| 50 | |
Alexander Kornienko | 6658055 | 2015-03-09 16:52:33 +0000 | [diff] [blame^] | 51 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSEDRAIICHECK_H |