clang-tools 20.0.0git
NoMallocCheck.h
Go to the documentation of this file.
1//===--- NoMallocCheck.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_NO_MALLOC_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H
11
12#include "../ClangTidyCheck.h"
13
15
16/// This checker is concerned with C-style memory management and suggest modern
17/// alternatives to it.
18/// The check is only enabled in C++. For analyzing malloc calls see Clang
19/// Static Analyzer - unix.Malloc.
20///
21/// For the user-facing documentation see:
22/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/no-malloc.html
24public:
25 /// Construct Checker and read in configuration for function names.
26 NoMallocCheck(StringRef Name, ClangTidyContext *Context)
27 : ClangTidyCheck(Name, Context),
28 AllocList(Options.get("Allocations", "::malloc;::calloc")),
29 ReallocList(Options.get("Reallocations", "::realloc")),
30 DeallocList(Options.get("Deallocations", "::free")) {}
31
32 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
33 return LangOpts.CPlusPlus;
34 }
35
36 /// Make configuration of checker discoverable.
37 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
38
39 /// Registering for malloc, calloc, realloc and free calls.
40 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
41
42 /// Checks matched function calls and gives suggestion to modernize the code.
43 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
44
45private:
46 /// Semicolon-separated list of fully qualified names of memory allocation
47 /// functions the check warns about. Defaults to `::malloc;::calloc`.
48 const StringRef AllocList;
49 /// Semicolon-separated list of fully qualified names of memory reallocation
50 /// functions the check warns about. Defaults to `::realloc`.
51 const StringRef ReallocList;
52 /// Semicolon-separated list of fully qualified names of memory deallocation
53 /// functions the check warns about. Defaults to `::free`.
54 const StringRef DeallocList;
55};
56
57} // namespace clang::tidy::cppcoreguidelines
58
59#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
This checker is concerned with C-style memory management and suggest modern alternatives to it.
Definition: NoMallocCheck.h:23
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Registering for malloc, calloc, realloc and free calls.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: NoMallocCheck.h:32
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
Checks matched function calls and gives suggestion to modernize the code.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Make configuration of checker discoverable.
NoMallocCheck(StringRef Name, ClangTidyContext *Context)
Construct Checker and read in configuration for function names.
Definition: NoMallocCheck.h:26
llvm::StringMap< ClangTidyValue > OptionMap