test(C1.1): cSourceCacheKey — content key for compiled #source units

Source bytes, declared-header CONTENT (header edits invalidate),
defines/flags/include dirs in order, LLVM version, and target
triple/sysroot all participate; section tags keep equal strings in
different roles distinct. Pure function + variance property tests;
nothing consumes it yet.
This commit is contained in:
agra
2026-06-12 16:43:42 +03:00
parent 1009181638
commit 10f5a4318d
3 changed files with 113 additions and 0 deletions

View File

@@ -33,6 +33,50 @@ pub const CImportInfo = struct {
flags: []const []const u8,
};
/// Cache key for one compiled `#source` member. Everything that can
/// change the produced object participates: the source bytes, the
/// unit's declared `#include` headers BY CONTENT (editing a declared
/// header invalidates), defines / flags / include dirs in declaration
/// order, the LLVM version, and the cross-target (triple + sysroot).
/// Section tags keep equal strings in different roles distinct (a
/// define never aliases a flag, an absent triple never aliases an
/// empty one). Transitive includes of the .c itself do NOT
/// participate — the block's declared surface is the invalidation
/// boundary.
pub fn cSourceCacheKey(
source_bytes: []const u8,
header_bytes: []const []const u8,
defines: []const []const u8,
flags: []const []const u8,
include_dirs: []const []const u8,
llvm_version: []const u8,
triple: ?[]const u8,
sysroot: ?[]const u8,
) u64 {
const Wyhash = std.hash.Wyhash;
var key = Wyhash.hash(0, "sx-c-import-v1");
key = Wyhash.hash(key, source_bytes);
key = Wyhash.hash(key, "\x01headers");
for (header_bytes) |hb| key = Wyhash.hash(key, hb);
key = Wyhash.hash(key, "\x01defines");
for (defines) |d| key = Wyhash.hash(key, d);
key = Wyhash.hash(key, "\x01flags");
for (flags) |f| key = Wyhash.hash(key, f);
key = Wyhash.hash(key, "\x01incdirs");
for (include_dirs) |inc| key = Wyhash.hash(key, inc);
key = Wyhash.hash(key, "\x01llvm");
key = Wyhash.hash(key, llvm_version);
if (triple) |t| {
key = Wyhash.hash(key, "\x01triple");
key = Wyhash.hash(key, t);
}
if (sysroot) |sr| {
key = Wyhash.hash(key, "\x01sysroot");
key = Wyhash.hash(key, sr);
}
return key;
}
/// Handle returned from loadCObjectsForJIT — caller must call unload() after JIT.
pub const CImportHandle = struct {
dylib_handle: ?*anyopaque = null,