stage3 ai编写并提供测试用例并重构nob

This commit is contained in:
zzy committed 2026-10-31 11:06:00 +08:00
1 parent 2c531f23de
commit b4831c8d1c
150 files changed
+15838 -231

No files matched your search

+339 -58
View File
@@ -88,15 +88,141 @@ typedef struct {
size_t capacity;
} str_da_t;
static const char *path_basename(const char *path) {
const char *base = path;
for (const char *p = path; *p; p += 1) {
if (*p == '/' || *p == '\\')
base = p + 1;
}
return base;
}
// `*` 匹配任意串。只用来比文件名(不含分隔符),所以不需要处理跨目录。
static bool wildcard_match(const char *pat, const char *s) {
const char *star = NULL;
const char *resume = NULL;
while (*s) {
if (*pat == '*') {
star = pat++;
resume = s;
} else if (*pat == *s) {
pat++;
s++;
} else if (star) {
pat = star + 1;
s = ++resume;
} else {
return false;
}
}
while (*pat == '*')
pat++;
return *pat == '\0';
}
// 依赖模式的展开:只支持两种写法
// dir/*.ext —— 只看 dir 的直接子文件
// dir/**/*.ext —— 递归 dir 的子目录
// 不含 '*' 的条目按字面路径原样返回(跨目录的显式依赖用这个)。
typedef struct {
size_t dir_len;
const char *name_pattern;
bool recursive;
str_da_t *out;
} glob_ctx_t;
static bool collect_glob_entry(Nob_Walk_Entry entry) {
if (entry.type != NOB_FILE_REGULAR)
return true;
glob_ctx_t *ctx = (glob_ctx_t *)entry.data;
const char *rel = entry.path + ctx->dir_len;
while (*rel == '/' || *rel == '\\')
rel++;
if (!ctx->recursive) {
for (const char *p = rel; *p; p += 1) {
if (*p == '/' || *p == '\\')
return true;
}
}
if (!wildcard_match(ctx->name_pattern, path_basename(rel)))
return true;
da_append(ctx->out, nob_temp_strdup(entry.path));
return true;
}
// 某个模式一条都没匹配到就报错:依赖悄悄变空比多编一次危险得多。
static bool expand_patterns(const char **patterns, size_t count, str_da_t *out) {
for (size_t i = 0; i < count; i += 1) {
const char *pat = patterns[i];
if (strchr(pat, '*') == NULL) {
da_append(out, pat);
continue;
}
const char *slash = NULL;
for (const char *p = pat; *p; p += 1) {
if (*p == '/' || *p == '\\')
slash = p;
}
if (slash == NULL || slash == pat) {
nob_log(NOB_ERROR, "pattern `%s` needs a directory part", pat);
return false;
}
size_t dir_len = (size_t)(slash - pat);
const char *name_pattern = slash + 1;
bool recursive = false;
if (dir_len >= 2 && pat[dir_len - 1] == '*' && pat[dir_len - 2] == '*') {
recursive = true;
dir_len -= 2;
while (dir_len > 0 && (pat[dir_len - 1] == '/' || pat[dir_len - 1] == '\\'))
dir_len -= 1;
}
if (dir_len == 0) {
nob_log(NOB_ERROR, "pattern `%s` needs a directory part", pat);
return false;
}
char *dir = nob_temp_sprintf("%.*s", (int)dir_len, pat);
glob_ctx_t ctx = {
.dir_len = dir_len,
.name_pattern = name_pattern,
.recursive = recursive,
.out = out,
};
size_t before = out->count;
if (!nob_walk_dir(dir, collect_glob_entry, .data = &ctx))
return false;
if (out->count == before) {
nob_log(NOB_ERROR, "pattern `%s` matched nothing", pat);
return false;
}
}
return true;
}
bool run_target(const char *name, Nob_Cmd *args);
const char *build_target(const char *name);
static const char *build_exe(build_t *target) {
const char *result = NULL;
Nob_Cmd cmd = {0};
str_da_t objs = {0};
str_da_t deps = {0};
for (size_t i = 0; i < target->exe.srcs_count; ++i) {
const char *src = target->exe.srcs[i];
str_da_t srcs = {0};
str_da_t headers = {0};
// srcs/headers 里可以写通配模式(见 expand_patterns),展开一次给后面所有源文件用。
if (!expand_patterns(target->exe.srcs, target->exe.srcs_count, &srcs))
goto defer;
if (!expand_patterns(target->exe.headers, target->exe.headers_count, &headers))
goto defer;
for (size_t i = 0; i < srcs.count; ++i) {
const char *src = srcs.items[i];
const char *obj = obj_path(src);
if (!nob_file_exists(src)) {
nob_log(NOB_ERROR, "source file `%s` does not exist", src);
@@ -107,8 +233,8 @@ static const char *build_exe(build_t *target) {
da_append(&deps, src);
da_append(&deps, CFLAGS_STAMP);
for (size_t j = 0; j < target->exe.headers_count; ++j) {
da_append(&deps, target->exe.headers[j]);
for (size_t j = 0; j < headers.count; ++j) {
da_append(&deps, headers.items[j]);
}
int needs = nob_needs_rebuild(obj, deps.items, deps.count);
@@ -135,8 +261,10 @@ static const char *build_exe(build_t *target) {
int needs = nob_needs_rebuild(exe, objs.items, objs.count);
if (needs < 0)
goto defer;
if (needs == 0)
return exe;
if (needs == 0) {
result = exe;
goto defer;
}
nob_cc(&cmd);
nob_cmd_append(&cmd, SPL_OPTIMIZATION_FLAG);
@@ -144,16 +272,17 @@ static const char *build_exe(build_t *target) {
nob_cc_inputs(&cmd, objs.items[i]);
}
nob_cc_output(&cmd, exe);
if (!nob_cmd_run(&cmd)) {
if (!nob_cmd_run(&cmd))
goto defer;
}
return exe;
result = exe;
defer:
nob_cmd_free(cmd);
da_free(objs);
da_free(deps);
return NULL;
da_free(srcs);
da_free(headers);
return result;
}
const char *build_target(const char *name) {
@@ -170,8 +299,28 @@ const char *build_target(const char *name) {
return build_exe(target);
}
case BUILD_SIR: {
Nob_Cmd cmd = {0};
const char *target_sir = sir_path(name);
// 编译器先建好:它的产物也要算进依赖,编译器变了就得重编 .sir。
const char *compiler = build_target(target->sir.compiler);
if (!compiler) {
nob_log(NOB_ERROR, "failed to build `%s`", name);
return NULL;
}
str_da_t deps = {0};
bool ok = expand_patterns(target->sir.deps, target->sir.deps_count, &deps);
if (ok) {
da_append(&deps, target->sir.src);
da_append(&deps, compiler);
}
int needs = ok ? nob_needs_rebuild(target_sir, deps.items, deps.count) : -1;
da_free(deps);
if (needs < 0)
return NULL;
if (needs == 0)
return target_sir;
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, target->sir.src, target_sir);
if (!run_target(target->sir.compiler, &cmd)) {
nob_log(NOB_ERROR, "failed to build `%s`", name);
@@ -185,27 +334,41 @@ const char *build_target(const char *name) {
Nob_Cmd cmd = {0};
const char *target_exe = exe_path(name);
const char *target_c = c_path(name);
// TODO need_rebuild
int needs = nob_needs_rebuild(target_c, target->native.deps, target->native.deps_count);
if (needs < 0)
const char *compiler = build_target(target->native.compiler);
if (!compiler) {
nob_log(NOB_ERROR, "failed to build `%s`", name);
return NULL;
}
str_da_t deps = {0};
bool ok = expand_patterns(target->native.deps, target->native.deps_count, &deps);
if (ok) {
da_append(&deps, target->native.src);
da_append(&deps, compiler);
}
int needs = ok ? nob_needs_rebuild(target_c, deps.items, deps.count) : -1;
if (needs < 0) {
da_free(deps);
return NULL;
}
if (needs != 0) {
nob_cmd_append(&cmd, "--dump", "c", target->native.src, target_c);
if (!run_target(target->native.compiler, &cmd)) {
nob_log(NOB_ERROR, "failed to build `%s`", name);
nob_cmd_free(cmd);
da_free(deps);
return NULL;
}
cmd.count = 0;
}
/* native exe 的输入是 SPL 源与 crt 头;build/*.c 是产物,不作为依赖。 */
needs = nob_needs_rebuild(target_exe, target->native.deps,
target->native.deps_count);
if (needs == 0)
needs = nob_needs_rebuild(target_exe, target->native.crt_deps,
target->native.crt_deps_count);
// exe 的输入:生成的 .c、SPL 依赖、crt 头。
da_append(&deps, target_c);
for (size_t i = 0; i < target->native.crt_deps_count; i += 1)
da_append(&deps, target->native.crt_deps[i]);
needs = nob_needs_rebuild(target_exe, deps.items, deps.count);
da_free(deps);
if (needs < 0)
return NULL;
if (needs == 0)
@@ -214,7 +377,7 @@ const char *build_target(const char *name) {
nob_cc(&cmd);
/* 生成的 C 里有跨类型指针访问;-O2 严格别名会误优化。 */
nob_cmd_append(&cmd, "-I./stage0", "-O2", "-fno-strict-aliasing",
"-D_CRT_SECURE_NO_WARNINGS");
"-D_CRT_SECURE_NO_WARNINGS", "-D_CRT_NONSTDC_NO_DEPRECATE");
nob_cc_inputs(&cmd, target_c);
nob_cc_output(&cmd, target_exe);
if (!nob_cmd_run(&cmd)) {
@@ -273,13 +436,36 @@ bool run_target(const char *name, Nob_Cmd *args) {
// ---- 测试 ----
// Expect 协议:扫整个文件的注释行找 `// Expect retcode = N`,取第一条命中(注解可以在任意位置)。
// `// Expect compile_error` 表示编译必须失败;后面可跟一个或多个 E#### 错误码。
// `// Expect stdout = ...` 暂不比较,遇到跳过。
// 返回 false 只表示文件读不出来或 retcode 写坏了;没写注解则 has_retcode 为假,由调用方按 0 处理。
// 返回 false 只表示文件读不出来、retcode 写坏或错误码格式非法;没写注解则 has_retcode
// 为假,由调用方按 0 处理。
typedef struct {
bool has_retcode;
int retcode;
bool compile_error;
int error_codes[8];
size_t error_code_count;
bool has_location;
int line;
int col;
} test_expect_t;
static bool test_parse_error_code(Nob_String_View value, int *out) {
value = nob_sv_trim(value);
if (value.count != 5 || value.data[0] != 'E')
return false;
int v = 0;
size_t i = 1;
for (; i < value.count; i += 1) {
if (!isdigit((unsigned char)value.data[i]))
return false;
v = v * 10 + (value.data[i] - '0');
}
*out = v;
return true;
}
static bool test_parse_int(Nob_String_View value, int *out) {
if (value.count == 0)
return false;
@@ -321,10 +507,52 @@ static bool test_parse_expect(const char *fname, test_expect_t *expect) {
continue;
line = nob_sv_trim_left(nob_sv_from_parts(line.data + 6, line.count - 6));
Nob_String_View key = nob_sv_trim(nob_sv_chop_by_delim(&line, '='));
Nob_String_View key = nob_sv_trim(nob_sv_chop_by_delim(&line, ' '));
Nob_String_View value = nob_sv_trim(line);
if (nob_sv_eq(key, nob_sv_from_cstr("compile_error"))) {
expect->compile_error = true;
if (nob_sv_starts_with(value, nob_sv_from_cstr("="))) {
value = nob_sv_trim_left(nob_sv_from_parts(value.data + 1, value.count - 1));
}
while (value.count > 0) {
Nob_String_View code = nob_sv_trim(nob_sv_chop_by_delim(&value, ' '));
if (code.count == 0)
continue;
int parsed_code = 0;
if (expect->error_code_count >= NOB_ARRAY_LEN(expect->error_codes) ||
!test_parse_error_code(code, &parsed_code)) {
malformed = true;
break;
}
expect->error_codes[expect->error_code_count++] = parsed_code;
}
if (malformed)
break;
continue;
}
if (nob_sv_eq(key, nob_sv_from_cstr("location"))) {
if (expect->has_location) {
malformed = true;
break;
}
if (nob_sv_starts_with(value, nob_sv_from_cstr("="))) {
value = nob_sv_trim_left(nob_sv_from_parts(value.data + 1, value.count - 1));
}
Nob_String_View line_sv = nob_sv_trim(nob_sv_chop_by_delim(&value, ':'));
Nob_String_View col_sv = nob_sv_trim(value);
if (!test_parse_int(line_sv, &expect->line) || !test_parse_int(col_sv, &expect->col) ||
expect->line <= 0 || expect->col <= 0) {
malformed = true;
break;
}
expect->has_location = true;
continue;
}
if (!nob_sv_eq(key, nob_sv_from_cstr("retcode")) || expect->has_retcode)
continue;
if (nob_sv_starts_with(value, nob_sv_from_cstr("="))) {
value = nob_sv_trim_left(nob_sv_from_parts(value.data + 1, value.count - 1));
}
int parsed = 0;
if (!test_parse_int(value, &parsed)) {
@@ -388,18 +616,75 @@ typedef struct {
int no_expect; // 没写 Expect 注解、按 0 期望的文件数
} test_ctx_t;
static bool test_has_error_code(const Nob_Cmd_Result *result, int code) {
char needle[24] = {0};
snprintf(needle, sizeof(needle), "error[E%04d]", code);
return result->out.items != NULL && strstr(result->out.items, needle) != NULL;
}
static void test_compile_and_run(const char *fname, test_ctx_t *ctx) {
Nob_Cmd compile = {0};
nob_cmd_extend(&compile, &ctx->compiler);
nob_cmd_append(&compile, fname, ctx->out_sir);
bool compiled = nob_cmd_run(&compile);
nob_cmd_free(compile);
if (!compiled) {
nob_log(NOB_ERROR, "%s: compile failed", fname);
test_expect_t expect = {0};
if (!test_parse_expect(fname, &expect)) {
nob_log(NOB_ERROR, "%s: broken `// Expect` annotation", fname);
ctx->failed += 1;
return;
}
Nob_Cmd compile = {0};
nob_cmd_extend(&compile, &ctx->compiler);
nob_cmd_append(&compile, fname, ctx->out_sir);
Nob_Cmd_Result compile_result = {0};
if (!run_capture(&compile, &compile_result)) {
nob_log(NOB_ERROR, "%s: failed to launch compiler", fname);
nob_cmd_free(compile);
ctx->failed += 1;
return;
}
nob_cmd_free(compile);
bool compiled = compile_result.exit_code == 0;
if (expect.compile_error) {
if (compiled) {
nob_log(NOB_ERROR, "%s: expected compile failure, but it compiled", fname);
ctx->failed += 1;
} else {
size_t i = 0;
bool missing = false;
for (; i < expect.error_code_count; i += 1) {
if (!test_has_error_code(&compile_result, expect.error_codes[i])) {
nob_log(NOB_ERROR, "%s: missing expected error E%04d", fname,
expect.error_codes[i]);
missing = true;
}
}
if (expect.has_location && !missing) {
char needle[64];
snprintf(needle, sizeof(needle), ":%d:%d:", expect.line, expect.col);
if (strstr(compile_result.out.items, needle) == NULL) {
nob_log(NOB_ERROR, "%s: missing expected source location %d:%d", fname,
expect.line, expect.col);
missing = true;
}
}
if (missing) {
nob_log(NOB_ERROR, "%s: compiler output:\n%.*s", fname,
(int)compile_result.out.count, compile_result.out.items);
ctx->failed += 1;
} else {
ctx->passed += 1;
}
}
nob_cmd_result_free(compile_result);
return;
}
if (!compiled) {
nob_log(NOB_ERROR, "%s: compile failed:\n%.*s", fname, (int)compile_result.out.count,
compile_result.out.items);
nob_cmd_result_free(compile_result);
ctx->failed += 1;
return;
}
nob_cmd_result_free(compile_result);
Nob_Cmd run = {0};
nob_cmd_extend(&run, &ctx->vm);
nob_cmd_append(&run, ctx->out_sir);
@@ -412,22 +697,16 @@ static void test_compile_and_run(const char *fname, test_ctx_t *ctx) {
}
nob_cmd_free(run);
test_expect_t expect = {0};
if (!test_parse_expect(fname, &expect)) {
nob_log(NOB_ERROR, "%s: broken `// Expect retcode = N`", fname);
if (!expect.has_retcode) {
expect.retcode = 0; // 没写注解的库测试按"跑完退出 0"处理
ctx->no_expect += 1;
}
if (expect.retcode != result.exit_code) {
nob_log(NOB_ERROR, "%s: expect retcode %d, got %d", fname, expect.retcode,
result.exit_code);
ctx->failed += 1;
} else {
if (!expect.has_retcode) {
expect.retcode = 0; // 没写注解的库测试按"跑完退出 0"处理
ctx->no_expect += 1;
}
if (expect.retcode != result.exit_code) {
nob_log(NOB_ERROR, "%s: expect retcode %d, got %d", fname, expect.retcode,
result.exit_code);
ctx->failed += 1;
} else {
ctx->passed += 1;
}
ctx->passed += 1;
}
nob_cmd_result_free(result);
}
@@ -465,15 +744,6 @@ typedef struct {
void *user_data;
} walk_ctx_t;
static const char *path_basename(const char *path) {
const char *base = path;
for (const char *p = path; *p; p += 1) {
if (*p == '/' || *p == '\\')
base = p + 1;
}
return base;
}
static bool walk_match_filter(const char *path, const scc_argparse_list_t *filters) {
if (filters->count == 0)
return true;
@@ -765,6 +1035,15 @@ static const char *build_kind_name(int kind) {
}
}
// list 显示展开后的真实文件数,而不是"模式条数"。
static size_t count_files(const char **patterns, size_t count) {
str_da_t out = {0};
bool ok = expand_patterns(patterns, count, &out);
size_t n = ok ? out.count : count;
da_free(out);
return n;
}
static int cli_list(void) {
printf("targets (%zu):\n", (size_t)NOB_ARRAY_LEN(targets));
printf(" %-12s %-9s %s\n", "NAME", "KIND", "DETAIL");
@@ -774,16 +1053,18 @@ static int cli_list(void) {
switch (t->kind) {
case BUILD_EXE:
case BUILD_EXE_WITH_OPTIMIZATION:
printf(" %-12s %-9s srcs=%zu headers=%zu\n", t->name, kind, t->exe.srcs_count,
t->exe.headers_count);
printf(" %-12s %-9s srcs=%zu headers=%zu\n", t->name, kind,
count_files(t->exe.srcs, t->exe.srcs_count),
count_files(t->exe.headers, t->exe.headers_count));
break;
case BUILD_SIR:
printf(" %-12s %-9s %s <- %s deps=%zu\n", t->name, kind, t->sir.src, t->sir.compiler,
t->sir.deps_count);
count_files(t->sir.deps, t->sir.deps_count));
break;
case BUILD_NATIVE:
printf(" %-12s %-9s %s <- %s deps=%zu crt=%zu\n", t->name, kind, t->native.src,
t->native.compiler, t->native.deps_count, t->native.crt_deps_count);
t->native.compiler, count_files(t->native.deps, t->native.deps_count),
t->native.crt_deps_count);
break;
}
}