[clang-tools-extra] [clang-tidy] Add bugprone-suspicious-stringview-data-usage check (PR #83716)
Julian Schmidt via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 6 12:47:06 PST 2024
================
@@ -0,0 +1,100 @@
+//===--- SuspiciousStringviewDataUsageCheck.cpp - clang-tidy --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SuspiciousStringviewDataUsageCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+SuspiciousStringviewDataUsageCheck::SuspiciousStringviewDataUsageCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ StringViewTypes(utils::options::parseStringList(Options.get(
+ "StringViewTypes", "::std::basic_string_view;::llvm::StringRef"))),
+ AllowedCallees(
+ utils::options::parseStringList(Options.get("AllowedCallees", ""))) {}
+
+void SuspiciousStringviewDataUsageCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "StringViewTypes",
+ utils::options::serializeStringList(StringViewTypes));
+ Options.store(Opts, "AllowedCallees",
+ utils::options::serializeStringList(AllowedCallees));
+}
+
+bool SuspiciousStringviewDataUsageCheck::isLanguageVersionSupported(
+ const LangOptions &LangOpts) const {
+ return LangOpts.CPlusPlus;
+}
+
+std::optional<TraversalKind>
+SuspiciousStringviewDataUsageCheck::getCheckTraversalKind() const {
+ return TK_AsIs;
+}
+
+void SuspiciousStringviewDataUsageCheck::registerMatchers(MatchFinder *Finder) {
+
+ auto AncestorCall = anyOf(
+ cxxConstructExpr(), callExpr(unless(cxxOperatorCallExpr())), lambdaExpr(),
+ initListExpr(
+ hasType(qualType(hasCanonicalType(hasDeclaration(recordDecl()))))));
+
+ auto DataMethod =
+ cxxMethodDecl(hasName("data"),
+ ofClass(matchers::matchesAnyListedName(StringViewTypes)));
+
+ auto DataWithSelfCall =
+ cxxMemberCallExpr(on(ignoringParenImpCasts(expr().bind("self"))),
+ callee(DataMethod))
+ .bind("data-call");
+ auto SizeCall = cxxMemberCallExpr(
+ callee(cxxMethodDecl(hasAnyName("size", "length"))),
+ on(ignoringParenImpCasts(
+ matchers::isStatementIdenticalToBoundNode("self"))));
+
+ Finder->addMatcher(
+ cxxMemberCallExpr(
+ on(ignoringParenImpCasts(expr().bind("self"))), callee(DataMethod),
+ expr().bind("data-call"),
+ hasParent(expr(anyOf(
+ invocation(
+ expr().bind("call"), unless(cxxOperatorCallExpr()),
+ hasAnyArgument(
+ ignoringParenImpCasts(equalsBoundNode("data-call"))),
+ unless(hasAnyArgument(ignoringParenImpCasts(SizeCall))),
+ unless(hasAnyArgument(hasDescendant(expr(
+ SizeCall,
+ hasAncestor(expr(AncestorCall).bind("ancestor-size")),
+ hasAncestor(expr(equalsBoundNode("call"),
+ equalsBoundNode("ancestor-size"))))))),
+ hasDeclaration(namedDecl(
+ unless(matchers::matchesAnyListedName(AllowedCallees))))),
+ initListExpr(expr().bind("init"),
----------------
5chmidti wrote:
`init` is unused
https://github.com/llvm/llvm-project/pull/83716
More information about the cfe-commits
mailing list