xref: /llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h (revision 5b5b75bfd4d469d56db7980c578214c26288faa9)
16ccc1c34SJonas Toth //===--- OwningMemoryCheck.h - clang-tidy------------------------*- C++ -*-===//
26ccc1c34SJonas Toth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66ccc1c34SJonas Toth //
76ccc1c34SJonas Toth //===----------------------------------------------------------------------===//
86ccc1c34SJonas Toth 
96ccc1c34SJonas Toth #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
106ccc1c34SJonas Toth #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
116ccc1c34SJonas Toth 
12860aefd0SNathan James #include "../ClangTidyCheck.h"
136ccc1c34SJonas Toth 
144718da50SCarlos Galvez namespace clang::tidy::cppcoreguidelines {
156ccc1c34SJonas Toth 
166ccc1c34SJonas Toth /// Checks for common use cases for gsl::owner and enforces the unique owner
176ccc1c34SJonas Toth /// nature of it whenever possible.
186ccc1c34SJonas Toth ///
196ccc1c34SJonas Toth /// For the user-facing documentation see:
206e566bc5SRichard /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/owning-memory.html
216ccc1c34SJonas Toth class OwningMemoryCheck : public ClangTidyCheck {
226ccc1c34SJonas Toth public:
OwningMemoryCheck(StringRef Name,ClangTidyContext * Context)236ccc1c34SJonas Toth   OwningMemoryCheck(StringRef Name, ClangTidyContext *Context)
24c9aea86eSJonas Toth       : ClangTidyCheck(Name, Context),
25c9aea86eSJonas Toth         LegacyResourceProducers(Options.get(
26c9aea86eSJonas Toth             "LegacyResourceProducers", "::malloc;::aligned_alloc;::realloc;"
27c9aea86eSJonas Toth                                        "::calloc;::fopen;::freopen;::tmpfile")),
28c9aea86eSJonas Toth         LegacyResourceConsumers(Options.get(
29c9aea86eSJonas Toth             "LegacyResourceConsumers", "::free;::realloc;::freopen;::fclose")) {
30c9aea86eSJonas Toth   }
isLanguageVersionSupported(const LangOptions & LangOpts)31e40a742aSNathan James   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
32e40a742aSNathan James     return LangOpts.CPlusPlus11;
33e40a742aSNathan James   }
34c9aea86eSJonas Toth 
35c9aea86eSJonas Toth   /// Make configuration of checker discoverable.
36c9aea86eSJonas Toth   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
37c9aea86eSJonas Toth 
386ccc1c34SJonas Toth   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
396ccc1c34SJonas Toth   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
getCheckTraversalKind()40*5b5b75bfSPiotr Zegar   std::optional<TraversalKind> getCheckTraversalKind() const override {
41*5b5b75bfSPiotr Zegar     return TK_IgnoreUnlessSpelledInSource;
42*5b5b75bfSPiotr Zegar   }
436ccc1c34SJonas Toth 
446ccc1c34SJonas Toth private:
456ccc1c34SJonas Toth   bool handleDeletion(const ast_matchers::BoundNodes &Nodes);
46c9aea86eSJonas Toth   bool handleLegacyConsumers(const ast_matchers::BoundNodes &Nodes);
476ccc1c34SJonas Toth   bool handleExpectedOwner(const ast_matchers::BoundNodes &Nodes);
486ccc1c34SJonas Toth   bool handleAssignmentAndInit(const ast_matchers::BoundNodes &Nodes);
496ccc1c34SJonas Toth   bool handleAssignmentFromNewOwner(const ast_matchers::BoundNodes &Nodes);
506ccc1c34SJonas Toth   bool handleReturnValues(const ast_matchers::BoundNodes &Nodes);
516ccc1c34SJonas Toth   bool handleOwnerMembers(const ast_matchers::BoundNodes &Nodes);
52c9aea86eSJonas Toth 
53c9aea86eSJonas Toth   /// List of old C-style functions that create resources.
54c9aea86eSJonas Toth   /// Defaults to
55c9aea86eSJonas Toth   /// `::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile`.
5612cb5405SNathan James   const StringRef LegacyResourceProducers;
57c9aea86eSJonas Toth   /// List of old C-style functions that consume or release resources.
58c9aea86eSJonas Toth   /// Defaults to `::free;::realloc;::freopen;::fclose`.
5912cb5405SNathan James   const StringRef LegacyResourceConsumers;
606ccc1c34SJonas Toth };
616ccc1c34SJonas Toth 
624718da50SCarlos Galvez } // namespace clang::tidy::cppcoreguidelines
636ccc1c34SJonas Toth 
646ccc1c34SJonas Toth #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
65