clang-tools  14.0.0git
OwningMemoryCheck.h
Go to the documentation of this file.
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
24 public:
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  }
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
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::cppcoreguidelines::OwningMemoryCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: OwningMemoryCheck.h:33
clang::tidy::cppcoreguidelines::OwningMemoryCheck::OwningMemoryCheck
OwningMemoryCheck(StringRef Name, ClangTidyContext *Context)
Definition: OwningMemoryCheck.h:25
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::cppcoreguidelines::OwningMemoryCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Make configuration of checker discoverable.
Definition: OwningMemoryCheck.cpp:34
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::cppcoreguidelines::OwningMemoryCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Match common cases, where the owner semantic is relevant, like function calls, delete expressions and...
Definition: OwningMemoryCheck.cpp:41
clang::tidy::cppcoreguidelines::OwningMemoryCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: OwningMemoryCheck.cpp:178
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cppcoreguidelines::OwningMemoryCheck
Checks for common use cases for gsl::owner and enforces the unique owner nature of it whenever possib...
Definition: OwningMemoryCheck.h:23