clang-tools 20.0.0git
NoMallocCheck.cpp
Go to the documentation of this file.
1//===--- NoMallocCheck.cpp - clang-tidy------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "NoMallocCheck.h"
10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include <algorithm>
15#include <string>
16#include <vector>
17
18using namespace clang::ast_matchers;
19using namespace clang::ast_matchers::internal;
20
22
24 Options.store(Opts, "Allocations", AllocList);
25 Options.store(Opts, "Reallocations", ReallocList);
26 Options.store(Opts, "Deallocations", DeallocList);
27}
28
29void NoMallocCheck::registerMatchers(MatchFinder *Finder) {
30 // Registering malloc, will suggest RAII.
31 Finder->addMatcher(callExpr(callee(functionDecl(hasAnyName(
33 .bind("allocation"),
34 this);
35
36 // Registering realloc calls, suggest std::vector or std::string.
37 Finder->addMatcher(
38 callExpr(callee(functionDecl(
39 hasAnyName(utils::options::parseStringList((ReallocList))))))
40 .bind("realloc"),
41 this);
42
43 // Registering free calls, will suggest RAII instead.
44 Finder->addMatcher(
45 callExpr(callee(functionDecl(
46 hasAnyName(utils::options::parseStringList((DeallocList))))))
47 .bind("free"),
48 this);
49}
50
51void NoMallocCheck::check(const MatchFinder::MatchResult &Result) {
52 const CallExpr *Call = nullptr;
53 StringRef Recommendation;
54
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";
61
62 assert(Call && "Unhandled binding in the Matcher");
63
64 diag(Call->getBeginLoc(), "do not manage memory manually; %0")
65 << Recommendation << SourceRange(Call->getBeginLoc(), Call->getEndLoc());
66}
67
68} // namespace clang::tidy::cppcoreguidelines
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