• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- OwningMemoryCheck.h - clang-tidy------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace cppcoreguidelines {
17 
18 /// Checks for common use cases for gsl::owner and enforces the unique owner
19 /// nature of it whenever possible.
20 ///
21 /// For the user-facing documentation see:
22 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-owning-memory.html
23 class OwningMemoryCheck : public ClangTidyCheck {
24 public:
OwningMemoryCheck(StringRef Name,ClangTidyContext * Context)25   OwningMemoryCheck(StringRef Name, ClangTidyContext *Context)
26       : ClangTidyCheck(Name, Context),
27         LegacyResourceProducers(Options.get(
28             "LegacyResourceProducers", "::malloc;::aligned_alloc;::realloc;"
29                                        "::calloc;::fopen;::freopen;::tmpfile")),
30         LegacyResourceConsumers(Options.get(
31             "LegacyResourceConsumers", "::free;::realloc;::freopen;::fclose")) {
32   }
isLanguageVersionSupported(const LangOptions & LangOpts)33   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
34     return LangOpts.CPlusPlus11;
35   }
36 
37   /// Make configuration of checker discoverable.
38   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
39 
40   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
41   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
42 
43 private:
44   bool handleDeletion(const ast_matchers::BoundNodes &Nodes);
45   bool handleLegacyConsumers(const ast_matchers::BoundNodes &Nodes);
46   bool handleExpectedOwner(const ast_matchers::BoundNodes &Nodes);
47   bool handleAssignmentAndInit(const ast_matchers::BoundNodes &Nodes);
48   bool handleAssignmentFromNewOwner(const ast_matchers::BoundNodes &Nodes);
49   bool handleReturnValues(const ast_matchers::BoundNodes &Nodes);
50   bool handleOwnerMembers(const ast_matchers::BoundNodes &Nodes);
51 
52   /// List of old C-style functions that create resources.
53   /// Defaults to
54   /// `::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile`.
55   const std::string LegacyResourceProducers;
56   /// List of old C-style functions that consume or release resources.
57   /// Defaults to `::free;::realloc;::freopen;::fclose`.
58   const std::string LegacyResourceConsumers;
59 };
60 
61 } // namespace cppcoreguidelines
62 } // namespace tidy
63 } // namespace clang
64 
65 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
66