10 #include "../utils/Matchers.h" 11 #include "../utils/OptionsUtils.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 23 namespace cppcoreguidelines {
26 Matcher<FunctionDecl> hasAnyListedName(
const std::string &FunctionNames) {
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 if (!getLangOpts().CPlusPlus)
45 Finder->addMatcher(callExpr(callee(functionDecl(hasAnyListedName(AllocList))))
51 callExpr(callee(functionDecl(hasAnyListedName(ReallocList))))
57 callExpr(callee(functionDecl(hasAnyListedName(DeallocList))))
62 void NoMallocCheck::check(
const MatchFinder::MatchResult &
Result) {
63 const CallExpr *Call =
nullptr;
64 StringRef Recommendation;
66 if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"allocation")))
67 Recommendation =
"consider a container or a smart pointer";
68 else if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"realloc")))
69 Recommendation =
"consider std::vector or std::string";
70 else if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"free")))
71 Recommendation =
"use RAII";
73 assert(Call &&
"Unhandled binding in the Matcher");
75 diag(Call->getBeginLoc(),
"do not manage memory manually; %0")
76 << Recommendation << SourceRange(Call->getBeginLoc(), Call->getEndLoc());
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result