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 14 namespace clang { 15 namespace tidy { 16 namespace cppcoreguidelines { 17 18 /// This checker is concerned with C-style memory management and suggest modern 19 /// alternatives to it. 20 /// The check is only enabled in C++. For analyzing malloc calls see Clang 21 /// Static Analyzer - unix.Malloc. 22 /// 23 /// For the user-facing documentation see: 24 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-no-malloc.html 25 class NoMallocCheck : public ClangTidyCheck { 26 public: 27 /// Construct Checker and read in configuration for function names. NoMallocCheck(StringRef Name,ClangTidyContext * Context)28 NoMallocCheck(StringRef Name, ClangTidyContext *Context) 29 : ClangTidyCheck(Name, Context), 30 AllocList(Options.get("Allocations", "::malloc;::calloc")), 31 ReallocList(Options.get("Reallocations", "::realloc")), 32 DeallocList(Options.get("Deallocations", "::free")) {} 33 isLanguageVersionSupported(const LangOptions & LangOpts)34 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { 35 return LangOpts.CPlusPlus; 36 } 37 38 /// Make configuration of checker discoverable. 39 void storeOptions(ClangTidyOptions::OptionMap &Opts) override; 40 41 /// Registering for malloc, calloc, realloc and free calls. 42 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 43 44 /// Checks matched function calls and gives suggestion to modernize the code. 45 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 46 47 private: 48 /// Semicolon-separated list of fully qualified names of memory allocation 49 /// functions the check warns about. Defaults to `::malloc;::calloc`. 50 const std::string AllocList; 51 /// Semicolon-separated list of fully qualified names of memory reallocation 52 /// functions the check warns about. Defaults to `::realloc`. 53 const std::string ReallocList; 54 /// Semicolon-separated list of fully qualified names of memory deallocation 55 /// functions the check warns about. Defaults to `::free`. 56 const std::string DeallocList; 57 }; 58 59 } // namespace cppcoreguidelines 60 } // namespace tidy 61 } // namespace clang 62 63 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NO_MALLOC_H 64