10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
19using namespace clang::ast_matchers::internal;
31 Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName(
38 callExpr(callee(functionDecl(
45 callExpr(callee(functionDecl(
52 const CallExpr *Call =
nullptr;
53 StringRef Recommendation;
55 if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"allocation")))
56 Recommendation =
"consider a container or a smart pointer";
57 else if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"realloc")))
58 Recommendation =
"consider std::vector or std::string";
59 else if ((Call = Result.Nodes.getNodeAs<CallExpr>(
"free")))
60 Recommendation =
"use RAII";
62 assert(Call &&
"Unhandled binding in the Matcher");
64 diag(Call->getBeginLoc(),
"do not manage memory manually; %0")
65 << Recommendation << SourceRange(Call->getBeginLoc(), Call->getEndLoc());
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Registering for malloc, calloc, realloc and free calls.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
Checks matched function calls and gives suggestion to modernize the code.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Make configuration of checker discoverable.
std::vector< StringRef > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
llvm::StringMap< ClangTidyValue > OptionMap