xref: /llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/NoMallocCheck.cpp (revision ebdfb9cf0cea0bd9ae91052b714397569d1c653a)
1 //===--- NoMallocCheck.cpp - clang-tidy------------------------------------===//
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 #include "NoMallocCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include <iostream>
14 #include <string>
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace cppcoreguidelines {
21 
22 void NoMallocCheck::registerMatchers(MatchFinder *Finder) {
23   // C-style memory management is only problematic in C++.
24   if (!getLangOpts().CPlusPlus)
25     return;
26 
27   // Registering malloc, will suggest RAII.
28   Finder->addMatcher(
29       callExpr(callee(functionDecl(hasAnyName("::malloc", "::calloc"))))
30           .bind("aquisition"),
31       this);
32 
33   // Registering realloc calls, suggest std::vector or std::string.
34   Finder->addMatcher(
35       callExpr(callee(functionDecl(hasName("::realloc")))).bind("realloc"),
36       this);
37 
38   // Registering free calls, will suggest RAII instead.
39   Finder->addMatcher(
40       callExpr(callee(functionDecl(hasName("::free")))).bind("free"), this);
41 }
42 
43 void NoMallocCheck::check(const MatchFinder::MatchResult &Result) {
44   const CallExpr *Call = nullptr;
45   StringRef Recommendation;
46 
47   if ((Call = Result.Nodes.getNodeAs<CallExpr>("aquisition")))
48     Recommendation = "consider a container or a smart pointer";
49   else if ((Call = Result.Nodes.getNodeAs<CallExpr>("realloc")))
50     Recommendation = "consider std::vector or std::string";
51   else if ((Call = Result.Nodes.getNodeAs<CallExpr>("free")))
52     Recommendation = "use RAII";
53 
54   assert(Call && "Unhandled binding in the Matcher");
55 
56   diag(Call->getLocStart(), "do not manage memory manually; %0")
57       << Recommendation << SourceRange(Call->getLocStart(), Call->getLocEnd());
58 }
59 
60 } // namespace cppcoreguidelines
61 } // namespace tidy
62 } // namespace clang
63