[clang-tools-extra] [clang-tidy] Add deprecated-posix-functions check (PR #195641)
Yanzuo Liu via cfe-commits
cfe-commits at lists.llvm.org
Wed May 6 07:23:24 PDT 2026
================
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "DeprecatedPosixFunctionsCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "llvm/ADT/StringSwitch.h"
+
+using namespace clang::ast_matchers;
+using namespace llvm;
+
+namespace clang::tidy::portability {
+
+static constexpr StringRef DeprecatedFunctionId = "DeprecatedFunctions";
+static constexpr StringRef DeclRefId = "DRE";
+
+static StringRef getReplacementFor(StringRef FunctionName) {
+ // TODO: Suggest Annex K replacements when available.
+ return StringSwitch<StringRef>(FunctionName)
+ .Case("bcmp", "memcmp")
+ .Case("bcopy", "memmove")
+ .Case("bzero", "memset")
+ .Case("getpw", "getpwuid")
+ .Case("vfork", "posix_spawn")
+ .Default({});
+}
+
+void DeprecatedPosixFunctionsCheck::registerMatchers(MatchFinder *Finder) {
+ const auto FunctionNamesMatcher =
+ hasAnyName("::bcmp", "::bcopy", "::bzero", "::getpw", "::vfork");
+ Finder->addMatcher(
+ declRefExpr(
+ to(functionDecl(FunctionNamesMatcher).bind(DeprecatedFunctionId)))
+ .bind(DeclRefId),
+ this);
+}
+
+void DeprecatedPosixFunctionsCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ const auto *SourceExpr = Result.Nodes.getNodeAs<DeclRefExpr>(DeclRefId);
+ const auto *FuncDecl =
+ Result.Nodes.getNodeAs<FunctionDecl>(DeprecatedFunctionId);
+ if (!SourceExpr || !FuncDecl)
----------------
zwuis wrote:
Use `assert` :)
https://github.com/llvm/llvm-project/pull/195641
More information about the cfe-commits
mailing list