blob: 4cec8a8346b20f6bda2656047f02f47c221a2617 [file] [log] [blame]
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +00001//===--- NoMallocCheck.h - clang-tidy----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H
12
13#include "../ClangTidy.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/ASTMatchers/ASTMatchFinder.h"
16
17namespace clang {
18namespace tidy {
19namespace cppcoreguidelines {
20
21/// This checker is concerned with C-style memory management and suggest modern
22/// alternatives to it.
23/// The check is only enabled in C++. For analyzing malloc calls see Clang
24/// Static Analyzer - unix.Malloc.
25///
26/// For the user-facing documentation see:
27/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-no-malloc.html
28class NoMallocCheck : public ClangTidyCheck {
29public:
Alexander Kornienkod993e762017-03-02 08:28:55 +000030 /// Construct Checker and read in configuration for function names.
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +000031 NoMallocCheck(StringRef Name, ClangTidyContext *Context)
Alexander Kornienkod993e762017-03-02 08:28:55 +000032 : ClangTidyCheck(Name, Context),
33 AllocList(Options.get("Allocations", "::malloc;::calloc")),
34 ReallocList(Options.get("Reallocations", "::realloc")),
35 DeallocList(Options.get("Deallocations", "::free")) {}
36
37 /// Make configuration of checker discoverable.
38 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +000039
40 /// Registering for malloc, calloc, realloc and free calls.
41 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
42
43 /// Checks matched function calls and gives suggestion to modernize the code.
44 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
Alexander Kornienkod993e762017-03-02 08:28:55 +000045
46private:
Simon Pilgrim9a5409832017-03-30 13:10:33 +000047 /// Semicolon-separated list of fully qualified names of memory allocation
Alexander Kornienkod993e762017-03-02 08:28:55 +000048 /// functions the check warns about. Defaults to `::malloc;::calloc`.
49 const std::string AllocList;
Simon Pilgrim9a5409832017-03-30 13:10:33 +000050 /// Semicolon-separated list of fully qualified names of memory reallocation
Alexander Kornienkod993e762017-03-02 08:28:55 +000051 /// functions the check warns about. Defaults to `::realloc`.
52 const std::string ReallocList;
Simon Pilgrim9a5409832017-03-30 13:10:33 +000053 /// Semicolon-separated list of fully qualified names of memory deallocation
Alexander Kornienkod993e762017-03-02 08:28:55 +000054 /// functions the check warns about. Defaults to `::free`.
55 const std::string DeallocList;
Alexander Kornienkoebdfb9c2016-12-13 16:38:18 +000056};
57
58} // namespace cppcoreguidelines
59} // namespace tidy
60} // namespace clang
61
62#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H