1 //===--- OwningMemoryCheck.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_CPPCOREGUIDELINES_OWNING_MEMORY_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
12 
13 #include "../ClangTidy.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace cppcoreguidelines {
18 
19 /// Checks for common use cases for gsl::owner and enforces the unique owner
20 /// nature of it whenever possible.
21 ///
22 /// For the user-facing documentation see:
23 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-owning-memory.html
24 class OwningMemoryCheck : public ClangTidyCheck {
25 public:
26   OwningMemoryCheck(StringRef Name, ClangTidyContext *Context)
27       : ClangTidyCheck(Name, Context),
28         LegacyResourceProducers(Options.get(
29             "LegacyResourceProducers", "::malloc;::aligned_alloc;::realloc;"
30                                        "::calloc;::fopen;::freopen;::tmpfile")),
31         LegacyResourceConsumers(Options.get(
32             "LegacyResourceConsumers", "::free;::realloc;::freopen;::fclose")) {
33   }
34 
35   /// Make configuration of checker discoverable.
36   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
37 
38   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
39   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
40 
41 private:
42   bool handleDeletion(const ast_matchers::BoundNodes &Nodes);
43   bool handleLegacyConsumers(const ast_matchers::BoundNodes &Nodes);
44   bool handleExpectedOwner(const ast_matchers::BoundNodes &Nodes);
45   bool handleAssignmentAndInit(const ast_matchers::BoundNodes &Nodes);
46   bool handleAssignmentFromNewOwner(const ast_matchers::BoundNodes &Nodes);
47   bool handleReturnValues(const ast_matchers::BoundNodes &Nodes);
48   bool handleOwnerMembers(const ast_matchers::BoundNodes &Nodes);
49 
50   /// List of old C-style functions that create resources.
51   /// Defaults to
52   /// `::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile`.
53   const std::string LegacyResourceProducers;
54   /// List of old C-style functions that consume or release resources.
55   /// Defaults to `::free;::realloc;::freopen;::fclose`.
56   const std::string LegacyResourceConsumers;
57 };
58 
59 } // namespace cppcoreguidelines
60 } // namespace tidy
61 } // namespace clang
62 
63 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
64