[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:58 PST 2025


================
@@ -0,0 +1,40 @@
+//===--- SprintfArgumentOverlapCheck.h - clang-tidy -------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SPRINTFARGUMENTOVERLAPCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SPRINTFARGUMENTOVERLAPCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns if any arguments to the ``sprintf`` family of functions overlap with
+/// the destination buffer (the first argument).
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/sprintf-argument-overlap.html
+class SprintfArgumentOverlapCheck : public ClangTidyCheck {
+public:
+  SprintfArgumentOverlapCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+  std::optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+    return LangOpts.CPlusPlus || LangOpts.C99;
+  }
+
+private:
+  const std::string SprintfRegex;
----------------
PiotrZSL wrote:

Note: llvm::StringRef should be fine here

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


More information about the cfe-commits mailing list