[clang-tools-extra] Add bugprone-sprintf-argument-overlap (PR #114244)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 13 12:08:59 PST 2025


================
@@ -0,0 +1,106 @@
+//===--- SprintfArgumentOverlapCheck.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 "SprintfArgumentOverlapCheck.h"
+#include "../utils/ASTUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+// Similar to forEachArgumentWithParam. forEachArgumentWithParam does not work
+// with variadic functions like sprintf, since there is no `decl()` to match
+// against in the parameter list `...`.
+AST_MATCHER_P(CallExpr, forEachArgument, ast_matchers::internal::Matcher<Expr>,
+              ArgMatcher) {
+  using namespace clang::ast_matchers::internal;
+  BoundNodesTreeBuilder Result;
+  int ParamIndex = 0;
+  bool Matched = false;
+  for (unsigned ArgIndex = 0; ArgIndex < Node.getNumArgs(); ++ArgIndex) {
+    BoundNodesTreeBuilder ArgMatches(*Builder);
+    if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()), Finder,
+                           &ArgMatches)) {
+      BoundNodesTreeBuilder ParamMatches(ArgMatches);
+      Result.addMatch(ArgMatches);
+      Matched = true;
+    }
+    ++ParamIndex;
+  }
+  *Builder = std::move(Result);
+  return Matched;
+}
+
+AST_MATCHER_P(Stmt, identicalTo, std::string, ID) {
----------------
PiotrZSL wrote:

there is isStatementIdenticalToBoundNode in utils/Matchers.h

https://github.com/llvm/llvm-project/pull/114244


More information about the cfe-commits mailing list