11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
16 using namespace clang::ast_matchers;
20 namespace cppcoreguidelines {
22 void NoMallocCheck::registerMatchers(MatchFinder *
Finder) {
24 if (!getLangOpts().CPlusPlus)
29 callExpr(callee(functionDecl(hasAnyName(
"::malloc",
"::calloc"))))
35 callExpr(callee(functionDecl(hasName(
"::realloc")))).bind(
"realloc"),
40 callExpr(callee(functionDecl(hasName(
"::free")))).bind(
"free"),
this);
43 void NoMallocCheck::check(
const MatchFinder::MatchResult &
Result) {
44 const CallExpr *Call =
nullptr;
45 StringRef Recommendation;
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";
54 assert(Call &&
"Unhandled binding in the Matcher");
56 diag(Call->getLocStart(),
"do not manage memory manually; %0")
57 << Recommendation << SourceRange(Call->getLocStart(), Call->getLocEnd());
std::unique_ptr< ast_matchers::MatchFinder > Finder