1ebdfb9cfSAlexander Kornienko //===--- NoMallocCheck.h - clang-tidy----------------------------*- C++ -*-===// 2ebdfb9cfSAlexander Kornienko // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ebdfb9cfSAlexander Kornienko // 7ebdfb9cfSAlexander Kornienko //===----------------------------------------------------------------------===// 8ebdfb9cfSAlexander Kornienko 9ebdfb9cfSAlexander Kornienko #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H 10ebdfb9cfSAlexander Kornienko #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H 11ebdfb9cfSAlexander Kornienko 12860aefd0SNathan James #include "../ClangTidyCheck.h" 13ebdfb9cfSAlexander Kornienko 14*4718da50SCarlos Galvez namespace clang::tidy::cppcoreguidelines { 15ebdfb9cfSAlexander Kornienko 16ebdfb9cfSAlexander Kornienko /// This checker is concerned with C-style memory management and suggest modern 17ebdfb9cfSAlexander Kornienko /// alternatives to it. 18ebdfb9cfSAlexander Kornienko /// The check is only enabled in C++. For analyzing malloc calls see Clang 19ebdfb9cfSAlexander Kornienko /// Static Analyzer - unix.Malloc. 20ebdfb9cfSAlexander Kornienko /// 21ebdfb9cfSAlexander Kornienko /// For the user-facing documentation see: 226e566bc5SRichard /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/no-malloc.html 23ebdfb9cfSAlexander Kornienko class NoMallocCheck : public ClangTidyCheck { 24ebdfb9cfSAlexander Kornienko public: 25d993e76aSAlexander Kornienko /// Construct Checker and read in configuration for function names. NoMallocCheck(StringRef Name,ClangTidyContext * Context)26ebdfb9cfSAlexander Kornienko NoMallocCheck(StringRef Name, ClangTidyContext *Context) 27d993e76aSAlexander Kornienko : ClangTidyCheck(Name, Context), 28d993e76aSAlexander Kornienko AllocList(Options.get("Allocations", "::malloc;::calloc")), 29d993e76aSAlexander Kornienko ReallocList(Options.get("Reallocations", "::realloc")), 30d993e76aSAlexander Kornienko DeallocList(Options.get("Deallocations", "::free")) {} 31d993e76aSAlexander Kornienko isLanguageVersionSupported(const LangOptions & LangOpts)32e40a742aSNathan James bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { 33e40a742aSNathan James return LangOpts.CPlusPlus; 34e40a742aSNathan James } 35e40a742aSNathan James 36d993e76aSAlexander Kornienko /// Make configuration of checker discoverable. 37d993e76aSAlexander Kornienko void storeOptions(ClangTidyOptions::OptionMap &Opts) override; 38ebdfb9cfSAlexander Kornienko 39ebdfb9cfSAlexander Kornienko /// Registering for malloc, calloc, realloc and free calls. 40ebdfb9cfSAlexander Kornienko void registerMatchers(ast_matchers::MatchFinder *Finder) override; 41ebdfb9cfSAlexander Kornienko 42ebdfb9cfSAlexander Kornienko /// Checks matched function calls and gives suggestion to modernize the code. 43ebdfb9cfSAlexander Kornienko void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 44d993e76aSAlexander Kornienko 45d993e76aSAlexander Kornienko private: 469a540983SSimon Pilgrim /// Semicolon-separated list of fully qualified names of memory allocation 47d993e76aSAlexander Kornienko /// functions the check warns about. Defaults to `::malloc;::calloc`. 4812cb5405SNathan James const StringRef AllocList; 499a540983SSimon Pilgrim /// Semicolon-separated list of fully qualified names of memory reallocation 50d993e76aSAlexander Kornienko /// functions the check warns about. Defaults to `::realloc`. 5112cb5405SNathan James const StringRef ReallocList; 529a540983SSimon Pilgrim /// Semicolon-separated list of fully qualified names of memory deallocation 53d993e76aSAlexander Kornienko /// functions the check warns about. Defaults to `::free`. 5412cb5405SNathan James const StringRef DeallocList; 55ebdfb9cfSAlexander Kornienko }; 56ebdfb9cfSAlexander Kornienko 57*4718da50SCarlos Galvez } // namespace clang::tidy::cppcoreguidelines 58ebdfb9cfSAlexander Kornienko 59ebdfb9cfSAlexander Kornienko #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H 60