blob: 04783cff8cd34da1f73f35c09283a71f0ab426d2 [file] [log] [blame]
Benjamin Kramerda3658e2014-07-23 11:49:46 +00001//===--- 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
15namespace clang {
16namespace tidy {
Alexander Kornienko2b3124202015-03-02 12:25:03 +000017namespace misc {
Benjamin Kramerda3658e2014-07-23 11:49:46 +000018
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.
39class UnusedRAIICheck : public ClangTidyCheck {
40public:
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000041 UnusedRAIICheck(StringRef Name, ClangTidyContext *Context)
42 : ClangTidyCheck(Name, Context) {}
Benjamin Kramerda3658e2014-07-23 11:49:46 +000043 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
44 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
45};
46
Alexander Kornienko2b3124202015-03-02 12:25:03 +000047} // namespace misc
Benjamin Kramerda3658e2014-07-23 11:49:46 +000048} // namespace tidy
49} // namespace clang
50
51#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_RAII_H