================
@@ -16,6 +16,49 @@
 using namespace clang::ast_matchers;
 
 namespace clang::tidy::modernize {
+namespace {
+// Given two argument indices X and Y, matches when a call expression has a
+// string at index X with an expression representing that string's length at
+// index Y. The string can be a string literal or a variable. The length can be
+// matched via an integer literal or a call to strlen() in the case of a string
+// literal, and by a call to size() or length() in the string variable case.
+AST_POLYMORPHIC_MATCHER_P2(HasStringAndLengthArguments,
+                           AST_POLYMORPHIC_SUPPORTED_TYPES(
+                               CallExpr, CXXConstructExpr,
+                               CXXUnresolvedConstructExpr, ObjCMessageExpr),
+                           unsigned, StringArgIndex, unsigned, LengthArgIndex) 
{
+  if (StringArgIndex >= Node.getNumArgs() ||
+      LengthArgIndex >= Node.getNumArgs()) {
+    return false;
+  }
+
+  const Expr *StringArgExpr =
+      Node.getArg(StringArgIndex)->IgnoreParenImpCasts();
+  const Expr *LengthArgExpr =
+      Node.getArg(LengthArgIndex)->IgnoreParenImpCasts();
+
+  if (const auto *StringArg = dyn_cast<StringLiteral>(StringArgExpr)) {
+    // Match an integer literal equal to the string length or a call to strlen.
+    const auto Matcher = expr(anyOf(
+        integerLiteral(equals(StringArg->getLength())),
+        callExpr(
+            callee(functionDecl(hasName("strlen"))), argumentCountIs(1),
+            hasArgument(0, stringLiteral(hasSize(StringArg->getLength()))))));
+    return Matcher.matches(*LengthArgExpr, Finder, Builder);
----------------
PiotrZSL wrote:

i do not recommend constructing matcher in matcher, mainly due to performance 
reasons (constant construction of matcher).
What you could do is to pass matcher to this matcher as "inner matcher", and 
then just call it here, or convert it to simple function.

https://github.com/llvm/llvm-project/pull/89530
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to