10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
22 StringViewTypes(utils::options::parseStringList(Options.get(
23 "StringViewTypes",
"::std::basic_string_view;::llvm::StringRef"))),
25 utils::options::parseStringList(Options.get(
"AllowedCallees",
""))) {}
36 const LangOptions &LangOpts)
const {
37 return LangOpts.CPlusPlus;
40std::optional<TraversalKind>
47 auto AncestorCall = anyOf(
48 cxxConstructExpr(), callExpr(unless(cxxOperatorCallExpr())), lambdaExpr(),
50 hasType(qualType(hasCanonicalType(hasDeclaration(recordDecl()))))));
53 cxxMethodDecl(hasName(
"data"),
56 auto SizeCall = cxxMemberCallExpr(
57 callee(cxxMethodDecl(hasAnyName(
"size",
"length"))),
58 on(ignoringParenImpCasts(
59 matchers::isStatementIdenticalToBoundNode(
"self"))));
61 auto DescendantSizeCall = expr(hasDescendant(
62 expr(SizeCall, hasAncestor(expr(AncestorCall).bind(
"ancestor-size")),
63 hasAncestor(expr(equalsBoundNode(
"parent"),
64 equalsBoundNode(
"ancestor-size"))))));
68 on(ignoringParenImpCasts(expr().bind(
"self"))), callee(DataMethod),
69 expr().bind(
"data-call"),
72 expr().bind(
"parent"), unless(cxxOperatorCallExpr()),
74 ignoringParenImpCasts(equalsBoundNode(
"data-call"))),
75 unless(hasAnyArgument(ignoringParenImpCasts(SizeCall))),
76 unless(hasAnyArgument(DescendantSizeCall)),
77 hasDeclaration(namedDecl(
79 initListExpr(expr().bind(
"parent"),
80 hasType(qualType(hasCanonicalType(hasDeclaration(
82 AllowedCallees))))))),
83 unless(DescendantSizeCall)))))),
88 const MatchFinder::MatchResult &Result) {
89 const auto *DataCallExpr =
90 Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"data-call");
91 diag(DataCallExpr->getExprLoc(),
92 "result of a `data()` call may not be null terminated, provide size "
93 "information to the callee to prevent potential issues")
94 << DataCallExpr->getCallee()->getSourceRange();
llvm::SmallString< 256U > Name
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
std::optional< TraversalKind > getCheckTraversalKind() const override
SuspiciousStringviewDataUsageCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap