10 #include "../utils/Matchers.h"
11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
19 using namespace clang::ast_matchers::internal;
23 namespace cppcoreguidelines {
27 const std::vector<std::string> NameList =
29 return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
34 Options.store(Opts,
"Allocations", AllocList);
35 Options.store(Opts,
"Reallocations", ReallocList);
36 Options.store(Opts,
"Deallocations", DeallocList);
39 void NoMallocCheck::registerMatchers(MatchFinder *Finder) {
41 Finder->addMatcher(callExpr(callee(functionDecl(
hasAnyListedName(AllocList))))
58 void NoMallocCheck::check(
const MatchFinder::MatchResult &Result) {
59 const CallExpr *Call =
nullptr;
60 StringRef Recommendation;
62 if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"allocation")))
63 Recommendation =
"consider a container or a smart pointer";
64 else if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"realloc")))
65 Recommendation =
"consider std::vector or std::string";
66 else if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"free")))
67 Recommendation =
"use RAII";
69 assert(Call &&
"Unhandled binding in the Matcher");
71 diag(Call->getBeginLoc(),
"do not manage memory manually; %0")
72 << Recommendation << SourceRange(Call->getBeginLoc(), Call->getEndLoc());