[libc-commits] [libc] [libc] Add regex AST and ExprPool (PR #198728)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Thu Jun 18 22:22:35 PDT 2026


================
@@ -0,0 +1,163 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Pool for Regular Expression AST nodes (Implementation).
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/regex/regex_expr_pool.h"
+#include "hdr/regex_macros.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/alloc-checker.h"
+#include "src/__support/hash.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace {
+
+// Hash an Expr node for hash-consing.
+uint64_t hash_expr(const Expr &e) {
+  // Initialise HashState with a constant seed. The specific value (0x12345678)
+  // is an arbitrary placeholder; HashState immediately mixes this seed with
+  // high-entropy constants (derived from aHash) to produce a strong hash, while
+  // the constant value guarantees deterministic hashing for hash-consing.
+  internal::HashState hasher(0x12345678);
+  uint64_t kind = static_cast<uint64_t>(e.kind);
+  hasher.update(&kind, sizeof(kind));
+  switch (e.kind) {
+  case ExprKind::Literal:
+    hasher.update(&e.ch, sizeof(e.ch));
+    break;
+  case ExprKind::Concat:
+  case ExprKind::Alt:
+    hasher.update(&e.bin.left, sizeof(e.bin.left));
+    hasher.update(&e.bin.right, sizeof(e.bin.right));
+    break;
+  default:
+    break;
+  }
+  return hasher.finish();
+}
+
+} // namespace
+
+ExprPool::Block::Block() : next(nullptr), used(0) {}
+
+ExprPool::ExprPool() : head(nullptr), current(nullptr), node_count(0) {
+  AllocChecker ac;
+  hashtable = new (ac) Expr *[HASH_TABLE_SIZE];
+  if (ac) {
+    for (size_t i = 0; i < HASH_TABLE_SIZE; ++i)
----------------
vonosmas wrote:

Can you use `inline_memset` here? I think we prevent compiler from generating a real memset call here due to `-fno-builtin` we tend to add to compiler flags.

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


More information about the libc-commits mailing list