llvm-project
97 строк · 3.9 Кб
1//===--- SuspiciousStringviewDataUsageCheck.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 "SuspiciousStringviewDataUsageCheck.h"
10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::bugprone {
18
19SuspiciousStringviewDataUsageCheck::SuspiciousStringviewDataUsageCheck(
20StringRef Name, ClangTidyContext *Context)
21: ClangTidyCheck(Name, Context),
22StringViewTypes(utils::options::parseStringList(Options.get(
23"StringViewTypes", "::std::basic_string_view;::llvm::StringRef"))),
24AllowedCallees(
25utils::options::parseStringList(Options.get("AllowedCallees", ""))) {}
26
27void SuspiciousStringviewDataUsageCheck::storeOptions(
28ClangTidyOptions::OptionMap &Opts) {
29Options.store(Opts, "StringViewTypes",
30utils::options::serializeStringList(StringViewTypes));
31Options.store(Opts, "AllowedCallees",
32utils::options::serializeStringList(AllowedCallees));
33}
34
35bool SuspiciousStringviewDataUsageCheck::isLanguageVersionSupported(
36const LangOptions &LangOpts) const {
37return LangOpts.CPlusPlus;
38}
39
40std::optional<TraversalKind>
41SuspiciousStringviewDataUsageCheck::getCheckTraversalKind() const {
42return TK_AsIs;
43}
44
45void SuspiciousStringviewDataUsageCheck::registerMatchers(MatchFinder *Finder) {
46
47auto AncestorCall = anyOf(
48cxxConstructExpr(), callExpr(unless(cxxOperatorCallExpr())), lambdaExpr(),
49initListExpr(
50hasType(qualType(hasCanonicalType(hasDeclaration(recordDecl()))))));
51
52auto DataMethod =
53cxxMethodDecl(hasName("data"),
54ofClass(matchers::matchesAnyListedName(StringViewTypes)));
55
56auto SizeCall = cxxMemberCallExpr(
57callee(cxxMethodDecl(hasAnyName("size", "length"))),
58on(ignoringParenImpCasts(
59matchers::isStatementIdenticalToBoundNode("self"))));
60
61auto DescendantSizeCall = expr(hasDescendant(
62expr(SizeCall, hasAncestor(expr(AncestorCall).bind("ancestor-size")),
63hasAncestor(expr(equalsBoundNode("parent"),
64equalsBoundNode("ancestor-size"))))));
65
66Finder->addMatcher(
67cxxMemberCallExpr(
68on(ignoringParenImpCasts(expr().bind("self"))), callee(DataMethod),
69expr().bind("data-call"),
70hasParent(expr(anyOf(
71invocation(
72expr().bind("parent"), unless(cxxOperatorCallExpr()),
73hasAnyArgument(
74ignoringParenImpCasts(equalsBoundNode("data-call"))),
75unless(hasAnyArgument(ignoringParenImpCasts(SizeCall))),
76unless(hasAnyArgument(DescendantSizeCall)),
77hasDeclaration(namedDecl(
78unless(matchers::matchesAnyListedName(AllowedCallees))))),
79initListExpr(expr().bind("parent"),
80hasType(qualType(hasCanonicalType(hasDeclaration(
81recordDecl(unless(matchers::matchesAnyListedName(
82AllowedCallees))))))),
83unless(DescendantSizeCall)))))),
84this);
85}
86
87void SuspiciousStringviewDataUsageCheck::check(
88const MatchFinder::MatchResult &Result) {
89const auto *DataCallExpr =
90Result.Nodes.getNodeAs<CXXMemberCallExpr>("data-call");
91diag(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();
95}
96
97} // namespace clang::tidy::bugprone
98