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::tidy::cppcoreguidelines { 15 16 /// Checks for common use cases for gsl::owner and enforces the unique owner 17 /// nature of it whenever possible. 18 /// 19 /// For the user-facing documentation see: 20 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/owning-memory.html 21 class OwningMemoryCheck : public ClangTidyCheck { 22 public: OwningMemoryCheck(StringRef Name,ClangTidyContext * Context)23 OwningMemoryCheck(StringRef Name, ClangTidyContext *Context) 24 : ClangTidyCheck(Name, Context), 25 LegacyResourceProducers(Options.get( 26 "LegacyResourceProducers", "::malloc;::aligned_alloc;::realloc;" 27 "::calloc;::fopen;::freopen;::tmpfile")), 28 LegacyResourceConsumers(Options.get( 29 "LegacyResourceConsumers", "::free;::realloc;::freopen;::fclose")) { 30 } isLanguageVersionSupported(const LangOptions & LangOpts)31 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { 32 return LangOpts.CPlusPlus11; 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; getCheckTraversalKind()40 std::optional<TraversalKind> getCheckTraversalKind() const override { 41 return TK_IgnoreUnlessSpelledInSource; 42 } 43 44 private: 45 bool handleDeletion(const ast_matchers::BoundNodes &Nodes); 46 bool handleLegacyConsumers(const ast_matchers::BoundNodes &Nodes); 47 bool handleExpectedOwner(const ast_matchers::BoundNodes &Nodes); 48 bool handleAssignmentAndInit(const ast_matchers::BoundNodes &Nodes); 49 bool handleAssignmentFromNewOwner(const ast_matchers::BoundNodes &Nodes); 50 bool handleReturnValues(const ast_matchers::BoundNodes &Nodes); 51 bool handleOwnerMembers(const ast_matchers::BoundNodes &Nodes); 52 53 /// List of old C-style functions that create resources. 54 /// Defaults to 55 /// `::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile`. 56 const StringRef LegacyResourceProducers; 57 /// List of old C-style functions that consume or release resources. 58 /// Defaults to `::free;::realloc;::freopen;::fclose`. 59 const StringRef LegacyResourceConsumers; 60 }; 61 62 } // namespace clang::tidy::cppcoreguidelines 63 64 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H 65