Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:19

0001 //===--- OwningMemoryCheck.h - clang-tidy------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::cppcoreguidelines {
0015 
0016 /// Checks for common use cases for gsl::owner and enforces the unique owner
0017 /// nature of it whenever possible.
0018 ///
0019 /// For the user-facing documentation see:
0020 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/owning-memory.html
0021 class OwningMemoryCheck : public ClangTidyCheck {
0022 public:
0023   OwningMemoryCheck(StringRef Name, ClangTidyContext *Context)
0024       : ClangTidyCheck(Name, Context),
0025         LegacyResourceProducers(Options.get(
0026             "LegacyResourceProducers", "::malloc;::aligned_alloc;::realloc;"
0027                                        "::calloc;::fopen;::freopen;::tmpfile")),
0028         LegacyResourceConsumers(Options.get(
0029             "LegacyResourceConsumers", "::free;::realloc;::freopen;::fclose")) {
0030   }
0031   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
0032     return LangOpts.CPlusPlus11;
0033   }
0034 
0035   /// Make configuration of checker discoverable.
0036   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0037 
0038   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0039   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0040   std::optional<TraversalKind> getCheckTraversalKind() const override {
0041     return TK_IgnoreUnlessSpelledInSource;
0042   }
0043 
0044 private:
0045   bool handleDeletion(const ast_matchers::BoundNodes &Nodes);
0046   bool handleLegacyConsumers(const ast_matchers::BoundNodes &Nodes);
0047   bool handleExpectedOwner(const ast_matchers::BoundNodes &Nodes);
0048   bool handleAssignmentAndInit(const ast_matchers::BoundNodes &Nodes);
0049   bool handleAssignmentFromNewOwner(const ast_matchers::BoundNodes &Nodes);
0050   bool handleReturnValues(const ast_matchers::BoundNodes &Nodes);
0051   bool handleOwnerMembers(const ast_matchers::BoundNodes &Nodes);
0052 
0053   /// List of old C-style functions that create resources.
0054   /// Defaults to
0055   /// `::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile`.
0056   const StringRef LegacyResourceProducers;
0057   /// List of old C-style functions that consume or release resources.
0058   /// Defaults to `::free;::realloc;::freopen;::fclose`.
0059   const StringRef LegacyResourceConsumers;
0060 };
0061 
0062 } // namespace clang::tidy::cppcoreguidelines
0063 
0064 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H