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