clang-tools  10.0.0svn
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 "../ClangTidy.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 
34  /// Make configuration of checker discoverable.
35  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
36 
37  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
38  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
39 
40 private:
41  bool handleDeletion(const ast_matchers::BoundNodes &Nodes);
42  bool handleLegacyConsumers(const ast_matchers::BoundNodes &Nodes);
43  bool handleExpectedOwner(const ast_matchers::BoundNodes &Nodes);
44  bool handleAssignmentAndInit(const ast_matchers::BoundNodes &Nodes);
45  bool handleAssignmentFromNewOwner(const ast_matchers::BoundNodes &Nodes);
46  bool handleReturnValues(const ast_matchers::BoundNodes &Nodes);
47  bool handleOwnerMembers(const ast_matchers::BoundNodes &Nodes);
48 
49  /// List of old C-style functions that create resources.
50  /// Defaults to
51  /// `::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile`.
52  const std::string LegacyResourceProducers;
53  /// List of old C-style functions that consume or release resources.
54  /// Defaults to `::free;::realloc;::freopen;::fclose`.
55  const std::string LegacyResourceConsumers;
56 };
57 
58 } // namespace cppcoreguidelines
59 } // namespace tidy
60 } // namespace clang
61 
62 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_OWNING_MEMORY_H
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Match common cases, where the owner semantic is relevant, like function calls, delete expressions and...
Base class for all clang-tidy checks.
OwningMemoryCheck(StringRef Name, ClangTidyContext *Context)
Checks for common use cases for gsl::owner and enforces the unique owner nature of it whenever possib...
static constexpr llvm::StringLiteral Name
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Make configuration of checker discoverable.
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.