[clang] [clang][-Wunsafe-buffer-usage] Add per-family subgroups of -Wunsafe-buffer-usage-in-libc-call (PR #233604)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 23:21:20 PST 2026


================
@@ -1140,93 +1149,97 @@ hasUnsafeFormatOrSArg(ASTContext &Ctx, const CallExpr *Call,
 //  1. It's name, after stripping off predefined prefix and suffix, is
 //     `CoreName`; and
 //  2. `CoreName` or `CoreName[str/wcs]` is one of the `PredefinedNames`, which
-//     is a set of libc function names.
+//     maps libc function names to their families.
 //
 //  Note: For predefined prefix and suffix, see `matchName()`.
 //  The notation `CoreName[str/wcs]` means a new name obtained from replace
 //  string "wcs" with "str" in `CoreName`.
-static bool isPredefinedUnsafeLibcFunc(const FunctionDecl &Node) {
-  static const std::set<StringRef> PredefinedNames = {
+//
+// Returns the family of the matched function, or std::nullopt if it does not
+// match.
+static std::optional<UnsafeLibcFunctionFamily>
+getPredefinedUnsafeLibcFuncFamily(const FunctionDecl &Node) {
+  static const llvm::StringMap<UnsafeLibcFunctionFamily> PredefinedNames = {
----------------
usx95 wrote:

Suggestion for better readability of this piece:
```cpp
static const llvm::StringMap<UnsafeLibcFunctionFamily> PredefinedNames = [] {
  using F = UnsafeLibcFunctionFamily;
  llvm::StringMap<F> M;
  auto Add = [&](F Family, std::initializer_list<StringRef> Names) {
    for (StringRef N : Names)
      M[N] = Family;
  };
  Add(F::NumericConversion, {"atof", "atoi", "atol", "atoll", "strtol", /*...*/});
  Add(F::StringManipulation, {"strcpy", "strncpy", /*...*/});
  Add(F::StringExamination, {"strlen", "strnlen", /*...*/});
  Add(F::Memory, {"memchr", "wmemchr", /*...*/ "bcopy", "bzero"});
  Add(F::IO, {"fread", "fwrite", /*...*/});
  Add(F::Other, {"strerror_s", "strerror_r", "bsearch", "qsort"});
  return M;
}();
```

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


More information about the cfe-commits mailing list