blob: 023202ae83f6ee3d8516aafbf0fbbfee705f4a89 [file] [log] [blame]
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +00001//===--- NoMallocCheck.h - clang-tidy----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +00006//
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 "../ClangTidy.h"
13#include "clang/AST/ASTContext.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15
16namespace clang {
17namespace tidy {
18namespace cppcoreguidelines {
19
20/// This checker is concerned with C-style memory management and suggest modern
21/// alternatives to it.
22/// The check is only enabled in C++. For analyzing malloc calls see Clang
23/// Static Analyzer - unix.Malloc.
24///
25/// For the user-facing documentation see:
26/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-no-malloc.html
27class NoMallocCheck : public ClangTidyCheck {
28public:
Alexander Kornienkod993e762017-03-02 08:28:55 +000029 /// Construct Checker and read in configuration for function names.
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +000030 NoMallocCheck(StringRef Name, ClangTidyContext *Context)
Alexander Kornienkod993e762017-03-02 08:28:55 +000031 : ClangTidyCheck(Name, Context),
32 AllocList(Options.get("Allocations", "::malloc;::calloc")),
33 ReallocList(Options.get("Reallocations", "::realloc")),
34 DeallocList(Options.get("Deallocations", "::free")) {}
35
36 /// Make configuration of checker discoverable.
37 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +000038
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;
Alexander Kornienkod993e762017-03-02 08:28:55 +000044
45private:
Simon Pilgrim9a5409832017-03-30 13:10:33 +000046 /// Semicolon-separated list of fully qualified names of memory allocation
Alexander Kornienkod993e762017-03-02 08:28:55 +000047 /// functions the check warns about. Defaults to `::malloc;::calloc`.
48 const std::string AllocList;
Simon Pilgrim9a5409832017-03-30 13:10:33 +000049 /// Semicolon-separated list of fully qualified names of memory reallocation
Alexander Kornienkod993e762017-03-02 08:28:55 +000050 /// functions the check warns about. Defaults to `::realloc`.
51 const std::string ReallocList;
Simon Pilgrim9a5409832017-03-30 13:10:33 +000052 /// Semicolon-separated list of fully qualified names of memory deallocation
Alexander Kornienkod993e762017-03-02 08:28:55 +000053 /// functions the check warns about. Defaults to `::free`.
54 const std::string DeallocList;
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +000055};
56
57} // namespace cppcoreguidelines
58} // namespace tidy
59} // namespace clang
60
61#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H