[clang] [Clang SA]: add support for mismatched ownership_returns+ownership_takes calls for custom allocation classes (PR #98941)

Pavel Skripkin via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 13:05:20 PDT 2024


================
@@ -103,14 +103,49 @@ using namespace std::placeholders;
 namespace {
 
 // Used to check correspondence between allocators and deallocators.
-enum AllocationFamily {
+enum AllocationFamilyKind {
   AF_None,
   AF_Malloc,
   AF_CXXNew,
   AF_CXXNewArray,
   AF_IfNameIndex,
   AF_Alloca,
-  AF_InnerBuffer
+  AF_InnerBuffer,
+  AF_Custom,
+};
+
+struct AllocationFamily {
+  AllocationFamilyKind Kind;
+  std::optional<StringRef> CustomName;
+
+  explicit AllocationFamily(AllocationFamilyKind kind,
+                            std::optional<StringRef> name = std::nullopt)
+      : Kind(kind), CustomName(name) {
+    assert(kind != AF_Custom || name != std::nullopt);
+
+    // Preseve previous behavior when "malloc" class means AF_Malloc
+    if (Kind == AF_Malloc && CustomName) {
+      if (CustomName.value() == "malloc")
+        CustomName = std::nullopt;
+      else
+        Kind = AF_Custom;
+    }
+  }
+
+  bool operator==(const AllocationFamily &Other) const {
+    return std::tie(Kind, CustomName) == std::tie(Other.Kind, Other.CustomName);
+  }
+
+  bool operator!=(const AllocationFamily &Other) const {
+    return !(*this == Other);
+  }
+
+  void Profile(llvm::FoldingSetNodeID &ID) const {
+    ID.AddInteger(Kind);
+
+    if (Kind == AF_Custom)
+      ID.AddString(CustomName.value());
+  }
----------------
pskrgag wrote:

Why? If it's not custom class, then unwrapping `CustomName` would cause an exception, isn't it?

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


More information about the cfe-commits mailing list